- Home /
Is there a script to merge terrain tiles into one terrain?
Hi,
is there a script to merge terrain tiles into one terrain? No stitching needed, just reconfigure tiling.
Kind Regards, Skandy
Comment
Best Answer
Answer by SkandYxyz · Jun 19, 2021 at 06:18 PM
Here a script i wrote to merge the heightmaps of 256 terrains into a single terrain heightmap. using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEditor;
[ExecuteInEditMode]
public class CreateBigTerrain : MonoBehaviour
{
public Terrain _bigTerrain;
public Terrain _tiles;
public TerrainData tData;
public RenderTexture tileHeightmap;
public RenderTexture newHeightmap;
public bool active=true;
private int progressId;
IEnumerator Start()
{
if(active)
{
// Create a new progress indicator
progressId = Progress.Start("Merging Terrain");
yield return StartCoroutine("Calculate");
// The task is finished. Remove the associated progress indicator.
Progress.Remove(progressId);
active=false;
StopAllCoroutines();
}
DestroyImmediate(this);
}
IEnumerator Calculate()
{
TerrainData terrainData;
string path;
bool flipVertical=false;
Vector2 inputLevelsRange;
//_bigTerrain = GameObject.Find("bigTerrain").GetComponent<Terrain>();
int heightmapWidth = 1024;
int heightmapHeight = 1024;
float normalize = (1 << 16);
byte[] data = new byte[4096 * 4096 * 2];
for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
_tiles = GameObject.Find("Terrain_"+x+"_"+y+"-20210513 - 125829").GetComponent<Terrain>();
tData = _tiles.terrainData;
float[,] heights = tData.GetHeights(0, 0, 1024, 1024);
for (int ty = 0; ty < 256; ++ty)
{
for (int tx = 0; tx < 256; ++tx)
{
int index = (x*256+tx)+(y*256+ty)*4096; //tx*4 + ty*4 * 1024;
//int srcY = flipVertical ? 1024 - 1 - y : y;
float remappedHeight = heights[ty*4, tx*4];// * (inputLevelsRange.y - inputLevelsRange.x) + inputLevelsRange.x;
int height = Mathf.RoundToInt(remappedHeight * normalize);
ushort compressedHeight = (ushort)Mathf.Clamp(height, 0, ushort.MaxValue);
byte[] byteData = System.BitConverter.GetBytes(compressedHeight);
data[index * 2 + 0] = byteData[0];
data[index * 2 + 1] = byteData[1];
}
}
yield return new WaitForSeconds(0.1f);
Progress.Report(progressId, x+16*y, 256,"Heightmap of Terrain_"+x+"_"+y+"-20210513 - 125829");
}
}
path="Assets/Heightmaps/bigTerrain";
FileStream fs = new FileStream((path + ".raw"), FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
yield return null;
}
// Update is called once per frame
void Update()
{
}
}
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Chopping down trees painted onto the terrain 2 Answers
Lights attached to objects turning off over terrain 2 Answers
Splitting map for multiplayer? 1 Answer
Update terrain alphamap at runtime makes my game crawling 3 Answers