I wrote a script that changes the height of the terrain and changes the texture layer in the coordinates of the CSV file.
Below is the script I wrote.
public void LoadTerrainData()
{
string[] lines = CacheTerrainChange.TerrainChangeString.Split('\n');
foreach (string line in lines)
{
string[] values = line.Split(',');
double lat = double.Parse(values[0]);
double lng = double.Parse(values[1]);
float altitude = float.Parse(values[2]);
Vector3 terrainPos = ConvertCoordinates(lat, lng);
foreach (Terrain terrain in terrains)
{
float x = (terrainPos.x - terrain.transform.position.x) / terrain.terrainData.size.x;
float z = (terrainPos.z - terrain.transform.position.z) / terrain.terrainData.size.z;
int heightmapX = Mathf.RoundToInt(x * (terrain.terrainData.heightmapResolution - 1));
int heightmapZ = Mathf.RoundToInt(z * (terrain.terrainData.heightmapResolution - 1));
float[,] heights = terrain.terrainData.GetHeights(0, 0, terrain.terrainData.heightmapResolution, terrain.terrainData.heightmapResolution);
if (heightmapX >= 0 && heightmapX < terrain.terrainData.heightmapResolution && heightmapZ >= 0 && heightmapZ < terrain.terrainData.heightmapResolution)
{
float previousAltitude = heights[heightmapZ, heightmapX] * terrain.terrainData.size.y;
heights[heightmapZ, heightmapX] = altitude / terrain.terrainData.size.y;
terrain.terrainData.SetHeights(0, 0, heights);
int alphamapX = Mathf.RoundToInt(x * (terrain.terrainData.alphamapResolution - 1));
int alphamapZ = Mathf.RoundToInt(z * (terrain.terrainData.alphamapResolution - 1));
float[,,] splatmapData = terrain.terrainData.GetAlphamaps(0, 0, terrain.terrainData.alphamapWidth, terrain.terrainData.alphamapHeight);
if (alphamapX >= 0 && alphamapX < terrain.terrainData.alphamapResolution && alphamapZ >= 0 && alphamapZ < terrain.terrainData.alphamapResolution)
{
for (int i = 0; i < terrain.terrainData.alphamapLayers; i++)
{
if (i == 1) // Red Layer
{
if (altitude > previousAltitude)
{
splatmapData[alphamapZ, alphamapX, i] = 1f;
}
else
{
splatmapData[alphamapZ, alphamapX, i] = 0f;
}
}
else if (i == 2) // green Layer
{
if (altitude < previousAltitude)
{
splatmapData[alphamapZ, alphamapX, i] = 1f;
}
else
{
splatmapData[alphamapZ, alphamapX, i] = 0f;
}
}
else
{
splatmapData[alphamapZ, alphamapX, i] = 0f;
}
terrain.terrainData.SetAlphamaps(0, 0, splatmapData);
}
}
}
}
}
}
The above script applies only to coordinates of a point on Unity.
If there is a coordinate of one point, I want to set the height as much as the desired circular range around the coordinate of one point. Like a ‘set height’ brush.
Is there a function or library to implement this function?
Help me plz…