Unity perspective mirror effect shader template test to achieve shader learning miscellaneous (1)

1. An example of perspective mirror effect

. Three objects are created in the scene, a square, a wall, and a sphere.
Then create three shaders and three initial shaders . Drag the three shaders to the three shaders and
then add the material. Drag the ball to three objects. Give these three objects red, blue, and green for easy observation.

Look at the effect of using the red square as a perspective mirror:
Game view: It

can be clearly seen through the area of ​​the red square. See the green sphere through the wall.

2. Effect realization The
perspective effect is actually achieved through the template test. Use the red square as the mask, add the same template value to the three shaders, and then pass the sphere through the template test.
Specific steps: Open the three initial shaders created, find the pass, and add a template test

//cube.shader  红色方块
 Pass
        {
    
    
		Stencil
			{
    
    
                Ref 2 //模板值给一个2,三个物体模板值都必须相同
                Comp always//表示在模板测试中总是通过
                Pass replace//作为遮罩物
            }
//wall.shader  蓝色墙体
Pass
        {
    
    
			Stencil
			{
    
    
                Ref 2 
                Comp NotEqual//不通过模板测试
            }
//sphere.shader  绿色球体
Pass
        {
    
    
			Stencil
			{
    
    
                Ref 2 
                Comp Equal//通过模板测试
            }

Now let’s look at the effect:

you can see the red square, but you can’t see the green sphere through it. This is because in the template test, the square gives Comp always and the sphere gives Comp Equal. Both of them will pass the template. Test, so it will be written to the buffer, and only display objects closer to the camera.
So we need to add a code to the pass of cube.shader: ZWrite Off

 Pass
        {
    
    
		Stencil
			{
    
    
                Ref 2 //模板值给一个2,三个物体模板值都必须相同
                Comp always
                Pass replace
            }
			ZWrite Off//不写入缓冲区

Now let's look at the effect:

the perspective effect has now been achieved.

This caused additional problems.
Now when you look at the sphere directly in the game, it is invisible:

This is because after passing the template test, it will not be written in the buffer outside the perspective range. The solution is very simple, that is, just copy its pass code, that is, there are two passes in a shader , But the copied pass code should delete the test template code inside, that is, the Stencil code, so that the object can be directly observed in the game

3. Brief description of the principle of
template test The template test can be understood as dividing the buffer into two, one is the range where the masked object is located, and the other is the range outside the masked object. The two ranges in the above example are shown in the figure:

Summarize the various situations into a table:
in the table below, temporarily call
the buffer in the range of the
masked object: inner buffer, and the bufferoutside the masked object as outer buffer.

Whether as a mask Whether to pass the mask test Write buffer situation
Yes Yes Will be written to the inner and outer buffers
Yes no Will not write to the inner buffer, will write to the outer buffer
no Yes Will write to the inner buffer, not the outer buffer
no no Will not write to the inner buffer, will write to the outer buffer

Using these principles can achieve a simple perspective effect. If you want to achieve a semi-perspective effect similar to X-ray, you must learn the rendering queue control in depth.
That's it for this article, everyone is welcome to comment!

Guess you like

Origin blog.csdn.net/qq_40385747/article/details/108208196