Simple shadow mapping example

Here’s a (very) simple shadow mapping example that might get you started. Ask here if you have questions and I’ll do my best to answer them. I’ve annotated the code a little bit, but my intensions are to improve it more at some later time.

Code: https://github.com/jhonnyking/my-public-defold-examples/tree/master/shadow-mapping
HTML5 build: https://jhonnyking.github.io/my-public-defold-examples/shadow-mapping/

25 Likes

When Defold editor can be a 3D editor?

2 Likes

Don’t you think that you (as Defold engeneer) should use in examples render.set_render_target (introduced in 1.2.139) instead of old and deprecated enable/disable_render_target?

3 Likes

Wow, the shadow is even casted on the other object :smiley: Do this method allows to cast self shadows? On the object itself?

2 Likes

Yeah you are right, totally forgot about that change :slight_smile:

5 Likes

Yeah, it does self-shadow, look at the shadow side of the sphere to see the effect. It looks a “bit” strange now due to the bias value, but it’s a common issue with shadow mapping. There’s fixes and improvements to add that will make it better :slight_smile:

4 Likes

Implemented cascaded shadow maps this weekend, I’ll clean up the code and put the source online later this week :slight_smile:

15 Likes

Did this ever go live?

2 Likes

I’m studying your original example now and I have some questions.

Is it cheaply possible to improve the quality of shadows like this? Smoothing the shadow or something?

2021-03-07%2020_17_13-shadow-mapping-example

Percentage-closer filtering as mentioned here?

https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping

What is the best way to allow some objects to cast shadows but not to receive shadows? So in the below it would be nice looking to allow the voxel character to cast a shadow but to be shaded in its own way.

2021-03-07%2020_17_28-shadow-mapping-example 2021-03-07%2020_18_09-shadow-mapping-example

Would it be possible to check if an object is inside of a shadow (a point is “standing” on top of a shadow) to change its shading? So like from lit to more shaded. Some games do this. I can imagine how this is done but I don’t know how it would probably be done.

1 Like

What I’m thinking is 1. draw shadows to target 2. draw objects with shadow catching enabled 3. draw shadows 4. draw objects without shadow catching enabled.

One way is to have an object mask. So when you render depth for your shadow mask, you can lookup the object mask and see its shadowing ‘capabilities’, its very useful for other things like heat maps… etc…
This way you can enable/disable on a per object basis. Im assuming you are doing a projected shadow map pass for multiple shadow levels.

1 Like

Hm there should be a branch with the cascade code in the original repo, but I’ll check again

https://github.com/jhonnyking/my-public-defold-examples/tree/shadow-mapping-cascades There is!

Should that work? I tried the one of the examples (on Linux Debian 10) - shadow-mapping-cascades-example:

