Legacy Documentation: Version 5.6 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

TerrainData.GetAlphamaps

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public function GetAlphamaps(x: int, y: int, width: int, height: int): float[,,];
public float[,,] GetAlphamaps(int x, int y, int width, int height);

Parameters

x The x offset to read from.
y The y offset to read from.
width The width of the alpha map area to read.
height The height of the alpha map area to read.

Returns

float[,,] A 3D array of floats, where the 3rd dimension represents the mixing weight of each splatmap at each x,y coordinate.

Description

Returns the alpha map at a position x, y given a width and height.

The returned array is three-dimensional - the first two dimensions represent x and y coordinates on the map, while the third denotes the splatmap texture to which the alphamap is applied.

#pragma strict
// Add some random "noise" to the alphamaps.
function AddAlphaNoise(t: Terrain, noiseScale: float) {
	var maps: float[,,] = t.terrainData.GetAlphamaps(0, 0, t.terrainData.alphamapWidth, t.terrainData.alphamapHeight);
	for (var y: int = 0; y < t.terrainData.alphamapHeight; y++) {
		for (var x: int = 0; x < t.terrainData.alphamapWidth; x++) {
			var a0: float = maps[x, y, 0];
			var a1: float = maps[x, y, 1];
			a0 += Random.value * noiseScale;
			a1 += Random.value * noiseScale;
			var total: float = a0 + a1;
			maps[x, y, 0] = a0 / total;
			maps[x, y, 1] = a1 / total;
		}
	}
	t.terrainData.SetAlphamaps(0, 0, maps);
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { // Add some random "noise" to the alphamaps. void AddAlphaNoise(Terrain t, float noiseScale) { float[,,] maps = t.terrainData.GetAlphamaps(0, 0, t.terrainData.alphamapWidth, t.terrainData.alphamapHeight);

for (int y = 0; y < t.terrainData.alphamapHeight; y++) { for (int x = 0; x < t.terrainData.alphamapWidth; x++) { float a0 = maps[x, y, 0]; float a1 = maps[x, y, 1];

a0 += Random.value * noiseScale; a1 += Random.value * noiseScale;

float total = a0 + a1;

maps[x, y, 0] = a0 / total; maps[x, y, 1] = a1 / total; } }

t.terrainData.SetAlphamaps(0, 0, maps); } }