# God Rays

## Recommended Posts

Asheh    354
I have been trying to impliment god rays from gpu gems 3 i have the pipeline setup where each light is rendered as a sphere (circle) in white then the geometry in black. Using this texture i perform a post process, taken from nvidia:
float4 PostProcessGodRaysPS( float2 OriginalUV : TEXCOORD0 ) : COLOR
{
float4 lightPos = mul(g_LightSources[0].Position, g_CurrWorldViewProj);
float2 screenLightPos = lightPos.xy / lightPos.w * 0.5f - 0.5f;
screenLightPos.y = 1.0f - screenLightPos.y;

// Calculate vector from pixel to light source in screen space.
half2 deltaTexCoord = (OriginalUV - screenLightPos.xy);

// Divide by number of samples and scale by control factor.
deltaTexCoord *= 1.0f / NUM_SAMPLES * Density;

// Store initial sample.
half3 color = tex2D(GodRaysSamp, OriginalUV);

// Set up illumination decay factor.
half illuminationDecay = 1.0f;

// Evaluate summation from Equation 3 NUM_SAMPLES iterations.
for (int i = 0; i < NUM_SAMPLES; i++)
{
// Step sample location along ray.
OriginalUV -= deltaTexCoord;
// Retrieve sample at new location.
half3 sample = tex2D(GodRaysSamp, OriginalUV);
// Apply sample attenuation scale/decay factors.
sample *= illuminationDecay * Weight;
// Accumulate combined color.
color += sample;
// Update exponential decay factor.
illuminationDecay *= Decay;
}
// Output final color with a further scale control factor.
return float4( color * Exposure, 1);
}


The outcome is not light streaks but mearly a repeating sphere from center to top right of the screen. Im unsure what im doing wrong. Ash.

Asheh    354

##### Share on other sites
lefthandman    138
Perhaps you could try several smaller "god rays" because the code you posted seems to be dead on with the article (unless I'm missing something). You could even try combining rays to make a ray that big. And if you want to make it look extra "god-like" increase the decay, and increase the illumination so that it fades out even more and has a more visible starting place.

##### Share on other sites
ankharlyn    122
I'm attempting to implement this for work right now as well, and I have the *exact* same results as Asheh. It seems to me that it's just a bug of some sort, since the results are definitely incorrect (regardless of tweaking the other parameters, it shouldn't be rendering a single solid shaft like that, but instead should spread them in a radial fashion).

The GPU Gems 3 article mentions rendering the quad for this shader using an ortho projection, though I honestly can't see how that would make a difference, since the shader uses the texture coordinates of the pixel, not the vertex positions at all.

@Asheh, if you'd like, perhaps we could hook up in IM and attempt to help eachother fix this bug. Just PM me if you want to try that :)

##### Share on other sites
ankharlyn    122
(I'm making a separate post since it's been a bit since my last, and the content is different.)

So, I managed to get the effect working. The problem that I had (and you're most likely having Asheh) is that the range for the texture coordinates and screen space light position in the fragment/pixel shader is different between OGL/DX. My issue was caused by having the sun position in the -1 to 1 range, and the texture coords in the 0 to 1 range.

To get more specific information about what's going wrong in your case, go ahead and just use the backbuffer for the occluded texture (instead of writing occluders as black, just use the normal backbuffer). As you move your camera around you'll be able to see where the center of the blur actually is in screen space, and more easily pinpoint what's going wrong.

##### Share on other sites
nicmenz    169
hello,

I implemented this effect three weeks ago, just have a look at

Volumetric Lighting with XNA

Just send me an PM with your email and I send you the code or any explanations.

greetings,
nicolas

[Edited by - nicmenz on May 6, 2009 4:43:31 AM]

##### Share on other sites
phresnel    953
Quote:
 Original post by nicmenz******@**********.***********.**

Better not post such on public fora.

##### Share on other sites
nicmenz    169
thanks changed :-)

##### Share on other sites
Krokhin    218
Quote:
 Original post by AshehI have been trying to impliment god rays from gpu gems 3 i have the pipeline setup where each light is rendered as a sphere (circle) in white then the geometry in black.Im unsure what im doing wrong.

Why you think so? It works pretty nice.
demo -volume.zip file(press F1 for help)
This demo shows "true" volumetric light,i.e god rays is visible when the light behind you.

##### Share on other sites
nicmenz    169
krokhin running your demo seems to be corrupted on my computer. I run a 8800 gtx, I guess you have an ATI card? Neither do I see volumetric lighting.

What technique do you use? I currently (desperately) try to perform a ray casting from the viewing perspective, but it doesn't work :-(

##### Share on other sites
Krokhin    218
I render depth and colors from transparent objects in cubemaps from light position, and draw spheres with blend around light using this info.Technique based at HLSL shaders,version 1.1 .This demo itself is pretty old (was writen ~4 years ago) and some bugs can take place of couse.