- Home /
Generated terrain heightmap is flipped from the alphamap.
Ive made a terrain generator which generates hills in random positions, after the hills are being generated and the heightmap is set the textures from the alphamap are setting themselfs by the height of each point on the terrain, different heights with different textures.
But, for some reason the textures are being set correctly but flipped from the height map, in wrong places around the terrain but in the same structure given by the height map. Please help me, how do I set the alpha map correctly and not flipped from the height map...
This is the hill generating part
public void GenerateTerrain()
{
for(int h=0;h<hillCount;h++)
{
CreateHill(Random.Range(0,tData.heightmapWidth),Random.Range(0,tData.heightmapHeight),Random.Range(0.005f,0.1f),Random.Range(0.5f,5f));
}
tData.SetHeights(0,0,terrainGRID);
Smooth();
}
public void CreateHill(int x, int y, float height,float pointyness)
{
float point = 0;
float distanceFromTop;
terrainGRID[x,y]=height;
for(int a=0;a<tData.heightmapWidth;a++)
{
for(int b=0;b<tData.heightmapHeight;b++)
{
distanceFromTop=Mathf.Sqrt( Mathf.Pow((y-b),2) + Mathf.Pow((x-a),2) );
point=((height-terrainGRID[a,b])*1000 - distanceFromTop*pointyness)/1000 ;
if(point<0)
point=0;
terrainGRID[a,b]+=point;
}
}
}
And this is the alpha map setting part which comes after the hills aprt:
SplatPrototype[] terrainTexture = new SplatPrototype[4];
terrainTexture[0] = new SplatPrototype();
terrainTexture[0].texture = (Texture2D)Resources.Load("terrain_ground2");
terrainTexture[0].tileOffset = new Vector2(0, 0);
terrainTexture[0].tileSize = new Vector2(15, 15);
terrainTexture[1] = new SplatPrototype();
terrainTexture[1].texture = (Texture2D)Resources.Load("terrain_ground3");
terrainTexture[1].tileOffset = new Vector2(0, 0);
terrainTexture[1].tileSize = new Vector2(15, 15);
terrainTexture[2] = new SplatPrototype();
terrainTexture[2].texture = (Texture2D)Resources.Load("terrain_ground4");
terrainTexture[2].tileOffset = new Vector2(0, 0);
terrainTexture[2].tileSize = new Vector2(15, 15);
terrainTexture[3] = new SplatPrototype();
terrainTexture[3].texture = (Texture2D)Resources.Load("terrain_ground5");
terrainTexture[3].tileOffset = new Vector2(0, 0);
terrainTexture[3].tileSize = new Vector2(15, 15);
tData.splatPrototypes = terrainTexture;
float[,,] map = new float[tData.heightmapWidth-1,tData.heightmapHeight-1, 4]; // needs to be 1
// For each point on the alphamap...
for (int y = 0; y < tData.heightmapWidth-1; y++)
{
for (int x = 0; x < tData.heightmapHeight-1; x++)
{
if(tData.GetHeight(x,y)<10)
map[x, y, 0] = 1.0f;
else if(tData.GetHeight(x,y)<20)
{
map[x, y, 0] = 0.5f;
map[x, y, 1] = 0.5f;
}
else if(tData.GetHeight(x,y)<30)
map[x, y, 1] = 1.0f;
else if(tData.GetHeight(x,y)<40)
{
map[x, y, 1] = 0.5f;
map[x, y, 2] = 0.5f;
}
else if(tData.GetHeight(x,y)<50)
map[x, y, 2] = 1.0f;
else if(tData.GetHeight(x,y)<60)
{
map[x, y, 2] = 0.5f;
map[x, y, 3] = 0.5f;
print (tData.GetHeight(x,y));
}
else if(tData.GetHeight(x,y)<70)
map[x, y, 3] = 1.0f;
else if(tData.GetHeight(x,y)<80)
{
map[x, y, 3] = 0.5f;
map[x, y, 4] = 0.5f;
print (tData.GetHeight(x,y));
}
else if(tData.GetHeight(x,y)<90)
map[x, y, 4] = 1.0f;
}
}
tData.SetAlphamaps(0, 0, map);
The outcome is :
Answer by benk0913 · Jul 31, 2013 at 05:22 PM
PROBLEM SOLVED, I had to replace the Y and X in the height map "ifs", it now sets the textures correctly.
i am using ur script but its not working it is returning me the height but the splat maps are returning null values i check it but its not working can u plz guide me
Yeah, I had to do the same thing, but I was wondering because in the docs, they say the alphamaps should be indexed as [x, y, l], where l is the layer of the texture. But then it works when we index it as [y, x, l]?