The author introduces: Jiang Xiewei, IT company technology partner, it senior lecturer, CSDN Community expert. Invited editors. Best-selling author, national patent inventor; published books: "Teach you architecture 3D Game engine" electronic industry publishing house and "Unity3d actual combat core technology specific explanation" electronic industry publishing house.
CSDN Video URL: http://edu.csdn.net/lecturer/144
We understand the production principle of Shader forge, will also use shader forge to make the rendering material, in the actual project development, we will encounter a variety of problems. Sometimes we need to use shader forge to render objects that work with the shader that we write ourselves together. In the process of cooperation will encounter problems, the following will be the author in the project development problems encountered and solutions to the reader to introduce.
We used the shade forge to render a metal armor with another transparent head in the metal armor, rendering effects such as those seen below:
The face is treated with an alpha-channel shader, and in the process of rotation, the face can not hide the model, so it looks very strange, such as the following see:
The arrows indicate that the place is not obscured by a transparent face. The shader used for face rendering are as follows:
Shader "Unlit/transparent_test" {Properties {_maintex ("Base (RGB) Trans (A)", 2D) = "White" {}}subshader {Tags {"Queue" = " Transparent "" Ignoreprojector "=" True "" rendertype "=" Transparent "}lod 100//zwrite offblend srcalpha Oneminussrcalpha Pass { cgprogram#pragma vertex vert#pragma fragment frag#pragma target 2.0#pragma multi_compile_fog#include " Unitycg.cginc "struct appdata_t {float4 vertex:position;float2 texcoord:texcoord0;}; struct V2F {float4 vertex:sv_position;half2 texcoord:texcoord0; Unity_fog_coords (1)};sampler2d _maintex;float4 _maintex_st;v2f Vert (appdata_t v) {v2f O;o.vertex = Unityobjecttoclippos (V.vertex); O.texcoord = Transform_tex (V.texcoord, _maintex); Unity_transfer_fog (O,o.vertex); return o;} Fixed4 Frag (v2f i): sv_target{fixed4 col = tex2d (_maintex, I.texcoord); Unity_apply_fog (I.fogcoord, col); return col;} ENDCG}}}
This shader is also the shader of Unity3d. Just changed the name of the file, in order to let the transparent face cover that half of the armor. How do I change it?
The Unity3d engine gives us the rendering order, in fact the main problem with this situation is the rendering order problem. It is very obvious that the armor is rendered first, and then the transparent face is rendered.
The problem has been found. Next you need to change the rendering order. is to render the transparent face material first. Re-render metal armor. This can be achieved occlusion relationship, and then the original shader based on changes such as the following:
Shader "Unlit/transparent_test" {Properties {_maintex ("Base (RGB) Trans (A)", 2D) = "White" {}}subshader {Tags {"Queue" = " Transparent-2000"" ignoreprojector "=" True "" rendertype "=" Transparent "}lod 100//zwrite offblend Srcalpha Oneminussrcalpha Pass { cgprogram#pragma vertex vert#pragma fragment frag#pragma target 2.0#pragma Multi_compile_ Fog#include "Unitycg.cginc" struct appdata_t {float4 vertex:position;float2 texcoord:texcoord0;}; struct V2F {float4 vertex:sv_position;half2 texcoord:texcoord0; Unity_fog_coords (1)};sampler2d _maintex;float4 _maintex_st;v2f Vert (appdata_t v) {v2f O;o.vertex = Unityobjecttoclippos (V.vertex); O.texcoord = Transform_tex (V.texcoord, _maintex); Unity_transfer_fog (O,o.vertex); return o;} Fixed4 Frag (v2f i): sv_target{fixed4 col = tex2d (_maintex, I.texcoord); Unity_apply_fog (I.fogcoord, col); return col;} ENDCG}}}
See the bold part of the code above, and then put it on the face model and look at the effect:
So we can conquer the problem. Isn't it very easy? In fact, shader programming is not as difficult as it might seem. Only careful analysis can get the desired effect, so learning unity shader programming must go into it.
Unity3d Advanced Rendering-shader Forge enhanced version