Directional sun shadow optimization.

Directional sun shadow optimization.

When expanding the scene to be large enough for the expansive world that Scarlet6 will have, I realized that we need to limit the meshes that cast shadows from the sun.

In real life the sun’s rays expand in all directions, so it is not a directional light. However the sun is so far away from us that we can assume for our purposes that its rays are parallel, and so it can be modelled as a directional light that affects everything in the world.

Above: Positioned underneath the world, looking up. No shadows. The world is a vast expanse of hexagonal prism cells.

This means it casts shadows on everything in the world, and a shadow map is calculated for everything in the world.

If shadows were turned on in the image above, everything would be black, as the hexagon cells would block the sunlight (shadow themselves).

Above: Shadows turned on world has ~1000 tiles in total, though only ~40 are visible.

Above we see that since we have about 1000 tiles in total, the directional light has to draw 1000 tiles into its shadow map. Doing this every frame results in over 1000 draw calls every frame (see the statistics on the bottom right of those images).

We have a budget for maybe about 300 draw calls per frame, so this puts us way over the limit.

I prefer not to bake static lightmaps into the scene, because I want the world to be changing and dynamic (those hexagon cells will change height), so we need a faster way to draw the sun shadows.

Above: Bounding boxes around the visible tiles make it clear we can only see about 40 tiles at this camera position.

The solution is to only draw the meshes into the shadow map that are relevant for what the camera can see at this frame. This means the 40 tiles we see on the screen, plus an educated guess of some tiles that are offscreen that may be casting shadows onto our scene.

Implementing that solution is up next.

Bean

Bean
I'm Bean. From BeanstalkBlue.
https://beanstalk.blue

Robust character collision!

Player character now detects collision. Continue reading

Collision beginnings

Published on March 24, 2017

Collision beginnings

Published on March 24, 2017