geometry – Deriving shadow position from a 2D heightmap

You can associate a “shadow height” with each cell on your map. This is the elevation something would need to have above 0 / “sea level” in order to rise out of the shadow and into the light.

You can initialize the shadow height of all tiles on the left edge of your map to zero.

Then, working across your map from left to right, each tile can compute its shadow height using its neighbouring tile to the left:

this.shadowHeight = max(leftNeighbour.terrainHeight + leftNeighbour.tileHeight, 
                  ) - shadowSlope;

…where shadowSlope is a parameter you can tune to control the length of your shadows, representing the slope of the terminator between lit and shadowed regions. Low values behave like sunrise/sunset, where the light rays are almost parallel to the ground and shadows get super long. High values behave more like noon where the light rays are coming down almost vertically, making tiny shadows.

Any tile with shadowHeight > terrainHeight is in shadow.

If you want to simulate soft shadows, you can set the shadow darkness to be proportionate to shadowHeight - terrainHeight.

Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email