Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


SetAlphamaps(int, int, float[,,])

Assign all splat values in the given map area.
Read time 2 minutesLast updated 4 hours ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.TerrainModule

SetAlphamaps(int, int, Single[0:,0:,0:])

public void SetAlphamaps(int x, int y, float[,,] map)

Parameters

The x offset to write to.

The y offset to write to.

The 3D array of splat weights to assign, where the third dimension represents the mixing weight of each splatmap at each x,y coordinate.

Remarks

The array supplied to this function determines the width and height of the portion to be replaced. The third dimension of the array corresponds to the number of terrain layers and must equal TerrainData.alphamapLayers. The array is indexed as [y,x,i].
The splat weights are stored in TerrainData.alphamapTextures, which are separate Texture2D objects. If an Editor script creates a TerrainData and saves it with
AssetDatabase.CreateAsset
after it assigns terrain layers, the alpha map textures are not added to the asset and the weights are lost. Save the TerrainData as an asset before you assign terrain layers and call this function. To make a change from an Editor script undoable, register both the TerrainData and its TerrainData.alphamapTextures with
Undo.RegisterCompleteObjectUndo
before you call this function. If you register only the TerrainData, undo does not restore the weights.

Examples

using UnityEngine;public class Example : MonoBehaviour{ public Terrain t; // Blend the first two terrain layers according to the steepness of // the slope at each point. The terrain must have at least two layers. void Start() { // The array is [height, width, layers] and covers every terrain layer. // This overwrites any existing weights; layers other than the first two are set to zero. float[,,] map = new float[t.terrainData.alphamapHeight, t.terrainData.alphamapWidth, t.terrainData.alphamapLayers]; // For each point on the alphamap... for (int y = 0; y < t.terrainData.alphamapHeight; y++) { for (int x = 0; x < t.terrainData.alphamapWidth; x++) { // Get the normalized terrain coordinate that // corresponds to the point. float normX = x * 1.0f / (t.terrainData.alphamapWidth - 1); float normY = y * 1.0f / (t.terrainData.alphamapHeight - 1); // Get the steepness value at the normalized coordinate. var angle = t.terrainData.GetSteepness(normX, normY); // Steepness is given as an angle, 0..90 degrees. Divide // by 90 to get an alpha blending value in the range 0..1. var frac = angle / 90.0; // Note the y and x are not in the traditional order. map[y, x, 0] = (float)frac; map[y, x, 1] = (float)(1 - frac); } } t.terrainData.SetAlphamaps(0, 0, map); }}