Documentation

Unity Engine


Unity Engine


Fog shader example in the Built-In Render Pipeline

Example of a shader that renders fog.
Read time 1 minuteLast updated a month ago

Shader "Custom/TextureCoordinates/Fog" { SubShader { Pass { HLSLPROGRAM #pragma vertex vert #pragma fragment frag //Needed for fog variation to be compiled. #pragma multi_compile_fog #include "UnityCG.cginc" struct vertexInput { float4 vertex : POSITION; float4 texcoord0 : TEXCOORD0; }; struct fragmentInput{ float4 position : SV_POSITION; float4 texcoord0 : TEXCOORD0; //Creates a variable that contains fog coordinates. The parameter must be a free TEXCOORD, for example 1 if TEXCOORD1 is free. UNITY_FOG_COORDS(1) }; fragmentInput vert(vertexInput i){ fragmentInput o; o.position = UnityObjectToClipPos(i.vertex); o.texcoord0 = i.texcoord0; //Compute fog amount from clip space position. UNITY_TRANSFER_FOG(o,o.position); return o; } fixed4 frag(fragmentInput i) : SV_Target { fixed4 color = fixed4(i.texcoord0.xy,0,0); //Apply fog (additive pass are automatically handled) UNITY_APPLY_FOG(i.fogCoord, color); //to handle custom fog color another option would have been //#ifdef UNITY_PASS_FORWARDADD // UNITY_APPLY_FOG_COLOR(i.fogCoord, color, float4(0,0,0,0)); //#else // fixed4 myCustomColor = fixed4(0,0,1,0); // UNITY_APPLY_FOG_COLOR(i.fogCoord, color, myCustomColor); //#endif return color; } ENDHLSL } }}