Light Scattering
by Ayush Chaudhary
Implementation of Light Scattering as published in GPU Gems 2 - nVidia
Light Scattering / God Rays Shader
exposure controls the overall intensity of the post-process,
weight controls the intensity of each sample,
decay dissipates each sample's contribution as the ray progresses away from the light source
exposure controls the overall intensity of the post-process,
weight controls the intensity of each sample,
decay dissipates each sample's contribution as the ray progresses away from the light source
/* Values that are to be passed for getting ideal Light Scattering(Not Overdone) Density = 0.34f; Weight = 0.5f; Decay = 0.97f; Exposition = 0.015f; */ sampler baseTexture : register(s0); Texture frameTex; sampler2D frameSampler = sampler_state { Texture =; ADDRESSU = CLAMP; ADDRESSV = CLAMP; MAGFILTER = LINEAR; MINFILTER = LINEAR; MIPFILTER = LINEAR; }; float4x4 View; float4x4 WorldViewProjection; half Density = 0.8f; half Weight = 0.9f; half Decay = 0.5f; half Exposition = 0.5f; half3 LightPosition; half3 LightDirection = {1.0f, 1.0f, 1.0f}; half3 CameraPos; int numSamples; half4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0 { half4 screenPos = mul(LightPosition, WorldViewProjection); half2 ssPos = screenPos.xy / screenPos.w * float2(0.5,-0.5) + 0.5; half2 oriTexCoord = texCoord; half2 deltaTexCoord = (texCoord - ssPos); deltaTexCoord *= 1.0f / numSamples * Density; half3 color = tex2D(baseTexture, texCoord); half illuminationDecay = 1.0f; for (int i=0; i < 32; i++) { texCoord -= deltaTexCoord; half3 sample = tex2D(baseTexture, texCoord); sample *= illuminationDecay * Weight; color += sample; illuminationDecay *= Decay; } half amount = dot(mul(LightDirection,View), half3(0.0f,0.0f,-1.0f)); half4 sampleFrame = tex2D(frameSampler, oriTexCoord); return saturate(amount-0.5) * half4(color * Exposition, 1) + sampleFrame; } technique Scatter { pass Pass1 { PixelShader = compile ps_2_b PixelShaderFunction(); } }
A few images
Rays overdone :
Windowy :
Other Pictures :