So I’ve been working on a simple procedural terrain script in the Unity engine. It currently defines a 2D array of floats, then assigns random values within a certain range. I then call a function that adds rows and collumns between the existing data points, generating midpoints. These midpoints are then given a new value based on the average of the two points on either side with an added noise value.
The next step was to procedurally texture the terrain. I wanted something that would blend textures smoothly, so the grass would become less dense as it progressed up the mountains. I accomplished this by writing my own shader. It currently takes two textures, and combines them based on the alpha values in a blend-map computed based on the altitude of the terrain.
The shader uses a single pass to combine all of the textures together and applies the final texture to the terrain.
The shader also uses lightmaps combined with built in pixel and vertex lights in order to add pre-computed shadows that can also be tinted using ambient light and point lights. The lightmaps are made using a raycast system, where a cast vector is specified, and is projected in a number of itterations along the surface of the terrain. This then writes to the texture on the UV coordinate that is mapped to the hit location. This way, any point that the ray does not hit has a default color value ( Black ), and wherever a ray collides, the value is set to white. This allows simple shadow computation based the slope of the mountain. The lightmap is then blurred to remove any sharp edges. This produces a simple black and white texture that can be multiplied over any other base texture to create shadows.
This system allows the user to create a random terrain of a specified size and resolution, with an adjustable lightmap accuracy, and tiling textures that are still shadowed correctly.
Here are the links to a live demo, and a download link for the asset package.
I will post a follow up article about custom grass shaders soon.