- Home /
The problem was caused because I had Automatic Baking of Lightmaps turned off. Works fine with it turned on.
SetAlphamaps causing editor to crash V5.4.0f3
EDIT: It seems to work fine if I turn on Automatic Baking of Lightmaps in the lighting window. So problem solved :-)
Hey guys, I'm trying to add some procedural texture painting to the terrain in my project, and as a start I'm trying to get the example given in the Unity Documentation to work.
Here's my C# code:
void Start()
{
// Get the attached terrain component
Terrain terrain = GetComponent<Terrain>();
// Get a reference to the terrain data
TerrainData terrainData = terrain.terrainData;
float[,,] map = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, 5];
// For each point on the alphamap...
for (int y = 0; y < terrainData.alphamapHeight; y++)
{
for (int x = 0; x < terrainData.alphamapWidth; x++)
{
// Get the normalized terrain coordinate that
// corresponds to the the point.
float normX = (x * 1 / (terrainData.alphamapWidth - 1));
float normY = (y * 1 / (terrainData.alphamapHeight - 1));
// Get the steepness value at the normalized coordinate.
float angle = 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.
float frac = angle / 90;
map[x, y, 0] = frac;
map[x, y, 1] = 0;
map[x, y, 2] = 1-frac;
map[x, y, 3] = 0;
map[x, y, 4] = 0;
}
}
terrainData.SetAlphamaps(0, 0, map);
}
However this causes the editor to crash when I run the code. The only way I can get it to work is if I set the strength of the first texture to 1, like so:
map[x, y, 0] = 1f;
Then it works but obviously doesn't result in the desired effect. Is there something really obvious I'm missing? I've spent hours googling for an answer but haven't found any solutions.
Thanks!
Answer by FelixWun · Sep 12, 2016 at 01:13 PM
Just in case anyone else runs into this problem, here's a workaround I came up with. I've set the first texture to a black jpeg and just leave it's value as 1f in the code. This gives you a base of 0 to add your other colours on top of. So in the editor, make sure the first texture in your list is the black texture, then set the others as desired.
Here's my code:
void Start()
{
// Get the attached terrain component
Terrain terrain = GetComponent<Terrain>();
// Get a reference to the terrain data
TerrainData terrainData = terrain.terrainData;
float[,,] map = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, 6];
// For each point on the alphamap...
for (int x = 0; x < terrainData.alphamapHeight; x++)
{
for (int y = 0; y < terrainData.alphamapWidth; y++)
{
// Get the normalized terrain coordinate that
// corresponds to the the point.
float normX = ((float)x * 1 / (terrainData.alphamapWidth - 1));
float normY = ((float)y * 1 / (terrainData.alphamapHeight - 1));
// Get the steepness value at the normalized coordinate.
float angle = terrainData.GetSteepness(normY, normX);
// Steepness is given as an angle, 0..90 degrees. Divide
// by 90 to get an alpha blending value in the range 0..1.
float frac = angle / 90;
map[x, y, 0] = 1f;
map[x, y, 1] = 1-frac;
map[x, y, 2] = frac;
map[x, y, 3] = 0;
map[x, y, 4] = 0;
map[x, y, 5] = 0;
}
}
terrainData.SetAlphamaps(0, 0, map);
}