Simple shadow mapping example

Thanks! Some of your suggestions are not that time consuming to make, so I’ll see if I can make some small examples :slight_smile:

12 Likes

After seeing these lovely examples I’ve been determined to get shadows in my game.

The models I’m using are much bigger than the ones in the Shadow Mapping Smooth example, so the shadows don’t work as expected. How can the example be adjusted to work with large models?

Also, and this is a long shot, is there a way to avoid self-cast shadows?

An example using the same models scaled x1000: shadow-mapping-smooth-shadows.zip (4.4 MB)

2 Likes

You need to tweak the projection matrix values in the controller script:

local proj_w = 1000
local proj_h = 1000
local proj   = vmath.matrix4_orthographic(-proj_w/2, proj_w/2, -proj_h/2, proj_h/2, 1.0, 1000)

And also you will probably want to tweak the depth texture size as well, in the render script.
But with those values you will have depth precision issues afaik, I’d try to scale down your scene or see if you can tweak values enough to get it working but with that scale its gonna be hard unless you use shadow cascades.

4 Likes

I was actually considering scaling all the models in the game down.

Great, will try this first. Thanks!

What are shadow cascades? Maybe that’s the way to go?

I’d try the other stuff first, shadow cascades are more complicated and performance heavy. It’s basically splitting the shadows up in several slices (shadowmaps) where you use a specific slice at a specific render distance, so you can get uniform resolution regardless of distance to the point being rendered. I think I have a working example of that on my GH aswell, but it’s more complicated

3 Likes

I was hoping to scale the models dynamically, but now I bumped into a separate issue:

Maybe these shadows will have to wait a bit. :slight_smile:

On a different note, is there a way to set the position of the light source?

Look in the controller script for this, and then go backwards from there :slight_smile:

msg.post("@render:", “set_light_transform”, { transform = self.light_transform, direction = self.light_vector })

1 Like

I’d like the light to follow a game object, but I’m cursing my inability to understand how I would achieve this in a sea of matrixes, and quaternions. So much to learn!

Update: I’m trying to cast a shadow from a car onto the track. This works great. The issue I’m having now is the artefacts that show from the self-cast shadow on the terrain. Is there a way to disable self-cast shadows?

5 Likes

Do not render road to shadow map maybe?

1 Like

That artifact is called “shadow acne”, try tweaking the bias value in the shadow shader, or as ivan says dont render it to the shadow map.

Thanks people!

I’ve been able to remove most of the acne by tweaking the bias and also by making sure all the model surfaces have “flat shading” set in Blender (this made a big difference).

How would I remove the road surface from the shadow map? Wouldn’t that also remove the shadow from the car?

You can set a special material for road and create a predicate (in render script) for it.

1 Like

Oh I see. Yeah, that would work, thanks!

Edit: Sorry, I’m still not with you. :thinking: The road surface is the only model that needs to receive shadows, so it can show the shadow of the car.

To be able to receive shadows from the car, doesn’t that mean it also automatically receives shadows from itself?

Mesh will receive shadow from shadow map, if there is no road in shadow map - it will receive shadows only from other meshes, right?
You don’t need road in shadow map, but you need to apply shadow map while rendering road model.

3 Likes

Yes, that makes sense. Really neat. I’m going to test this and report back.

Edit: It works really well! For some reason the shadows were inverted, so the numbers had to be tweaked, but really happy with the result.

Update: @Pkeod Asked if I could share this, and I think that’s a good idea. Maybe someone can help improve it. One thing I haven’t been able to figure out is how to cast the shadow on the walls.

Here is a minimal project with just the car , the track and the shadow stuff:
Shadow test.zip (130.4 KB)

12 Likes

That looks great! Glad to see this example is of use to someone :slight_smile:

3 Likes

Wow! Awesome!

3 Likes

The example above has been updated with a minimal example, in case someone wants to quickly try it out for themselves.

8 Likes

Where? :slight_smile: