Shadow mapping!
It's a technic for adding shadows to a game.
๐Ÿงต
Shadow maps save info about which areas of the scene are in light and which are in shadow. This data is saved into a texture called a shadow map.
If the scene and lights don't change we can pre bake all this info and use the same map for every frame! Sometimes this is called a light map and extra info can get packed in like light color.
If we have moving characters or lights we need to rebuild the map
๐Ÿ‘ every ๐Ÿ‘ single ๐Ÿ‘ frame ๐Ÿ‘
So how do we work out which area is in shadow?
We reuse the renderer and let the GPU do its thing.
Reset the shadow map to be full shadow. Put a camera at the light source and render everything we 'see' white. Anything the light can't see is shadow.
If we have more lights we need to repeat the process and combine the shadow maps.
16 lights, 16 extra renders.
Now we have our shadow map we can pump it into the shaders and we can darken all the shadow areas.
Awesome! ๐Ÿ˜Ž
Now we have a problem ๐Ÿ˜ฌ
The areas close to the camera need to be high resolution so we don't see a jagged pixel boundary between light and shadow.
When we increase the resolution our shadow map renders are taking too long and using up all the GPU memory ๐Ÿ˜ฑ
One solution is to split the shadow maps into chunks with different resolutions. High res close to the camera and low res when far from the camera. These are called cascading shadow maps.
Fantastic, we are putting our shadow pixel budget to good use. Spending it mostly up front, close to the camera. There is just one issue left....
At the boundary between map cascades we have a small artifact. We can make it less noticeable by pushing the boundary further away from the camera, but if we do that we waste precious pixels and make the close up shadows look worse.
Best thing to do is sit down and tweak the distance by hand until you find the trade-off you can live with.
Remember 99.995% of your players are not looking at your game in slow motion on 400% zoom. Close enough is good enough.
Notes:
If you are using point lights you often need to do even more render passes to cover all the directions the light can shine.
This is describing a foward render pipeline, deferred pipelines are a while other can of worms ๐Ÿ˜
You can follow @KeirRice.
Tip: mention @twtextapp on a Twitter thread with the keyword โ€œunrollโ€ to get a link to it.

Latest Threads Unrolled: