How to automatically apply different textures on terrain based on height?

Hello,

I would like to render my terrain only with respect to the height values obtained from the height map via HSV Shading. Basically extreme heights will be rendered with brown color, medium heights with green and heights close to sea level will be rendered with blue. Inbetween values will be interpolated. My terrain is too big so I cannot simply paint it with textures(it will take weeks)

Is there a way I can achieve this in unity? I suppose there is no way we can specify shading for the built-in terrain object but rather we should be changing the built-in shader(possibly overwriting it) itself which affects terrain rendering...

Can anyone please explain how to do it in detail?

Best,

Ates Akaydin

It sounds like what you really want to do is assign your splatmaps procedurally via scripting, which is entirely possible, although undocumented (and therefore not officially supported by Unity).

You'd need to assign one splatmap texture for each colour you require, then you need to create a c# script which does the procedural splatmapping.

First, make sure you have a reference to the terrainData object that your terrain is using. Eg:

TerrainData terrainData = Terrain.activeTerrain.terrainData;

The splatmap data is stored internally as a 3d array of floats, so to declare a new empty array ready for your custom splatmap data, do this:

float[, ,] splatmapData = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, terrainData.alphamapLayers];

The three dimensions represent X, Y, and Texture Index (of the textures assigned for terrain painting). X and Y are normalised terrain coordinates, going from 0-1. The value in the 3rd dimension is a "weight" indicating how much of that particular texture is blended in at that particular location on the splat map. The sum of the weights for every texture for any single X,Y value should add up to 1.

You would then want to fill that array with values based on the height reading at each point.

For example:

for (int y = 0; y < terrainData.alphamapHeight; y++)
{
    for (int x = 0; x < terrainData.alphamapWidth; x++)
    {

        // read the height at this location
        float height = terrainData.GetHeight(x,y);

        // determine the mix of textures 1, 2 & 3 to use 
        // (using a vector3, since it can be lerped & normalized)

        Vector3 splat = new Vector3(0,1,0);
        if (height > 0.5) {
            splat = Vector3.Lerp(splat, new Vector3(0,0,1), (height-0.5f)*2 );
        } else {
            splat = Vector3.Lerp(splat, new Vector3(1,0,0), height*2 );
        }

        // now assign the values to the correct location in the array
        splat.Normalize();
        splatmapData[x, y, 0] = splat.x;
        splatmapData[x, y, 1] = splat.y;
        splatmapData[x, y, 2] = splat.z;
    }
}

// and finally assign the new splatmap to the terrainData:
terrainData.SetAlphamaps(0, 0, splatmapData);

(The splat map is referred to as the 'alphamap' in unity. The terms are interchangable)

The code above makes the blend between the 3 textures over the entire possible height of the terrain. You'd need to change the code if you want the blend to occur over a different range within your height values.

Important Note:

Some of these functions are undocumented. Any future updates of the unity engine might change or remove these functions from the API. This means your project may not work in future versions of the Unity editor, and webplayer builds may not work with future versions of the plugin.

You can use it tutorial

There's an extension in the unity resources called Terrain Toolkit that will do exactly what you're talking about. It worked in Unity 2.x; I haven't yet tried it in Unity 3 yet.

Or you can check out my asset on here that paints texture on terrain base off height
https://www.assetstore.unity3d.com/en/#!/content/19749

You can use this asset to so link text works with vertex colors and textures