In the game often see this effect, the hero walked behind the obstacles, but we can see through the obstacles to the body of the hero, as if we have a perspective eye general.
It's all a routine.
In fact, the program ape was drawn two times when the hero model was shown.
Once the obscured part was painted with a translucent look.
The other was the one that was not obscured and painted again.
The following is implemented in unity.
First create a new material, Shader, scene.
Build a scene, a cube, a capsule.
Well, now is the most normal situation, capsule was blocked by the cube.
Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn
Modify the Shader below.
Shader "Cookbookshaders/cover Translucent" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}}subshader {Tags {"Render Type "=" Opaque "}lod 200ZWrite onztest Greater //greater/gequal/less/lequal/equal/notequal/always/never/off The default is lequal if the z-value of the pixel to be drawn is equal to the value in the depth buffer, it is replaced with the new pixel color value. This uses Greater, which means that if the current pixel Z-value to be rendered is greater than Z in the buffer, it is rendered, that is, the object behind it covers the front. Cgprogram#pragma Surface Surf lambertsampler2d _maintex;struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}
Just added two words before Cgprogram, set two things.
Zwrite Onztest Greater
Zwrite represents whether depth is written to the depth buffer, which is on by default
The ZTest representative determines whether the color of the current pixel is written to the color buffer by judging the depth, that is, whether to replace the previous pixel color with the current pixel color.
There are several values to be taken:
Greater/gequal/less/lequal/equal/notequal/always/never/off
The default is lequal if the z-value of the pixel to be drawn is equal to the value in the depth buffer, it is replaced with the new pixel color value.
This uses Greater, which means that if the current pixel Z-value to be rendered is greater than Z in the buffer, it is rendered, that is, the object behind it covers the front.
Now get the following effect:
This is the first drawing, covering the back of the capsule with the previous cube.
Then the second draw begins, which is not obscured.
Since it is not obscured, just follow the most common method,
Shader "Cookbookshaders/cover Translucent" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}}subshader {Tags {"Render Type "=" Opaque "}lod 200ZWrite onztest Greater//greater/gequal/less/lequal/equal/notequal/always/never/off default is LEqual If the z-value of the pixel to be drawn is equal to the value in the depth buffer, it is replaced with the new pixel color value. This uses Greater, which means that if the current pixel Z-value to be rendered is greater than Z in the buffer, it is rendered, that is, the object behind it covers the front. Cgprogram#pragma Surface Surf lambertsampler2d _maintex;struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.A;} endcg//the Ztest greater is set above, only the areas that are obscured are rendered. So the next place to render the area without occlusion. Zwrite onztest lequal//greater/gequal/less/lequal/equal/notequal/always/never/off default is LEqual if you want to draw the Z-value of the pixel The small remainder equals the value in the depth buffer, then replaces it with the new pixel color value. This uses Greater, which means that if the current pixel Z-value to be rendered is greater than Z in the buffer, it is rendered, that is, the object behind it covers the front. Cgprogram#pragma Surface Surf lambertsampler2d _maintex;struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.A;}ENDCG} FallBack "Diffuse"}
transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn
Now that's the way it works.
It looks like capsule in front of the cube, shader the effect is this, but we can not use this to put in the game, because there will be a lot of misunderstanding ...
Let's set this part of the occlusion to be transparent, and achieve the effect of being like a lol hiding grass.
The next step is to modify the code that was drawn for the first time and turn it transparent.
Now that you want to do the transparency, add the alpha tag and set Alpha.
The final changes are as follows
Shader "Cookbookshaders/cover Translucent" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}}subshader {Tags {"Render Type "=" Opaque "}lod 200ZWrite onztest Greater//greater/gequal/less/lequal/equal/notequal/always/never/off default is LEqual If the z-value of the pixel to be drawn is equal to the value in the depth buffer, it is replaced with the new pixel color value. This uses Greater, which means that if the current pixel Z-value to be rendered is greater than Z in the buffer, it is rendered, that is, the object behind it covers the front. Cgprogram#pragma Surface Surf Lambert alpha//Plus alpha allows this part of the occlusion to be transparently displayed sampler2d _maintex;struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = 0.5f; Set Alpha to 0.5}endcg//the Ztest greater is set above, only the areas that are obscured are rendered. So the next place to render the area without occlusion. Zwrite onztest lequal//greater/gequal/less/lequal/equal/notequal/always/never/off default is LEqual if you want to draw the Z-value of the pixel The small remainder equals the value in the depth buffer, then replaces it with the new pixel color value. Cgprogram#pragma Surface Surf lambertsampler2d _maintex;struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallbaCK "Diffuse"}
Final effecttransfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn
Sample Project Download:
Http://pan.baidu.com/s/1pKT17sf
Unity Shaders and Effects Cookbook (D-1) set ZTest to achieve occlusion semi-transparent effect