Procedural Heightmap Jagged

I’m trying to create a procedural heightmap using LibNoise for Unity. It works brilliantly for creating images and textures, however when I apply the heightmap to a Terrain I get something like this:

I’m not entirely sure why this is, but it doesn’t seem to be to do with the texture since it displays at full resolution when I apply the texture to a Plane.

When using TerrainToolkit, however, the terrain appears smooth, meaning that it also shouldn’t be a problem with the terrain settings.

My code for heightmap generation and allocation is as follows:

public void GenerateTerrainMountains () {
		RidgedMultifractal mountainTerrain = new RidgedMultifractal();
		mountainTerrain.Seed = (int) System.DateTime.Now.Ticks;
		mountainTerrain.OctaveCount = 4;
		mountainTerrain.Frequency = 1;
		mountainTerrain.Quality = QualityMode.High;

		ModuleBase mountainModule = mountainTerrain;

		TerrainData terrainData = terrain.terrainData;

		float[,] heightMapData = terrainData.GetHeights (0, 0, terrainData.heightmapWidth, terrainData.heightmapHeight);

		heightMap = new Noise2D (terrainData.heightmapWidth, terrainData.heightmapHeight, mountainModule);
		heightMap.GeneratePlanar (0, 1, 0, 1);
		Texture2D heightMapTexture = heightMap.GetTexture ();
		heightMapTexture.Apply ();

		previewPlane.renderer.material.mainTexture = heightMapTexture;

		Color[] colours = heightMapTexture.GetPixels ();

		// Read all detail layers into a 3D int array
		int index = 0;
		for (int heightX = 0; heightX < terrainData.heightmapWidth; heightX ++) {
			for (int heightY = 0; heightY < terrainData.heightmapHeight; heightY ++) {
				float heightMapOffset = colours [index].grayscale;
				heightMapData [heightX, heightY] = heightMapOffset;
				index ++;
		// Write all detail data to terrain data
		terrainData.SetHeights (0, 0, heightMapData);

If you need any more information I will be glad to provide it.
Thanks in advance :slight_smile:

Hey @AngryPanther ! I have found a solution for you. The problem you are having is because you are assigning the verts height value off a texture. The jagged terrain effect you have has happened because your height map is a lower resolution then your actual mesh.

The height map texture uses pixels as I’m sure you know the pixels have a flat value so when more than one vert gets applied to the same pixel they will have the same value creating the jagged effect or I guess low resolution effect.

The solution I found is rather to use a 2D float array and assign the heights that way. You can do this my using the heightmap.GetData function instead of get texture.

Getting the heightmap data into a 2D float array would look something like this:

`void GetTexture () {

    // DataH looks like this -----> float[,] DataH; .Its a 2D float array

    DataH = heightmap.GetData( false , 0 , 0, false); // <------- 2D FLOAT ARRAY solution by getting the data strait and not through a texture


	texture = heightmap.GetTexture (GradientPresets.Grayscale); // <------ TEXTURE solution that creates some problems


Applying it to your mesh would look something like this:

`for (int x = 0;x <= grid_size ;x+= Lod_Factor) {
for (int y = 0;y <= grid_size ;y+= Lod_Factor) {

			//Hieghts[v] =  texture.GetPixel (x, y).grayscale; <--- problematic at high frequancy

            Hieghts[v] = DataH [x,y]; // <--- preferd option


            verts [v] = new Vector3 ((x * quad_size) - middle  , Hieghts [v] , (y * quad_size) - middle);

			UVS [v] = new Vector2 ((x * quad_size) - middle, (y * quad_size) - middle);



I know this is probably a little late but I hope it helps.