Applying terrain textures by altitude and slope

Hi guys,

I had an idea today while fiddling with terrains and thought I would float it out there to see if I’m crazy.

One feature I’ve seen in other engines’ terrain editors is the ability to apply textures by height or slope. This can produce some nice effects, like automatically adding a rocky texture to a steep slope or placing snow at certain altitudes.

So I was wondering if it might be possible to do this sort of thing using a ScriptableWizard in Unity. My idea for doing it goes something like this:

  • First, you paint/import your heightmap.
  • Import your desired textures into the editor.
  • Then, generate a lightmap. From what I can tell, the act of generating a lightmap also generates a splat map image (could be wrong about the order of things here).
  • Use the new TerrainData.GetHeights function to create an array of height values from your heightmap.
  • Then, calculate the slope at each point in the height array and create a new array with slope values.
  • In some sort of wizard interface, you choose which texture(s) you want to apply based on altitude and set the height threshold. Then do the same thing for slope. (Actually, since I don’t think you can access the list of textures applied to a terrain, you might have to do it based on the colors in the splat map, which should map in a predictable way to the available textures).
  • The splat map is a texture2D object like any other, so once it exists, you could adjust its colors in script using your height and slope values and SetPixels (probably using one pass for altitude and another for slope).
    I suggest all this without really knowing anything about splat maps or how they are created in Unity–I only know what I’ve observed while messing with it.

There are a few problems with this plan that I’m aware of. First, I don’t know how well it would work with a long list of textures. From what I can tell, each splat map can handle four textures, and then additional maps are created. The script would have to understand the system of applying colors to multiple splat maps.

The second problem is that it might not look very good. Without some way to handle blending of colors (which I’m sure is possible but probably beyond my abilities), this approach would create some very noticable seams on the terrain. Still, it might be a useful starting point.

The third problem I see is that this may not be worth the time. Painting terrain textures is so easy with Unity’s system, it might just be faster to paint them by hand.

Any thoughts?

-BC

Currently there is no way to access splat maps via scripts, so you can’t procedurally paint your terrain.

… and apparently there’s no way to access the splat map except through the editor’s interface (you cannot import / export it). I posted a question about this a couple days ago and never got a reply…

I am sure this is possible to do in a wizard, but would take a bit of work and probably hacks. You can feed a script the hieghtmap and some other information and it can give you a texture 2d out that you somehow use to hijack the existing splat texture.

In the future when you can really script all attributes of the terrain maybe someone should work on a random terrain generator :wink:

Actually, you -can- export lightmaps and splat maps using this script or the one that comes with the Tropical Terrain Demo.