INFO:ENGINE: Engine service started on port 39857
INFO:ENGINE: Defold Engine 1.2.181 (ff5b94e)
INFO:ENGINE: Loading data from: build/default
INFO:ENGINE: Initialised sound device 'default'
DEBUG:SCRIPT: Listening for debugger on 0.0.0.0
DEBUG:SCRIPT: Debugger connected from 127.0.0.1
ERROR:GRAPHICS: gl error 36054: GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
dmengine: ../src/opengl/graphics_opengl.cpp:2019: dmGraphics::HRenderTarget dmGraphics::OpenGLNewRenderTarget(dmGraphics::HContext, uint32_t, const dmGraphics::TextureCreationParams *, const dmGraphics::TextureParams *): Assertion `false' failed.
INFO:CRASH: Using libunwind.a

Hardware support issue? Not sure.
< edit > The shadow-mapping-example seems to work fine.

I didn’t test before yet and tested now, seeing similar issue on Windows 10

INFO:DLIB: Log server started on port 63486
INFO:ENGINE: Engine service started on port 63487
INFO:ENGINE: Defold Engine 1.2.181 (ff5b94e)
INFO:ENGINE: Loading data from: build/default
INFO:ENGINE: Initialised sound device 'default'
ERROR:GRAPHICS: gl error 36054: GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
Assertion failed: false, file ..\src\opengl\graphics_opengl.cpp, line 2019
INFO:CRASH: Successfully wrote Crashdump to file: C:\Users\subso\AppData\Roaming\Defold/_crash
ERROR:CRASH: CALL STACK:

 0 0x7FF6C4EF30CA _scwprintf <unknown>:0
 1 0x7FF6C4EF30CA _scwprintf <unknown>:0
 2 0x7FF6C4EF30CA _scwprintf <unknown>:0
 3 0x7FF6C4EF30CA _scwprintf <unknown>:0
 4 0x7FF6C4EF30CA _scwprintf <unknown>:0
 5 0x7FF6C4EF30CA _scwprintf <unknown>:0
 6 0x7FF6C4EF30CA _scwprintf <unknown>:0
 7 0x7FF6C4EF30CA _scwprintf <unknown>:0
 8 0x7FF6C4EF30CA _scwprintf <unknown>:0
 9 0x7FF6C4EF30CA _scwprintf <unknown>:0
10 0x7FF6C4EF30CA _scwprintf <unknown>:0
11 0x7FF6C4EF30CA _scwprintf <unknown>:0
12 0x7FF6C4EF30CA _scwprintf <unknown>:0
13 0x7FF6C4EF30CA _scwprintf <unknown>:0
14 0x7FF6C4EF30CA _scwprintf <unknown>:0
15 0x7FF6C4EF30CA _scwprintf <unknown>:0
16 0x7FFFAD527020 BaseThreadInitThunk <unknown>:0
17 0x7FFFAEDC2630 RtlUserThreadStart <unknown>:0
Lua Callstack:
  /render/cascades.lua:182: in function create
  /render/default.render_script:39: in function </render/default.render_script:12>

Tested on working Vulkan build and it seems to get a bit further

2021-04-17%2012_58_12-Window

I deleted mtx_view from one of them and mtx_worldview from another (both were unused in the actual respective vp) and then it would build although it does not look right.

Thanks for having a look at this. I’ll investigate further next week.

1 Like


I’ve tried to build this shadow mapping example, a bit modified in summer 2020 and also have an error.

INFO:ENGINE: Defold Engine 1.2.181 (ff5b94e)
INFO:ENGINE: Loading data from: build/default
INFO:ENGINE: Initialised sound device 'default'
ERROR:GRAPHICS: 0(45) : error C7011: implicit cast from "float" to "vec3"

WARNING:RESOURCE: Unable to create resource: /assets/materials/diffuse.fpc: FORMAT_ERROR
WARNING:RESOURCE: Unable to create resource: /assets/materials/diffuse.materialc: FORMAT_ERROR
INFO:CRASH: Successfully wrote Crashdump to file: C:\Users\isunt\AppData\Roaming\Defold/_crash
ERROR:CRASH: CALL STACK:

 0 0x7FF6C5DE0800 dmCrash::GenerateCallstack D:\a\defold\defold\engine\crash\src\backtrace_win32.cpp:142
 1 0x7FF6C5EBC4AC _seh_filter_exe minkernel\crts\ucrt\src\appcrt\misc\exception_filter.cpp:219
 2 0x7FF6C5F036BF `__scrt_common_main_seh'::`1'::filt$0 d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:304
 3 0x7FF6C5E4FBE0 __C_specific_handler d:\agent\_work\4\s\src\vctools\crt\vcruntime\src\eh\riscchandler.cpp:290
 4 0x7FF8B79F1F60 __chkstk <unknown>:0
 5 0x7FF8B79A1020 RtlRaiseException <unknown>:0
 6 0x7FF8B79F0B80 KiUserExceptionDispatcher <unknown>:0
 7 0x7FF6C5B0A810 dmGameSystem::ResMeshCreate D:\a\defold\defold\engine\gamesys\src\gamesys\resources\res_mesh.cpp:356
 8 0x7FF6C5ADE920 dmResource::DoGet D:\a\defold\defold\engine\resource\src\resource.cpp:1136
 9 0x7FF6C5AE05E0 dmResource::Get D:\a\defold\defold\engine\resource\src\resource.cpp:1234
