Terrain Editor
During the second year at the game assembly we had a specialization course where we could pick a project of our own. At the time I was really interested in tools and was currently working on our custom engine for Blob Lobber so I went that direction and chose to create a terrain editor using a more bare bones version of our engine. The idea was to eventually incorporate the tool into the main engine to potentially use for the final project but that didn’t happen since we decided to use unreal engine 5 instead.
​Some stuff I did:
Marching cubes
The tool uses the marching cubes algorithm to create the terrain, I choose this technique over the more commonly used heightmap because I wanted to create a terrain system that supports more 3 dimensional terrain with caves and outcroppings and it seemed like a more interesting challenge.
Marching cubes is used for constructing a mesh based on a 3D grid of points that each contains a value, if the value is above a certain value the point is considered to be inside of the mesh. These points are then used to calculate how triangles of the mesh should be placed.
The way the tool works is by casting a ray towards the mesh and finding all points within a certain radius of the target area, and then manipulate the stored values based on which brush the user currently has, and then finally recalculating the triangles for the mesh with the new values. The brushes I had time to implement for the project was an "add" brush and an "remove" brush.
Chunk optimization
The largest piece of optimization I did was to split up the terrain into chunks that each holds a specified number of points. When manipulating the terrain, I first check which chunks overlaps the influence sphere of the brush and only perform the checks on the points in the chunks that have a possibility of being affected. This had an absolutely massive boost in performance. It could probably be taken even further if I continued with this project but for my purposes this was enough.
12*4*12 chunks with 32*32*32 points in each one
1 chunk with same total amount of points
Texture Painting
The tool also supports painting textures on the terrain. This works in a similar way to the manipulation of the terrain itself, each point has weight values for how much of each texture is affecting it which gets saved to each vertex as a float in the vertex color that then gets sampled and blended in the shader.
The biggest challenge here was the UVs for the terrain. I utilised a technique called triplanar projection which takes the vertexes world position in each of the three planes and uses it to sample from the texture three times (once for each cardinal direction), and then these textures are blended based on the normal of the current pixel. That means for example a pixel facing upwards will sample almost entirely based on the XZ-coordinate.
Triplanar projection also makes it very easy to have different textures used in different orientations, this is something I tested for fun but did not have time to integrate as an option in the tool.
Collision
The tool also supports creating collision for the terrain. Our engine had PhysX integration which supports baking mesh colliders which made it very simple to add.
Each chunk of the terrain remembers when it has been modified so that only the chunks which have updated data in them will rebake their collision.