Simple shadow mapping example

Made a little demo with shadow mapping + fog + second point light:

Html5 demo

  • Press and hold left mouse button to camera rorate.
  • Press and hold right mouse button to change direction of light source.
  • Middle click to spawn a dice.

Thanks to @jhonny.goransson for a shadow mapping example, @Pkeod for smooth shadows example and @aglitchman for a fog.

22 Likes

Looks awesome. This kind of demo should be one of the builtin examples.

10 Likes

+1 for including this example.

It would be great to start chiming in more as a community to provide example projects.
Construct for example is including a lot of those recently. They are not big at all, focus on one mechanic, code is documented and have appealing graphics/sounds. Of course these are provided by a 3rd party but I think this is what the community of an open source project should contribute :slight_smile:

7 Likes

Yes, we definitely want more examples too! Polished games work as a great showcase for Defold, but small examples that new users can pick apart is what will win over new developers.

4 Likes

added source code here: https://github.com/Dragosha/defold-things

9 Likes

updated with Steve animation.

Thanks to this guide: Guide: Exporting 3d skinned animations for Defold and this https://github.com/yeqwep/Toufu-3D-project

17 Likes

Great stuff! I want to make more examples like this, simply because it’s fun :slight_smile: If there’s any requests for some small rendering examples I can maybe cook something up during the holidays.

13 Likes

Nice! A couple I think would be good general usage.

  • fresnel
  • Cubemap
  • Reflection probe
  • Depth of field camera effect
  • basic fog

other things I think would be great basic PBR rendering, basic emission map, basic height map examples. Anyway awesome work @jhonny.goransson thank you for these examples and Happy Holidays!

11 Likes

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?