Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Custom/AtmosphericFog" {
- Properties {
- _MainTex ("Base (RGB)", 2D) = "black" {}
- }
- CGINCLUDE
- #include "UnityCG.cginc"
- uniform sampler2D _MainTex;
- uniform sampler2D _CameraDepthTexture;
- uniform float _GlobalDensity;
- uniform float _SeaLevel;
- uniform float _HeightScale;
- uniform float _AuraPower;
- uniform float4 _FogColor;
- uniform float4 _SunColor;
- uniform float4 _MainTex_TexelSize;
- uniform float4 _SunDir;
- // for fast world space reconstruction
- uniform float4x4 _FrustumCornersWS;
- uniform float4 _CameraWS;
- struct v2f {
- float4 pos : POSITION;
- float2 uv : TEXCOORD0;
- float2 uv_depth : TEXCOORD1;
- float4 interpolatedRay : TEXCOORD2;
- };
- v2f vert( appdata_img v )
- {
- v2f o;
- float index = v.vertex.z;
- v.vertex.z = 0.1; // 0.1
- o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
- o.uv = v.texcoord.xy;
- o.uv_depth = v.texcoord.xy;
- #if UNITY_UV_STARTS_AT_TOP
- if (_MainTex_TexelSize.y < 0)
- o.uv.y = 1-o.uv.y;
- #endif
- o.interpolatedRay = _FrustumCornersWS[(int)index];
- o.interpolatedRay.w = index;
- return o;
- }
- half4 frag (v2f i) : COLOR
- {
- float depth = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv_depth)));
- float3 worldPos = (_CameraWS + depth * i.interpolatedRay);
- float3 ray = worldPos - _CameraWS;
- float3 rayDir = normalize(ray);
- float distance = length(ray);
- float camHeightSeaLevel = _CameraWS.y - _SeaLevel;
- float fogDensity = _GlobalDensity * exp(-camHeightSeaLevel*_HeightScale) * (1.0 - exp(-distance*rayDir.y*_HeightScale))/rayDir.y;
- fogDensity = saturate(fogDensity);
- // Mask out skybox with depthbuffer hack
- if (depth == 1.0) {
- fogDensity = 0;
- }
- float sunAmount = max(dot(rayDir, _SunDir), 0.0);
- float4 fogColor = lerp(_FogColor, _SunColor, pow(sunAmount, _AuraPower));
- return lerp(tex2D(_MainTex, i.uv), fogColor, fogDensity);
- }
- ENDCG
- SubShader {
- Pass {
- ZTest Always Cull Off ZWrite Off
- Fog { Mode off }
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma fragmentoption ARB_precision_hint_fastest
- #pragma exclude_renderers flash
- ENDCG
- }
- }
- Fallback off
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement