Advertisement
Guest User

FogPlane.shader

a guest
Nov 20th, 2019
3,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Unlit/FogPlane"
  2. {
  3.     Properties
  4.     {
  5.         _Tint("Fog Tint", Color) = (1, 1, 1, .5)
  6.         _Strength("Fog Strength", Range(0,3)) = 0.5
  7.  
  8.     }
  9.         SubShader
  10.     {
  11.         Tags { "RenderType" = "Opaque"  "Queue" = "Transparent" }
  12.         LOD 100
  13.         Blend SrcAlpha OneMinusSrcAlpha
  14.  
  15.         Pass
  16.         {
  17.             CGPROGRAM
  18.             #pragma vertex vert
  19.             #pragma fragment frag
  20.             // make fog work
  21.             #pragma multi_compile_fog
  22.  
  23.             #include "UnityCG.cginc"
  24.  
  25.             struct appdata
  26.             {
  27.                 float4 vertex : POSITION;
  28.             };
  29.  
  30.             struct v2f
  31.             {
  32.                 UNITY_FOG_COORDS(1)
  33.                 float4 vertex : SV_POSITION;
  34.                 float4 scrPos : TEXCOORD2;//
  35.             };
  36.  
  37.             float4 _Tint;
  38.             uniform sampler2D _CameraDepthTexture; //Depth Texture
  39.             float _Strength;
  40.  
  41.             v2f vert(appdata v)
  42.             {
  43.                 v2f o;
  44.                 o.vertex = UnityObjectToClipPos(v.vertex);
  45.                 o.scrPos = ComputeScreenPos(o.vertex); // grab position on screen
  46.                 UNITY_TRANSFER_FOG(o,o.vertex);
  47.                 return o;
  48.             }
  49.  
  50.             fixed4 frag(v2f i) : SV_Target
  51.             {
  52.                 half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos))); // depth
  53.                 half4 fog = (_Strength * (depth - i.scrPos.w));// fog by comparing depth and screenposition
  54.                 half4 col = fog * _Tint;// add the color
  55.                 col = saturate(col);// clamp to prevent weird artifacts
  56.                 UNITY_APPLY_FOG(i.fogCoord, col); // comment out this line if you want this fog to override the fog in lighting settings
  57.                 return col;
  58.             }
  59.             ENDCG
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement