Advertisement
m_zandvliet

AtmosphericFog.shader

Sep 10th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. Shader "Custom/AtmosphericFog" {
  2. Properties {
  3. _MainTex ("Base (RGB)", 2D) = "black" {}
  4. }
  5.  
  6. CGINCLUDE
  7.  
  8. #include "UnityCG.cginc"
  9.  
  10. uniform sampler2D _MainTex;
  11. uniform sampler2D _CameraDepthTexture;
  12.  
  13. uniform float _GlobalDensity;
  14. uniform float _SeaLevel;
  15. uniform float _HeightScale;
  16. uniform float _AuraPower;
  17. uniform float4 _FogColor;
  18. uniform float4 _SunColor;
  19. uniform float4 _MainTex_TexelSize;
  20.  
  21. uniform float4 _SunDir;
  22.  
  23. // for fast world space reconstruction
  24. uniform float4x4 _FrustumCornersWS;
  25. uniform float4 _CameraWS;
  26.  
  27. struct v2f {
  28. float4 pos : POSITION;
  29. float2 uv : TEXCOORD0;
  30. float2 uv_depth : TEXCOORD1;
  31. float4 interpolatedRay : TEXCOORD2;
  32. };
  33.  
  34. v2f vert( appdata_img v )
  35. {
  36. v2f o;
  37. float index = v.vertex.z;
  38. v.vertex.z = 0.1; // 0.1
  39. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  40. o.uv = v.texcoord.xy;
  41. o.uv_depth = v.texcoord.xy;
  42.  
  43. #if UNITY_UV_STARTS_AT_TOP
  44. if (_MainTex_TexelSize.y < 0)
  45. o.uv.y = 1-o.uv.y;
  46. #endif
  47.  
  48. o.interpolatedRay = _FrustumCornersWS[(int)index];
  49. o.interpolatedRay.w = index;
  50.  
  51. return o;
  52. }
  53.  
  54. half4 frag (v2f i) : COLOR
  55. {
  56. float depth = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv_depth)));
  57.  
  58. float3 worldPos = (_CameraWS + depth * i.interpolatedRay);
  59. float3 ray = worldPos - _CameraWS;
  60. float3 rayDir = normalize(ray);
  61.  
  62. float distance = length(ray);
  63.  
  64. float camHeightSeaLevel = _CameraWS.y - _SeaLevel;
  65. float fogDensity = _GlobalDensity * exp(-camHeightSeaLevel*_HeightScale) * (1.0 - exp(-distance*rayDir.y*_HeightScale))/rayDir.y;
  66. fogDensity = saturate(fogDensity);
  67.  
  68. // Mask out skybox with depthbuffer hack
  69. if (depth == 1.0) {
  70. fogDensity = 0;
  71. }
  72.  
  73. float sunAmount = max(dot(rayDir, _SunDir), 0.0);
  74. float4 fogColor = lerp(_FogColor, _SunColor, pow(sunAmount, _AuraPower));
  75.  
  76. return lerp(tex2D(_MainTex, i.uv), fogColor, fogDensity);
  77. }
  78.  
  79. ENDCG
  80.  
  81. SubShader {
  82. Pass {
  83. ZTest Always Cull Off ZWrite Off
  84. Fog { Mode off }
  85.  
  86. CGPROGRAM
  87.  
  88. #pragma vertex vert
  89. #pragma fragment frag
  90. #pragma fragmentoption ARB_precision_hint_fastest
  91. #pragma exclude_renderers flash
  92.  
  93. ENDCG
  94. }
  95. }
  96.  
  97. Fallback off
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement