God rays Shader problem

someone ported god ray to monogame ?

Thanks.

Yes, for a 2D game it is simple enough and easy to port.
To be used in a 3D game, it needs more work: models have to be drawn black, the “sun” position have to be projected into screenspace, the naive method would require 2 geometry draw passes to achieve the final image, which can be too expensive with many models.
The 3D version:
https://pbs.twimg.com/media/C5baHY8WcAAoCei.jpg:large

Yes it’s for a 2D game … you have converted source code for monogame ?

I try to port God Ray sample from XNA To MonoGame OpenGL, but that doesn’t work …
I think it’s a shader problem …
I post project on GitHub here

If someone can help me :slight_smile:

To elaborate, screen space sun shafts are imho kinda easy for both 2D and 3D, you generally need projection from 3D space to screen anyway (I mean, you are rendering objects, we are talking three multiplication of three matrices here and some screen data).

Issue comes in if you want to do it right way which means rendering screenshaft even if you are not looking at source, great examples there are GTA V and Lords of Fallen, which actually made pretty good presentation on that topic:

I know that´s not what you want, I just want that this technique is more spread and we are slowly moving in direction of removing old school screen space light shafts where it makes sense.

But, so I am not here just to preach…
from your git:
float2 TexCoord = texCoord - halfPixel;
DeltaTexCoord = DeltaTexCoord ;

You shouldnt need halfPixel correction here, what you need tho is position of source, thus:

float4 PixelShaderFunction(float4 position : SV_Position, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
    // Calculate vector from pixel to light source in screen space.  
    float2 deltaTexCoord = (texCoord - ScreenLightPos.xy);
    // Divide by number of samples and scale by control factor.  
    deltaTexCoord *= 1.0f / NUM_SAMPLES * Density;
    // Store initial sample.  
    float3 base = tex2D(TextureSampler, texCoord);
    // Set up illumination decay factor.  
    float illuminationDecay = 1.0f;
    float3 sample;
    // Evaluate summation from Equation 3 NUM_SAMPLES iterations.  
    for (int i = 0; i < NUM_SAMPLES; i++)
    {
        // Step sample location along ray.  
        texCoord -= deltaTexCoord;
        // Retrieve sample at new location.  
        sample = tex2D(TextureSampler, texCoord).rgb;
        // Apply sample attenuation scale/decay factors.  
        sample *= illuminationDecay * Weight;
        // Accumulate combined color.  
        base += sample;
        // Update exponential decay factor.  
        illuminationDecay *= Decay;
        
    }
    base = clamp(base, 0, Max); //You dont need this if you dont want to clamp max intensity, it can be useful, tho I recommend HDR tone mapping as well if you are using bloom or LS when composing your postprocessing in last step
    return float4(base * Exposure, 1);
}
2 Likes

Oh and also one more important thing: You dont need second pass, just render occlusion mask for objects you want use for LS occlusion into separate render target, you can open several RTs at same time, just modify shader you are using to render your geometry. Not to mention you often want to render your own Depth, or world position, in most cases you can extract occlusion for LS from those.

Awesome presentation! Thanks for linking :slight_smile:

This is already what i do, by ‘pass’ i mean i fill the rendertargets with the occluders (done at the same time s the deferred stuff) and another shader mixes them to render the final godrays.
Can you read from an rt or stencil while filling a 2nd one?

Well but in that case you are not rendering geometry twice, it will be still same amount of draw calls per object.

I was talking about 2 geometry passes if the “naive” approach as depicted here was used : http://xnauk-randomchaosblogarchive.blogspot.fr/2012/10/2d-crepuscular-god-rays.html

oh, I guess I misunderstood, sorry about that, generally checking community very late at night.

No problem, sometimes it’s my turn too, but very early at dawn :confounded:

@procfxgen The issue is this one: https://github.com/MonoGame/MonoGame/issues/5261
I sent a PR with a workaround :slight_smile:

Thanks to Jjagg this is fixed now…

1 Like

Hi,
that’s a bit off topic - but I tried to port the 3D sample from random chaos. crepuscular-god-rays-and-web-ui-sample
and put my result to GitHub git:

Well, finally I managed to compile it but the result is just a single colored screen -(the colors are somehow changing at least when moving the cam… :slight_smile: )

As I’m just a beginner I would really appreciate any help!

Cheers, Stephan

Well,

meanwhile I managed to port the LightShafts project from Nicolas Menzel to monogame! Hooray! :smiley:
LightShafts Tutorial 2

I put it on gitHub: LightShafts-Monogame

I think I will try to make a short description here the next days - I really had a hard time to find a good example in the web - I found lots of, BUT all in xna3/4 and no way for me to get it running in monogame…
So I hope this might be helpful for someone else desperate like me… :smiley:

Cheers, Stephan

1 Like