top of page

Blockchain Terrain Generation #6 - Custom Shader/Lighting

I discovered many problems using the default Unity lighting system. Firstly, by default, the way Unity calculates shadows can be fairly slow and is not very optimized for a project like mine. To calculate each shadow it is having to redraw the mesh which drops the performance.

One fix to this is to use a process called "Baking" where the light and shadows are calculated once, and the lightmap generated is used at runtime which is better than making calculations every frame. However, baking will not work well for my project because it is a sandbox game where each block can be removed, and you can place blocks anywhere.

Another problem with Unity's default lighting system is that the shadow's quality by default is quite low, which results in light seeping through the edges of the mesh. So in caves and anywhere where there is a shadow, it would be very offputting.

I decided to research Minecraft to see how they do their lighting.

I started by running a classic build of Minecraft, alpha version 0.0.23a_01. On here, I noticed that each face was either lit up, or in shade, but there was no in-between. However, I thought this would be a good place to start.

To do this, I had to learn about writing a custom shader for Unity which is something I haven't done before.

I discovered this video by b3agz that showed how to write custom shaders for a voxel-based game. And with this knowledge, I implemented a custom shader into my project.

When I created a custom shader, I then made an adjustment to my face generation code to search for any block above the face and would apply a shadow level if a block was found.

Doing this was a large improvement to the default Unity lighting and looked more like the alpha Minecraft build I researched, but I wanted the lighting and shadows to behave a little more real instead of having a flat shadow value if there was a block above the face.

I looked at a more recent version of Minecraft to look at how they achieve their lighting now.

In this version, 1.18.1, the shadows gradually become darker as there is less light from the sky available.

In this video, I found a great visual explanation of how this light in Minecraft is calculated.

Every sky block is worth a value of 15, and if there is a block anywhere above the block being calculated, its value will become the value of the highest neighbouring block 1.

I can implement this idea into my project, then adjust my face generation code to look for the facing air block and assign its light value to the value of the air block it's touching.

I implemented this into my code. I decided to make the change from light to dark very gradual which I think works great in my project.

4 views0 comments
Post: Blog2_Post
bottom of page