You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.5 KiB

  1. Shader "Hidden/Post FX/Fog"
  2. {
  3. Properties
  4. {
  5. _MainTex("Main Texture", 2D) = "white" {}
  6. }
  7. CGINCLUDE
  8. #pragma multi_compile __ FOG_LINEAR FOG_EXP FOG_EXP2
  9. #include "UnityCG.cginc"
  10. #include "Common.cginc"
  11. #define SKYBOX_THREASHOLD_VALUE 0.9999
  12. struct Varyings
  13. {
  14. float2 uv : TEXCOORD0;
  15. float4 vertex : SV_POSITION;
  16. };
  17. Varyings VertFog(AttributesDefault v)
  18. {
  19. Varyings o;
  20. o.vertex = UnityObjectToClipPos(v.vertex);
  21. o.uv = UnityStereoScreenSpaceUVAdjust(v.texcoord, _MainTex_ST);
  22. return o;
  23. }
  24. sampler2D _CameraDepthTexture;
  25. half4 _FogColor;
  26. float _Density;
  27. float _Start;
  28. float _End;
  29. half ComputeFog(float z)
  30. {
  31. half fog = 0.0;
  32. #if FOG_LINEAR
  33. fog = (_End - z) / (_End - _Start);
  34. #elif FOG_EXP
  35. fog = exp2(-_Density * z);
  36. #else // FOG_EXP2
  37. fog = _Density * z;
  38. fog = exp2(-fog * fog);
  39. #endif
  40. return saturate(fog);
  41. }
  42. float ComputeDistance(float depth)
  43. {
  44. float dist = depth * _ProjectionParams.z;
  45. dist -= _ProjectionParams.y;
  46. return dist;
  47. }
  48. half4 FragFog(Varyings i) : SV_Target
  49. {
  50. half4 color = tex2D(_MainTex, i.uv);
  51. float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
  52. depth = Linear01Depth(depth);
  53. float dist = ComputeDistance(depth);
  54. half fog = 1.0 - ComputeFog(dist);
  55. return lerp(color, _FogColor, fog);
  56. }
  57. half4 FragFogExcludeSkybox(Varyings i) : SV_Target
  58. {
  59. half4 color = tex2D(_MainTex, i.uv);
  60. float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
  61. depth = Linear01Depth(depth);
  62. float skybox = depth < SKYBOX_THREASHOLD_VALUE;
  63. float dist = ComputeDistance(depth);
  64. half fog = 1.0 - ComputeFog(dist);
  65. return lerp(color, _FogColor, fog * skybox);
  66. }
  67. ENDCG
  68. SubShader
  69. {
  70. Cull Off ZWrite Off ZTest Always
  71. Pass
  72. {
  73. CGPROGRAM
  74. #pragma vertex VertFog
  75. #pragma fragment FragFog
  76. ENDCG
  77. }
  78. Pass
  79. {
  80. CGPROGRAM
  81. #pragma vertex VertFog
  82. #pragma fragment FragFogExcludeSkybox
  83. ENDCG
  84. }
  85. }
  86. }