Development Blog 2 - Randomness and Mesh Generation
Random Number Generation and Noise
This week research began with reading about Random Number Generation (RNG) in Unity, and how to utilize predictable randomness.
Rowe (2020) highlighted the use of RNG in Unity, which has its own Random Class to be called for generating random number, in addition to the existing to .NET’s Random class.1 2
Rowe (2020) advises that, for best practices, it is best to avoid Unity’s Random class system and instead use .NET’s Random class. The reasoning given is that .NET’s Random class provide superior predictable randomness in allowing for a seed to be passed into the generator giving reproducible outcomes. 3
As noted in the documentation for .Net’s Random Class, the random number generator it uses is a “modified version of Donald E. Knuth's subtractive random number generator algorithm”. 4
For the scope and scale of the project, this is more than suitable method for generating Random Numbers as it does not envision going beyond producing a game-centric product, with no planning for secure cryptographical data for example.
This informs the structure of the project going forward regarding the use of RNG – any introduction of randomness can take integer parameters from a “master” seed which will ensure the resulting randomness can be reproduced.
Mesh Generation with Random Heights
In implementing the goals of this project, a simple grid-based mesh generator was produced within the Unity Engine.* This was generated by creating a grid of vertices, with X and Z values for each vertex calculated by taking the grid axis count and multiplying by a spacing value. Importantly, the Y values were generated via a random range from 0, being no height to the mesh, to the given “Height Range” limit via the Unity Inspector.
*Please note, that in Unity’s Editor 3D environments, Y represents vertical space, with X and Z representing horizontal space.
This provided a basic implementation of procedural terrain with the heights of the mesh having variable values, however further building up on these principles would be required for increasing the realism and complexity of the mesh to allow it to visually appear as realistic terrain.
Unity’s documentation proved invaluable for providing the tools and knowledge needed in rendering a mesh with the scene space and would be required reading for anyone wishing to produce meshes within unity via scripts. They cover the steps required: 5 6 7
Generating an array of Vertices.
Generating an array of integers representing index of the vertices for drawing Triangles.
Generating Normals for allowing correct shading and lighting – do make use of Unity’s Mesh.RecalculateNormals() method which can calculate this.
Project Summary to Date
The introduction of generating mesh to simulate terrains is of interest going forward for this project; producing different yet reproducible environments lends well into one of the key goals of procedural generated content – providing for greater player engagement and replayability. This topic is to be further explored, with the goal of generating meshes with coherent values that reproduce a more realistic simulation of terrain. This can be achieved with grid-based noise generation, such as Perlin Noise – to be explored.
References
Unity, n.d. Random Class (Unity API). [online] Available at: https://docs.unity3d.com/ScriptReference/Random.html [Accessed 5 December 2024]
Microsoft, 2024. System.Random class (C#). [online] Available at: https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-random [Accessed 5 December 2024]
Rowe, E., 2020. Generating Predictable Random Numbers in Unity. Red Blue Games Blog. [online] Available at: https://blog.redbluegames.com/generating-predictable-random-numbers-in-unity-c97b7c4895ec [Accessed 5 December 2024]
Microsoft, 2024. System.Random class (C#). [online] Available at: https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-random [Accessed 5 December 2024]
Unity Technologies (n.d.) Access meshes via the Mesh API. Available at: https://docs.unity3d.com/Manual/UsingtheMeshClass.html (Accessed: 7 December 2024).
Unity Technologies (n.d.) Mesh. Available at: https://docs.unity3d.com/ScriptReference/Mesh.html (Accessed: 7 December 2024).
Unity Technologies (n.d.) Create a quad mesh via script. Available at: https://docs.unity3d.com/Manual/Example-CreatingaBillboardPlane.html (Accessed: 7 December 2024).