DepthFog.shader 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Shader "Hidden/DepthFog"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. }
  7. SubShader
  8. {
  9. // No culling or depth
  10. Cull Off ZWrite Off ZTest Always
  11. Pass
  12. {
  13. CGPROGRAM
  14. #pragma vertex vert
  15. #pragma fragment frag
  16. #include "UnityCG.cginc"
  17. struct appdata
  18. {
  19. float4 vertex : POSITION;
  20. float2 uv : TEXCOORD0;
  21. };
  22. struct v2f
  23. {
  24. float2 uv : TEXCOORD0;
  25. float4 vertex : SV_POSITION;
  26. };
  27. v2f vert (appdata v)
  28. {
  29. v2f o;
  30. o.vertex = UnityObjectToClipPos(v.vertex);
  31. o.uv = v.uv;
  32. return o;
  33. }
  34. sampler2D _MainTex;
  35. sampler2D _CameraDepthTexture;
  36. float _StartDistance,_EndDistance,_Height;
  37. fixed4 _FogCol;
  38. float4x4 _InvVP;
  39. fixed4 frag (v2f i) : SV_Target
  40. {
  41. fixed4 col = tex2D(_MainTex, i.uv);
  42. float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
  43. float4 proj = float4(i.uv.xy*2-1, depth, 1);
  44. float4 worldPos = mul(_InvVP, proj);
  45. worldPos.xyz /= worldPos.w;
  46. float ditance = length(worldPos.xyz - _WorldSpaceCameraPos.xyz);
  47. fixed fogFactor = smoothstep(_StartDistance, _EndDistance, ditance);
  48. fixed fogFactorHeigh = 1 - smoothstep(0, _Height, worldPos.y);
  49. fogFactor *= fogFactorHeigh;
  50. fixed3 finalCol = lerp(col.rgb, _FogCol, fogFactor* _FogCol.a);
  51. return fixed4(finalCol,1);
  52. }
  53. ENDCG
  54. }
  55. }
  56. }