I need the terrain texture to change at certain positions depending on where trees etc. are randomly positioned at runtime. I'm struggling to find any solid information about how to take a particular vector3 and tell the terrain to paint a different texture of size x at that location. So far I have found a lot of information about how to change textures based on height map information. As my map is flat, that information doesn't seem very relevant.
EDIT: using the following code when each tree was randomly instantiated:
public void ChangeTextureAtTreeLocation(Vector3 treePosition)
{
int mapX = (int)(((treePosition.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth);
int mapZ = (int)(((treePosition.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight);
float[,,] splatmapData = terrainData.GetAlphamaps(1, 1, 4, 4);
terrainData.SetAlphamaps( mapX, mapZ, splatmapData);
terrain.Flush();
}
got me this result:
As you can tell, the results aren't great. There are two main deficiencies:
- I don't know how to access the terrain texture array directly so I paint a portion of the map first (see the bottom left of the image), sample that portion, and then set that sample at the location of the trees. This is not ideal as I cannot change the shape, size, or opacity of the added terrain texture. There is also no blending between textures, which gives the texture a rabbit burrow look.
- you cannot see it in the image, but the textures do not appear under the trees but are directly north east of them. Not sure why that is. I'm assuming it has something to do with how textures are drawn relative to their origin.
terrainPos
in this example?terrain.GetPosition()
? I'm attempting to do the same thing you are and having no luck. \$\endgroup\$