Procedural Meshes: Generating Gemstones Part 1

So two years ago as a student researching for his thesis, I took part in the first procjam. Organised by Michael Cook, this is a games jam that focuses on procedural content, whether that be games, art, text, tools, anything that can make something. Last year I decided to go a bit basic, write some pre-existing noise and maze algorithms for the Haxe Programming Language, which I eventually tweaked and published on Haxelib and Github as MAN-Haxe.

Last year, I decided that for my current project, I was going to do something relevant, and this time use no pre-existing algorithms, this is where the Gemstone Generator comes in. I have images of the meshes below that show the progress from early successful generations to the final most generation test before the UI layout was cleaned up and the demo was uploaded. The generative process is now being used in Gemstone Keeper, albeit with a different rendering process considering I’m turning Unity’s Procedural Meshes into SFML meshes.


I originally discussed this a little bit with some other game developers at the procjam conference, as I figured that I could use an algorithm to procedurally generate creative looking gemstones at the time. The algorithm I wrote (which I go into detail on the page) is loosely based on algorithms to generate snowflakes, which usually have six lines of symmetry. Essentially, the main two parameters are an array of two-dimensional coordinates and a number to represent the amount of symmetry lines. The script will then create vertices at each angle which matches the amount per symmetry (e.g. two lines of symmetry = four edges, including the original). Additional parameters are used to modify the vertices to suite the developers needs, add a bit of variance to the vertices and even to make some slightly more interesting gemstone shapes.

It was nice to use Unity this time around, seeing how much of it has improved since the last time I used it. Creating Meshes procedurally is surprisingly easy, although the issues between unique and shared vertices did hinder me a bit because of my academic background with Direct3D and OpenGL (using indices to reduce the amount of data). Plus I didn’t realise until hours after writing my normal calculation that Unity provides a function that calculates the normals for you.

meshData = GetComponent<MeshFilter>().mesh;
meshData.Clear();                                             //Clear existing mesh data
meshData.vertices =     vertices.ToArray();        //Set Vertex Coordinates
meshData.uv =           uv.ToArray();                //Set Texture Coordinates
meshData.triangles =    triangles.ToArray();    //Set Polygon Indicies (Clockwise)
meshData.RecalculateNormals();                     //Calculates Vertex Normals
meshData.RecalculateBounds();                        //Calculate Collision Bounderies
meshData.Optimize();                                        //Makes Geometry Display Faster

User interface could have been done better, especially for inputting the coordinates. I was originally going to do a scroll list with the ability to add and remove elements at runtime using one of Unity’s live sessions as a guideline, but I gave up after an hour because while adding elements was easy to implement, removing them was pretty much impossible because of Unity’s event system behaving differently with prefabs.

What I will say though, is that Unity’s WebGL output is great. While it maybe because my project isn’t as complex as much bigger Unity projects (and there were minor snags with the UI), being able to build a game to WebGL and then uploading it straight to was very straightforward and the output is at least 98% similar to the output from the Unity Web Player.

I have created an page for the Unity script, so people are free to use it if they ever wanted procedural gemstones, or to look at the code and find ways of improving it. I originally submitted it to the Unity Asset Store, however it was declined. Despite that, I thought it was good enough to use in Gemstone Keeper, only problem was that was 2D and C++, but that’ll be for the next part.

2 thoughts on “Procedural Meshes: Generating Gemstones Part 1

  1. Pingback: Procedural Meshes: Generating Gemstones Part 2 | GAMEPOPPER

  2. Pingback: New Years Update | GAMEPOPPER

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s