Sponsored By

Antegods Code Update: Growing Procedural Plants

In this blog post, our programmer TJ will share his experiences in creating a tool to procedurally generate plants for the ever changing worlds of Antegods.

Peter de Jong, Blogger

April 4, 2017

6 Min Read
Game Developer logo in a gray background | Game Developer

With the crowdfunding campaign for Antegods on Fig.co ongoing, development of the game has been a little slow, but still we will try to keep you up to date on our development process with regular blog posts from our design, art and code team. This time, programmer TJ will share his experience in creating a tool to procedurally generate plants for the world of Antegods.

Introduction

Hi, I am the other Tom or TJ as they like to call me over here, one of the programmers working on Antegods. Procedural level generation adds to our dynamic, ever-changing environment and we want to give the same feeling to the plants that exist within the world of Antegods.

A plant has the following requirements:

  • Needs to be able to arc from the background towards the gameplay layer

  • Needs to be able to release energy

  • Should feel alive and natural

We came up with the idea to write a custom Unity editor that allows us to easily generate plant variations from plant components while still maintaining some level of control over the generation properties so we can decide what assets we want to use or discard. This is what it looks like in the Unity editor!

image

Procedural Meshes

A plant consists of four individual components namely a trunk, branch, leaf and flower. With those four components we can create a whole range of plant variations by modifying their size, position, rotation and interchanging plant components by for example, using the leaves of plant A with the trunk, branches and flower from plant B.

Unity’s mesh class exposes all the data we want to modify and makes it easy to add a couple of sub-meshes together. I picked the following example to show you how it’s done.


private void AddSubMeshToMesh(Mesh submesh, Mesh mesh) 
{ 
   mesh.subMeshCount++; 

   int[] triangles = submesh.GetTriangles(0); 
   int[] indices = submesh.GetIndices(0); 

   for (int i = 0; i < indices.Length; i++) 
   { 
      indices[i]+= mesh.vertexCount; 
      triangles[i]+= mesh.vertexCount; 
   } 

   vertexList.AddRange(submesh.vertices); 
   mesh.vertices = vertexList.ToArray(); 

   normalList.AddRange(submesh.normals); 
   mesh.normals = normalList.ToArray(); 

   uvList.AddRange(submesh.uv); 
   mesh.uv = uvList.ToArray(); 

   if (submesh.colors.Length == 0) 
      submesh.colors = new Color[submesh.vertexCount]; 

   colorList.AddRange(submesh.colors); 
   mesh.colors = colorList.ToArray(); 
   mesh.SetTriangles(triangles, mesh.subMeshCount - 1); 
   mesh.SetIndices(indices, MeshTopology.Triangles, mesh.subMeshCount - 1); 
}

First we increase the sub-mesh count and get the indices and triangle indexes belonging to the sub-mesh. We offset those with the number of vertices that are already stored in the mesh. Next we add the sub-mesh vertices, normals, colors and UV coordinates to temporary lists and update the mesh accordingly. In case one of our sub-meshes doesn’t have any vertex colors, we make sure to allocate them because Unity expects the same amount of vertex colors as there are vertices in the mesh!

Mesh Generation

To create a new plant we start by generating the trunk by instantiating one of the stored .fbx models an artist created. To make the plant arc towards the gameplay layer so that the players can pick up the energy released from the flowers, we use Unity’s animation curves. The x-axis represents the y-position of the vertex to modify and the y-axis represents the displacement of that vertex over the z-axis.

imageimage

In the editor we can change the number of branches a plant needs to have. This brings me to the following problem. How can we actually attach a branch onto the already deformed trunk?

We solved this by shooting random rays from outside of the mesh to the center. When a ray hits the deformed mesh collider, we can look up the normal at the point of intersection and use that to orient our branch vertices alongside the normal. We do this by constructing a quaternion through Quaternion.LookRotation which creates a quaternion by specifying the look direction (our normal) and an upward vector. By multiplying the vertices in the sub-mesh with the quaternion, we get the transformed vertices and we can re-assign them to the sub-mesh.


Quaternion rotation = Quaternion.LookRotation(hitInfo.normal, Vector3.up); 
Vector3[] tempVertices = subMeshCopy.vertices; 

for (int i = 0; i &lt tempVertices.Length; i++) 
{ 
   tempVertices[i]= hitInfo.point - (rotation * tempVertices[i]); 
} 

subMeshCopy.vertices = tempVertices;

Now that we have a trunk with branches we need to have some leaves on those branches! To determine where the leaf needs to attach to the branch we temporarily store the outer endings of all branches. This is done by calculating the total width of the branch and adding it to the point of intersection alongside the trunk normal.

Finally the plant needs to have a flower so it can release the energy that can be picked up by the players. By storing the trunk end point we can attach the flower at the right position which completes the process of creating a new plant asset.

Mesh Rendering

To make the plant feel alive we wanted to have some sort of animation going so the plant would sway as if wind is blowing against it. To achieve this we use a shader on our materials that displace the vertices in a specified direction over time. We only have one problem here, all branches and leaves are combined into a single mesh which means that all leaves currently move in a similar fashion. Something to improve on for the next iteration!

To render the procedural plant within the editor we can use Unity’s PreviewRenderUtility that happens to take a mesh as parameter. This way we can get a pretty good idea how the plant will look like once it is in-game. Pretty cool!

To conclude I think the plant generator is shaping up pretty nicely and I am excited to see how it looks like once the procedural plants are able to release energy. Here is the tool in action that shows the current workflow with one trunk, branch, leaf and flower.

imageimage

Social media

If you enjoyed this blog post, we hope you take the time to share it with your friends. To follow our development updates, please check us out on TumblrTwitter or Facebook. Or subscribe to our newsletter. Whatever is your taste in social media!

Antegods is crowdfunding at Fig.co at the moment! Don’t miss out on all the exclusive goodies we have for backers!

Antegods is supported by the Dutch Cultural Media Fund, Cultural Industries Fund NL and the MEDIA Programme of the European Union.

image

image

Read more about:

Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like