The author introduces: Jiang Xiewei, IT company technology partner, it senior lecturer, CSDN Community expert, guest editor, best-selling author, national patent inventor; published books: Teach You architecture 3D game engine, electronic industry press andUnity3d Actual combat core technical details of the 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 rendered objects with our own written shader mutual cooperation in order to play a role in the process of cooperation will encounter problems, the following will be the author in the project development problems and solutions to the reader to introduce.
We used the shade forge to render a metal armor with a transparent head inside the metal armor, rendering the effect as follows:
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, the effect is as follows:
The arrows indicate that the place is not obscured by a transparent face, and the shader used for face rendering are as follows:
Shader " Unlit/transparent_test "{Properties {_maintex (" Base (RGB) Trans (A) ", 2D) =" White "{}}subshader {Tags {" Queue "=" Transpar Ent "" 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 unity3d own shader, just modified the file name, in order to let transparent face cover that half armor, how to modify?
The Unity3d engine gives us the rendering order, in fact the main problem with this situation is the rendering order problem, it is obvious to render the armor first, and then render the transparent face. The problem is found, the next need to modify the rendering order, is to render a transparent face material, and then render metal armor, so that the occlusion relationship can be achieved, and then the original shader on the basis of modification as follows:
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 section of the code above so that it can be modified to hang onto the face model and look at the effect:
So we can solve the problem, isn't it simple? In fact, shader programming is not so difficult to imagine, as long as careful analysis can get the desired effect, so learning unity shader programming must go into.
Unity3d Advanced Rendering-shader Forge Enhanced Edition