I've written a script to create dynamic height maps. How do I use those height maps to generate dynamic procedural terrains?

Hello everyone,

I’ve written a few scripts that make dynamic procedural height maps based on perline noise. This works great! I followed another tutorial that showed me how to convert those height maps into meshes which works great! Here is an image of a map I generated procedurally.

However I’ve quickly come to realize that meshes do not make for the best terrains because of their limitations. Specifically I want to procedurally texture the map and place bushes etc. Everything I have read has said that terrains are a better type for this kind of application!

However I’m having trouble gaining any traction on how to implement a dynamic height map onto a terrain. There are plenty of resources I can read on how to do it manually, or how to import height maps etc. This isn’t what I want.

So my question is, given that I already have a height map stored in a normalized 2d array how do I transfer that height map to a terrain at runtime?

public static void GenerateTerrainMesh(float [,] heightMap) {

   Terrain mapTerrain = Terrain.activeTerrain; //Get the active terrain (make sure you only have 1 terrain in the scene)

   TerrainData terrainData = mapTerrain.terrainData;

   int width = heightMap.GetLength (0);
   int height = heightMap.GetLength (1);

   terrainData.size = new Vector3 (width, 1, height);
   terrainData.SetHeights (0, 0, heightMap);

Please not that I have manually placed a terrain object into my scene so I don’t need to (I don’t think?) dynamically create one.

That little bit of code is where I’ve gotten to after all my ready and trial and errors. I know it’s not much but you can see what I’m trying to do.

Thank you so much to anyone who can help me!

That suggests that the dimensions of your heightMap array do not match the dimensions of your terrain.
(note that that is not what line 10 does - that sets the size of the terrain, not the size of the heightmap).

The heightMap array should be a square, power of two +1, e.g. 513x513, 1025x1025, which matches the resolution of the terrain component set in the inspector. Is that the case?