Advertisement
Snoober

fog shader

Oct 23rd, 2020
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Unlit/Stencil Fog"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Texture", 2D) = "white" {}
  6.         _Color("Color", Color) = (0, 0, 0, 1)
  7.     }
  8.     SubShader
  9.     {
  10.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
  11.         LOD 100
  12.         Cull Off
  13.         ZWrite Off
  14.         Blend SrcAlpha OneMinusSrcAlpha
  15.  
  16.         Stencil {
  17.             Ref 1
  18.             Comp NotEqual
  19.         }
  20.  
  21.         Pass
  22.         {
  23.             CGPROGRAM
  24.             #pragma vertex vert
  25.             #pragma fragment frag
  26.             // make fog work
  27.             #pragma multi_compile_fog
  28.  
  29.             #include "UnityCG.cginc"
  30.  
  31.             fixed4 _Color;
  32.  
  33.             struct appdata
  34.             {
  35.                 float4 vertex : POSITION;
  36.                 float2 uv : TEXCOORD0;
  37.                 fixed4 color : COLOR;
  38.             };
  39.  
  40.             struct v2f
  41.             {
  42.                 float2 uv : TEXCOORD0;
  43.                 UNITY_FOG_COORDS(1)
  44.                 float4 vertex : SV_POSITION;
  45.                 fixed4 color : COLOR;
  46.             };
  47.  
  48.             sampler2D _MainTex;
  49.             float4 _MainTex_ST;
  50.  
  51.             v2f vert (appdata v)
  52.             {
  53.                 v2f o;
  54.                 o.vertex = UnityObjectToClipPos(v.vertex);
  55.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  56.                 UNITY_TRANSFER_FOG(o,o.vertex);
  57.                 o.color = v.color;
  58.                 return o;
  59.             }
  60.  
  61.             fixed4 frag (v2f i) : SV_Target
  62.             {
  63.                 // sample the texture
  64.                 fixed4 col = tex2D(_MainTex, i.uv);
  65.                 col *= _Color;
  66.                 // apply fog
  67.                 UNITY_APPLY_FOG(i.fogCoord, col);
  68.                 return col;
  69.             }
  70.             ENDCG
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement