Terrain from a Convex Hull and Triplanar Mapping

As I pointed out last month, I’m putting my personal projects on a hiatus for a while. Instead, I’m going to talk a bit about interesting game dev and graphics stuff that’s come up at work. Naturally I can’t blab too many details about our internal projects at PEAK6, but I do want to point out some of the publicly available technologies and/or resources.

The project I’m currently working on has involved a witch’s brew of proc gen techniques. I want to focus on two in particular: convex hulls, and triplanar mapping.

In this project I have a bunch of objects spawning on a plane, and I wanted to generate the terrain underneath them. That means I needed geometry that surrounds the entire collection of points, which is exactly what convex hulls do. Here’s one explanation of the concept: as illustrated, you start with a cloud of points, and then the algorithm calculates a shape which fits around that cloud.

Fortunately for me, there’s already a pretty good library for generating convex hulls in C#, called MIConvexHull. Now that library isn’t designed for Unity specifically, but I found someone had already packaged it for use in Unity.

Note however that this library won’t create a visible mesh for you. It’ll calculate where to put all the vertices and triangles (referred to in this library as “simplexes”) for the hull, but you still need to generate the actual mesh. I explained how to do that in Unity in my tutorial about generating mazes.

So that took care of the shape. However I still needed to texture my terrain. That could have been tricky, since the convex hull library wasn’t calculating UV coordinates, only the vertex positions and triangle indices. Because of that, I decided to take a UV-less approach to texturing.

If you’ve never heard of triplanar mapping before, allow me to introduce you to the concept. Basically, you project textures across the geometry along each of the 3 primary axes, resulting in stretch-free texturing in every direction. Obviously this approach doesn’t give you very detailed control of the texturing, so you wouldn’t want to do this on something like a character model. However this works great for procedurally generated terrain.

While there are some triplanar shaders on Unity’s Asset Store, I figured someone must have posted one online, or alternatively it can’t be that hard to write one myself. Sure enough, I found this article.

So with that shader assigned to a material, I slapped some grass on top and dirt underneath, and my convex hull looked like ground!

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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