10 0x7FF6C5AA5C60 dmGameObject::AcquireResources D:\a\defold\defold\engine\gameobject\src\gameobject\res_gameobject.cpp:64
11 0x7FF6C5AA62A0 dmGameObject::ResGameObjectCreate D:\a\defold\defold\engine\gameobject\src\gameobject\res_gameobject.cpp:161
12 0x7FF6C5ADE920 dmResource::DoGet D:\a\defold\defold\engine\resource\src\resource.cpp:1136
13 0x7FF6C5AE05E0 dmResource::Get D:\a\defold\defold\engine\resource\src\resource.cpp:1234
14 0x7FF6C5AA6A10 dmGameObject::AcquireResources D:\a\defold\defold\engine\gameobject\src\gameobject\res_collection.cpp:60
15 0x7FF6C5AA75E0 dmGameObject::ResCollectionCreate D:\a\defold\defold\engine\gameobject\src\gameobject\res_collection.cpp:259
16 0x7FF6C5ADE920 dmResource::DoGet D:\a\defold\defold\engine\resource\src\resource.cpp:1136
17 0x7FF6C5AE05E0 dmResource::Get D:\a\defold\defold\engine\resource\src\resource.cpp:1234
18 0x7FF6C5A8EEF0 dmEngine::Init D:\a\defold\defold\engine\engine\src\engine.cpp:1164
19 0x7FF6C5A935B0 dmEngineCreate D:\a\defold\defold\engine\engine\src\engine.cpp:1927
20 0x7FF6C5A93990 dmEngine::RunLoop D:\a\defold\defold\engine\engine\src\engine_loop.cpp:54
21 0x7FF6C5A8C600 engine_main D:\a\defold\defold\engine\engine\src\engine_main.cpp:127
22 0x7FF6C5E4A304 __scrt_common_main_seh d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
23 0x7FF8B7867020 BaseThreadInitThunk <unknown>:0
24 0x7FF8B79A2630 RtlUserThreadStart <unknown>:0

INFO:CRASH: Successfully wrote Crashdump to file: C:\Users\isunt\AppData\Roaming\Defold/_crash
ERROR:CRASH: CALL STACK:

 0 0x7FF6C5B0A810 dmGameSystem::ResMeshCreate D:\a\defold\defold\engine\gamesys\src\gamesys\resources\res_mesh.cpp:356
 1 0x7FF6C5ADE920 dmResource::DoGet D:\a\defold\defold\engine\resource\src\resource.cpp:1136
 2 0x7FF6C5AE05E0 dmResource::Get D:\a\defold\defold\engine\resource\src\resource.cpp:1234
 3 0x7FF6C5AA5C60 dmGameObject::AcquireResources D:\a\defold\defold\engine\gameobject\src\gameobject\res_gameobject.cpp:64
 4 0x7FF6C5AA62A0 dmGameObject::ResGameObjectCreate D:\a\defold\defold\engine\gameobject\src\gameobject\res_gameobject.cpp:161
 5 0x7FF6C5ADE920 dmResource::DoGet D:\a\defold\defold\engine\resource\src\resource.cpp:1136
 6 0x7FF6C5AE05E0 dmResource::Get D:\a\defold\defold\engine\resource\src\resource.cpp:1234
 7 0x7FF6C5AA6A10 dmGameObject::AcquireResources D:\a\defold\defold\engine\gameobject\src\gameobject\res_collection.cpp:60
 8 0x7FF6C5AA75E0 dmGameObject::ResCollectionCreate D:\a\defold\defold\engine\gameobject\src\gameobject\res_collection.cpp:259
 9 0x7FF6C5ADE920 dmResource::DoGet D:\a\defold\defold\engine\resource\src\resource.cpp:1136
10 0x7FF6C5AE05E0 dmResource::Get D:\a\defold\defold\engine\resource\src\resource.cpp:1234
11 0x7FF6C5A8EEF0 dmEngine::Init D:\a\defold\defold\engine\engine\src\engine.cpp:1164
12 0x7FF6C5A935B0 dmEngineCreate D:\a\defold\defold\engine\engine\src\engine.cpp:1927
13 0x7FF6C5A93990 dmEngine::RunLoop D:\a\defold\defold\engine\engine\src\engine_loop.cpp:54
14 0x7FF6C5A8C600 engine_main D:\a\defold\defold\engine\engine\src\engine_main.cpp:127
15 0x7FF6C5E4A304 __scrt_common_main_seh d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
16 0x7FF8B7867020 BaseThreadInitThunk <unknown>:0
17 0x7FF8B79A2630 RtlUserThreadStart <unknown>:0

INFO:CRASH: Successfully wrote MiniDump to file: C:\Users\isunt\AppData\Roaming\Defold/_crash.dmp

(1.2.181, Windows 10, GTX1060)

1 Like

Perhaps it’s related to OpenGL version? (I’m guessing here)
Which version did you get? (enable display.device_info in game.project)

Yeah something must have changed since I made that example, I can take a look during the week aswell!

3 Likes

Alright I think I’ve patched that example here:

(Note that I’ve moved/forked the examples repo into my private account from my king account)

4 Likes