EnviroHeightFog.shader 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Shader "Hidden/EnviroHeightFog"
  2. {
  3. Properties
  4. {
  5. //_MainTex ("Texture", any) = "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. #pragma multi_compile __ ENVIROURP
  17. #pragma multi_compile __ UNITY_COLORSPACE_GAMMA
  18. #include "UnityCG.cginc"
  19. #include "../Includes/FogInclude.cginc"
  20. struct v2f
  21. {
  22. float2 uv : TEXCOORD0;
  23. float4 position : SV_POSITION;
  24. float3 ray : TEXCOORD1;
  25. UNITY_VERTEX_OUTPUT_STEREO
  26. };
  27. v2f vert (appdata_img v)
  28. {
  29. v2f o;
  30. UNITY_SETUP_INSTANCE_ID(v);
  31. UNITY_INITIALIZE_OUTPUT(v2f, o);
  32. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  33. #if defined(ENVIROURP)
  34. o.position = float4(v.vertex.xyz,1.0);
  35. #if UNITY_UV_STARTS_AT_TOP
  36. o.position.y *= -1;
  37. #endif
  38. #else
  39. o.position = UnityObjectToClipPos(v.vertex);
  40. #endif
  41. o.uv = v.texcoord;
  42. return o;
  43. }
  44. float4x4 _LeftWorldFromView;
  45. float4x4 _RightWorldFromView;
  46. float4x4 _LeftViewFromScreen;
  47. float4x4 _RightViewFromScreen;
  48. UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
  49. UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
  50. void InverseProjectDepth (float depth, float2 texcoord, out float3 worldPos, out float dist, out float3 viewDir)
  51. {
  52. float4x4 proj, eyeToWorld;
  53. if (unity_StereoEyeIndex == 0)
  54. {
  55. proj = _LeftViewFromScreen;
  56. eyeToWorld = _LeftWorldFromView;
  57. }
  58. else
  59. {
  60. proj = _RightViewFromScreen;
  61. eyeToWorld = _RightWorldFromView;
  62. }
  63. #if !UNITY_UV_STARTS_AT_TOP
  64. texcoord.y = 1 - texcoord.y;
  65. #endif
  66. float2 uvClip = texcoord * 2.0 - 1.0;
  67. float clipDepth = depth; // Fix for OpenGl Core thanks to Lars Bertram
  68. clipDepth = (UNITY_NEAR_CLIP_VALUE < 0) ? clipDepth * 2 - 1 : clipDepth;
  69. float4 clipPos = float4(uvClip, clipDepth, 1.0);
  70. float4 viewPos = mul(proj, clipPos); // inverse projection by clip position
  71. viewPos /= viewPos.w; // perspective division
  72. worldPos = mul(eyeToWorld, viewPos).xyz;
  73. viewDir = worldPos - _WorldSpaceCameraPos.xyz;
  74. dist = length(viewDir);
  75. viewDir /= dist;
  76. }
  77. float4 frag (v2f i) : SV_Target
  78. {
  79. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  80. float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
  81. float linearDepth = Linear01Depth(depth);
  82. float viewDistance;
  83. float3 worldPos, viewDir;
  84. InverseProjectDepth(depth, i.uv.xy, worldPos, viewDistance, viewDir);
  85. float4 col = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv);
  86. float4 fog = GetExponentialHeightFog(worldPos,linearDepth);
  87. //this is not correct but LinearToGamma does produce even worse results..
  88. #if defined(UNITY_COLORSPACE_GAMMA)
  89. fog.rgb *= 1.5;
  90. #endif
  91. float3 final = ApplyVolumetricLights(fog,col.rgb, i.uv);
  92. return float4(final.rgb,1);
  93. }
  94. ENDCG
  95. }
  96. }
  97. }