• entries
    743
  • comments
    1924
  • views
    580063

Cube Shadow Mapping

Sign in to follow this  
Aardvajk

1702 views

screenshot.jpg

Managed to get cube shadow mapping working today. I'm not completely happy with the result but it was certainly easy enough to set up in the end, once I figured my way around the inevitable issues and artifacts. I'm using a 1024x1024 cube map in the screenshot above, but it looks kind of acceptable with a 512x512, even without PCF, although I do intend to tackle that eventually.

But now I want to have multiple lights supported. I can't imagine being able to hold a coherent level together if only one light at a time can cast shadows.

I've been reading around the various approaches to this. The result I'm looking for is for a shadowed pixel to not be blended multiple times. I'd rather all the shadowed pixels are a constant darkness.

So what I think I'm going to try is first render the scene to the backbuffer with only ambient lighting as if every pixel was shadowed, then for each light source:

  • Generate the cube map with the distances from the light source to pixel
  • Do an additive lighting pass over the backbuffer, but skip pixels that are in shadow
    My hope is that I'll then get nice, constant darkness shadows. Seems legit in theory anyway.

    Seems to be bewilderingly fast to generate the six faces of the cube map, so I think I can manage to render a few of these per frame. Three light sources should be sufficient to be able to link different rooms with different main lights together, then decorative, non-shadowing lights can be added in a single pass.

    That's the theory anyway. Will see how it works out.

    [EDIT Slightly later...]

    Just wanted to update here as I've just added PCF to this, and the results are remarkably good for a simple effort.

    pcf1.jpg

    Even right up close, the quality is far better than I expected:

    pcf2.jpg

    I'm just doing a 3x3 sample around the original vector, using an arbitrary offset in the pixel shader:

    #define am 0.1ffloat3 filter[] = { { -am, am, 0 }, { 0, am, 0 }, { am, am, 0 }, { -am, 0, 0 }, { 0, 0, 0 }, { am, 0, 0 }, { -am, -am, 0 }, { 0, -am, 0 }, { am, -am, 0 } };float shadowFac(float3 tolight, float3 normal){ float shadow = 0.0f; if(dot(tolight, normal) < 0) { for(int i = 0; i < 9; ++i) { float sampled = texCUBE(ds, tolight + filter).r; float actual = (length(tolight) - 0.5f) / 200; if(sampled < actual) shadow += 1.0f / 9.0f; } } return shadow;}I guess the value of am would have to be changed depending on the resolution of the cube map, but I'm pretty pleased with the effect there.

    Just hanging out around a camp-fire smile.png

    pcf3.jpg

    Thanks for stopping by.

    [EDIT]

    In response to IYP's comment below, I thought I should post another image showing how the PCF looks when the shadows are parallel to the world axes, as I think the lucky diagonal screenshot is giving a misleading impression of how good the PCF is here:

    pcf4.jpg

    I think this can be improved by using a random sampling kernel, but this can wait until I have multiple light sources working.
Sign in to follow this  


6 Comments


Recommended Comments

can you please share some more implementation details I don't seem to do any thing even a little close to your PCF implementation.

Share this comment


Link to comment

can you please share some more implementation details I don't seem to do any thing even a little close to your PCF implementation.


I can do, but the PCF bit is already in the entry there. If you have shadow mapping already set up with a shadowing term from 0 - 1, the only bit that is relevant to the PCF is the snippet I shared there.

The PCF doesn't look quite as good on shadows that are parallel to the world axes, maybe it just looks good on that particular angle in the close-up screenshot. I've just added another screenshot showing the PCF when parallel to the world axes, far less impressive.

What part of the implementation was it you wanted to know more about?

Share this comment


Link to comment

thanks for respond, my pcf also looks like the final image you added so I guess the problem is from the way shadows are.

 

Again thanks for respond

Share this comment


Link to comment

thanks for respond, my pcf also looks like the final image you added so I guess the problem is from the way shadows are.
 
Again thanks for respond


You're welcome. If I do investigate ways to improve this with random sampling, I'll be sure to post something about it in here. Thanks for showing an interest.

Share this comment


Link to comment

Holy crap! You're working on a game, not just a bytecode VM! biggrin.png


Rarely working on just one project at a time, you know how it goes smile.png

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now