Terraingeneration Dividing HeightMap into smaller Parts

Hey,

I’m creating an Terrain based on Size using a HeightMap.

What is working in my Script?

  1. Terraininstancing and positioning from Prefab
  2. HeightMapGeneration (1 HeightMap for everything)
  3. Getting Terrain at Position
  4. Adding HeightMap to Terraintile

What is my problem?
Every Single Terrain uses the same Part of the HeightMap.

What does my code looks like?

  1. HeightMapGeneration
    public float[,] HeightMapGeneration(int MapWidth, int MapHeight, int MapSeed, float Freq, float elev, Vector3 Offset)
    {
        float[,] HeightMap = new float[MapWidth, MapHeight];

        for (int x = 0; x < MapWidth; x++)
        {
            for (int z = 0; z < MapHeight; z++)
            {
                float nx = ((x + Offset.x) / Freq) + MapSeed; //x / HeightMapWidth - 0.5f;
                float nz = ((z + Offset.z) / Freq) + MapSeed; //y / HeightMapHeight - 0.5f;


                // Create different noise levels
                    float e0 =         Mathf.PerlinNoise(nx , nz);
                    float e1 = .5f *   Mathf.PerlinNoise(nx * 2, nz * 2) * e0;
                    float e2 = .25f *  Mathf.PerlinNoise(nx * 4, nz * 4) * (e1 + e0);

                // Combine Noise levels
                    float e = (e0 + e1 + e2);
             
                // Make flat areas
                    HeightMap[x, z] = Mathf.Pow(e, elev);
            }
        }
        return HeightMap;
    }
  1. HeightMap
        // Create HeightMap for every single Terrain
        
            // Cycle trough every Terrain
            for (int Width = 0; Width < Terraincount_Width; Width++)
            {
                for (int Height = 0; Height < Terraincount_Height; Height++)
                {
                    int TerrainOffset_X = Width * SingleTerrainSize + 500;
                    int TerrainOffset_Z = Height * SingleTerrainSize + 500;
                 
                // Get Terrain under Offset
                    Terrain ActiveTerrain = GetTerrain(new Vector3(TerrainOffset_X, 0, TerrainOffset_Z), Terrainarray);

                // Moove heightmap to local TerrainMap
                    float[,] TerrainMap = new float[SingleTerrainHMSize, SingleTerrainHMSize];  // local HeightMap
                     
                        int HeightMapFirstX = Width * SingleTerrainHMSize;      // First Position of the local HeightMap in the Global one
                        int HeightMapFirstZ = Height * SingleTerrainHMSize;
             
                    for (int TerrainMapX = 0; TerrainMapX <  SingleTerrainHMSize; TerrainMapX++)
                        {
                            int HeightMapPosition_X = HeightMapFirstX + TerrainMapX;
                  
                            for (int TerrainMapZ = 0; TerrainMapZ < SingleTerrainHMSize; TerrainMapZ++)
                            {
                             
                                int HeightMapPosition_Z = HeightMapFirstZ + TerrainMapZ;

                                // Moove 0f-1f Value to Heightvalue from MinHeight to Maxheight and then divide by MaxTerrainHeight
                                    TerrainMap[TerrainMapX, TerrainMapZ] = Mathf.Lerp(MinHeight, MaxHeight, HeightMapData[HeightMapPosition_X, HeightMapPosition_Z]) / TerrainMaxHeight;
                            }
                        }

                // Change Height
                    ActiveTerrain.terrainData.SetHeights(0, 0, TerrainMap);
                }
            
    }

In this Code:
Create HeightMap works, I recently logged all of it in the Console and it cycled trough every single Pixel

Create Terrain Works, It Creates Instances of each Terrain

All Terrains to Array also works

// Create HeightMap for every single Terrain does not work…
It Adds the same Heightdata to each Terrainmap (local HeightMap)

This is the result of an 2 by 2 Terrainfield

Either I’m to stupid to find the error or I’m blind :smile:
I tried to fix this for the past 2 days, so slowely but surely I’m thinking of the first.

Ok, just tested the behaviour again.

Looks like they are exact clones and keep themselves updating.


Changing one Terrain changes the other ones to…
5218067--519917--upload_2019-11-27_7-46-18.png
Even if they are clones from my flat Prefab

did you ever resolve this?