- Home /
Terrain Texture
I write some code to create a terrain and add maps.
This is my code:
============================================================================
float[, ,] splatmapData = new float[terrain.heightmapWidth, terrain.heightmapWidth, terrain.alphamapLayers];
for (int y = 0; y < terrain.heightmapWidth; y++)
{
for (int x = 0; x < terrain.heightmapWidth; x++)
{
// read the height at this location
float height = terrain.GetHeight(y, x);
//float height = terArr[x,y];
Vector3 splat = new Vector3(0, 1, 0);
if (height/128 < 0.2f)
{
//splat = Vector3.Lerp(splat, new Vector3(0, 0, 0.3f), height);
splat = new Vector3(1, 0, 0);
}
else if (height / 128 < 0.5f)
{
//splat = Vector3.Lerp(splat, new Vector3(0, 0, 0.6f), height);
splat = new Vector3(0, 1, 0);
}
else
{
//splat = Vector3.Lerp(splat, new Vector3(1, 0, 0), height);
splat = new Vector3(0, 0, 1);
}
// 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;
}
============================================================================
This code seems work well.But the only question is the above code only support 3 textures,if I want to add more texture on my Terrain.How can I do ?
What exactly do you wish to happen?
Add a new texture using script or a good way to handle splatmap that may contain more than one texture?
I want to use more than four texture,but it doesn't work.The terrain will be Pink
Would you explain how you tried to put 4 textures? If a texture was simply added to Terrain textures, then there should be no problem.
I add the texture like this:
//Texture
public Texture2D[] t;
terrain.splatPrototypes = CreateSplatPrototypes(t, new Vector2(4, 4), new Vector2(0, 0));
public SplatPrototype CreateSplatPrototype(Texture2D tmpTexture, Vector2 tmpTileSize, Vector2 tmpOffset)
{
SplatPrototype outSplatPrototype = new SplatPrototype();
outSplatPrototype.texture = tmpTexture;
outSplatPrototype.tileOffset = tmpOffset;
outSplatPrototype.tileSize = tmpTileSize;
return outSplatPrototype;
}
public SplatPrototype[] CreateSplatPrototypes(Texture2D[] tmpTextures, Vector2 tmpTileSize, Vector2 tmpOffset)
{
//Debug.Log(tmpTextures.Length);
SplatPrototype[] outSplatPrototypes = new SplatPrototype[tmpTextures.Length];
for (int i = 0; i < tmpTextures.Length; ++i)
{
outSplatPrototypes[i] = CreateSplatPrototype(tmpTextures[i], tmpTileSize, tmpOffset);
}
return outSplatPrototypes;
}
Follow this Question
Related Questions
Terrain Painting Script 0 Answers
How to "paint" the terrain in a script? 1 Answer
Terrain texture low quality 0 Answers
Blob Shadow and Trees Using Terrain Cause Issues 0 Answers
create terrain using script 1 Answer