@@ -69,6 +69,7 @@ float4 _OcclusionTexture_TexelSize;
6969half _Intensity;
7070float _Radius;
7171float _Downsample;
72+ float3 _FogParams; // x: density, y: start, z: end
7273
7374// Accessors for packed AO/normal buffer
7475fixed4 PackAONormal (fixed ao, fixed3 n)
@@ -220,6 +221,28 @@ float3 PickSamplePoint(float2 uv, float index)
220221 return v * l;
221222}
222223
224+ // Fog handling in forward
225+ half ComputeFog (float z)
226+ {
227+ half fog = 0.0 ;
228+ #if FOG_LINEAR
229+ fog = (_FogParams.z - z) / (_FogParams.z - _FogParams.y);
230+ #elif FOG_EXP
231+ fog = exp2 (-_FogParams.x * z);
232+ #else // FOG_EXP2
233+ fog = _FogParams.x * z;
234+ fog = exp2 (-fog * fog);
235+ #endif
236+ return saturate (fog);
237+ }
238+
239+ float ComputeDistance (float depth)
240+ {
241+ float dist = depth * _ProjectionParams.z;
242+ dist -= _ProjectionParams.y;
243+ return dist;
244+ }
245+
223246//
224247// Distance-based AO estimator based on Morgan 2011 http://goo.gl/2iz3P
225248//
@@ -282,6 +305,13 @@ half4 FragAO(VaryingsMultitex i) : SV_Target
282305 // Apply other parameters.
283306 ao = pow (ao * _Intensity / _SampleCount, kContrast);
284307
308+ // Apply fog when enabled (forward-only)
309+ #if !FOG_OFF
310+ float d = Linear01Depth (SAMPLE_DEPTH_TEXTURE (_CameraDepthTexture, uv));
311+ d = ComputeDistance (d);
312+ ao *= ComputeFog (d);
313+ #endif
314+
285315 return PackAONormal (ao, norm_o);
286316}
287317
0 commit comments