Development Log 5 - Revisiting Terrain Generation with Meshes


Implementation Of Terrain

 

Figure 1: Screenshot of Inspector at current projects iteration - current input parameters for terrain. Added exposed properties for mesh colour, a smoothing Boolean and a material texture 

 

This week terrain generation was revisited. The goals heading into the project this week were; allow the mesh to update live, to keep the height values consistent across re-generation of the mesh, apply a texture across the mesh and apply smoothing to the mesh. 

 

Figure 2: Coroutine implemented for checking input values changed and allowing mesh regeneration. 

 

Live Updating the Mesh

The single script in the project so far, TerrainGenerator.cs, was updated to allow live updating of the Mesh, a Coroutine was implemented which checks for any changes in input value, and if detected, the mesh is generated. See Figure 2. 


Consistent Height Value on Regeneration of Mesh

Next, an attempt was made to keep a memory of the heights of the vertices when generating new grids with different X and Z scales into an 2D array and reapplying the heights upon regeneration of the mesh.  

The result failed as when reducing the X and Z scales, the stored height values did not correctly apply to the vertices new position in a newly generated grid - for example, from generating a 5 x 4 grid, when regenerated as a 4 x 4 grid, the vertices from the original 5th X axis become out-of-range of the new grid, leading to out-of-range exception errors. 

Whilst this could be solved through ignoring vertices outside the new grid size, this would fail to solve the other error due to a new grid size the saved height values no longer apply to the respective point on the regenerated grid. 

 

Figure 3: Inconsistent height map values on regeneration. 

 

As noted in conclusion of Development Log 2, Perlin Noise has been highlighted as the ideal method of choice for developing terrain which would solve the issue encountered in consistent height map values of the mesh.  

The Perlin Noise method used within Unity, Mathf.PerlinNoise(), takes values from a sample of Perlin Noise. Any set coordinates on this will always return the same value. 1

The project would not require storage of any height value data in memory due to the noise sample being consistent with any given coordinates. 


Applying Texture

A texture was applied to the Mesh to attempt to provide realism in the terrain generated, but this approach failed as the texture repeated to often and the texture stretched when height values of the mesh increased. This implementation failed and should require a new approach. (See Figure 3). 


Smoothing Function

The smoothness function sought to provide smoothing to each vertex value due to the mesh being generated having sharp changes in neighbouring vertex height values – this can be attributed to the randomness in the height values being provided from the random selection between 0 and maximum allowed height. 

 

Figure 4: Smoothness demonstration - before and after. 

 

The smoothing function creates a duplicate array for each vertex in the mesh gird. For each vertex in the mesh and the respective vertex’s neighbouring vertices, the height values added together, averaged out and then applied to the new duplicate array.  

Once all the vertices have been looped through and averaged the new arrays values are applied to the original mesh vertices. This produced the difference seen in Figure 3. 


References

  1. Unity (n.d.) Mathf.PerlinNoise. Unity Documentation. Available at: https://docs.unity3d.com/ScriptReference/Mathf.PerlinNoise.html (Accessed: 7 December 2024)

Previous
Previous

Development Log 6 - Perlin Noise and Implementation

Next
Next

Development Blog 4 - Appendix A - Procedural Storytelling and Defining Good Procedural Content