Terrain Generation Tool

I’ve spent the last week or so on and off developing a simple terrain generation tool for Unity. If you’ve ever used the built-in terrain component then you’ll understand the frustrations I had, particularly when attempting something as basic as importing a heightmap. Although a handful of terrain tools for Unity already exist, I still favour the terrain component in its vanilla state and decided that extending its functionality would be more appropriate for my needs. I feel as though it’s necessary to point out that this tool isn’t intended to be a complete terrain generation package, but rather a simple set of tools to quickly generate terrain and paint it before editing manually.

The script is what could be considered the sum total of my learning experiences regarding Unity’s terrain component over the course of the week. Although the included documentation covered most of what I needed to know, I took the time to follow a few tutorials on the subject and to see what was possible with a relatively small amount of work. The first step was getting terrain generation working, this turned out to be far easier than expected as Unity already had a function for generating perlin noise. I later added the ability to generate fractal noise by specifying the number of octaves and support for importing a heightmap from a texture.

I soon discovered that it would be necessary to provide a way of smoothing the terrain after the addition of heightmap support, to remove the blocky remnants that occur when mapping a heightmap texture comprised of pixels to a different sized heightmap array. I found a rather simple yet elegant solution after reading a tutorial detailing such a method. By averaging each point on the terrain with the average of its surrounding points, it becomes possible to perform a smoothing pass that can be applied over and over until a desired level of smoothness is achieved. Here is a link to the tutorial.

The second step was to provide ways for quickly painting large areas of the terrain, based on topographical characteristics. After reading some suggestions from a tutorial on procedural splatmapping in Unity, I implemented rules for painting the terrain based on surface normal, steepness and height. It took me a while to expose this functionality in a way that was simple, yet powerful enough in terms of flexibility. In the end I achieved this by adding support for enabling multiple rules in unison, dramatically increasing the level of precision and control over how the terrain is painted without any additional overhead. Here is a link to the tutorial.

The functionality of this tool is exposed through Unity’s inspector view, thankfully the process of writing a custom inspector GUI is clearly outlined in Unity’s documentation. I did go through a number of different layouts before settling on what is implemented now, in an attempt to make the controls as least confusing to use. The inspector layout is divided into two sections, terrain generation and painting or texturing. The texturing controls are only available if textures have been added to the terrain and if enabled, can be hidden when not in use. Below is a screenshot of the inspector view with texturing controls enabled, buttons to apply the heightmap or weightmap are hidden until a texture is selected.




Originally the plan was to support procedural placement of trees and vegetation, both of which ended up as desirables that I'll most likely implement once I have the immediate need for such tools. Two additional features that weren’t originally part of the scope in relation to the texturing side of things was support for weightmaps and alphamap smoothing, this was relatively straightforward with implementations based on their terrain generation counterparts. I felt as though having full heightmap and weightmap support vastly extends the versatility of the tool by means of supporting dedicated third-party tools. Below is a screenshot of a scene that took around ten minutes to build, using only the standard Unity assets and the tool herein to generate and paint the terrain. Download it here.



Comments

Popular Posts