Question by 
               lwq · Aug 30, 2016 at 02:29 AM · 
                unity 5terrainproceduralheightmap  
              
 
              Procedural Terrain Diamond Square Height Issue
I've tried keeping the height max to 0-1 however it is not working. Here's the screenshot: 
Here's the code: public float roughness; public float maxHeight; public int size; private Terrain terrain; private float[,] heightMap;
     // Use this for initialization
     void Start () {
 
         terrain = Terrain.activeTerrain;
         heightMap = new float[size, size];
         genTerrain();
         terrain.terrainData.SetHeights(0, 0, heightMap);
     
     }
 
     bool isInRange(int x, int y)
     {
         if(x >= 0 && x < size && y >= 0 && y < size)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 
     float square(int x, int y, int step)
     {
         float avg = 0.0f;
         float counter = 0.0f;
 
         if(isInRange(x - step, y - step))
         {
             avg += heightMap[x - step, y - step];
             counter++;
         }
 
         if(isInRange(x - step, y + step))
         {
             avg += heightMap[x - step, y + step];
             counter++;
         }
 
         if(isInRange(x + step, y + step))
         {
             avg += heightMap[x + step, y + step];
             counter++;
         }
 
         if(isInRange(x + step, y - step))
         {
             avg += heightMap[x + step, y - step];
             counter++;
         }
 
         return avg / counter;
            
     }
 
     float diamond(int x, int y, int step)
     {
         float avg = 0.0f;
         float counter = 0.0f;
 
         if(isInRange(x - step, y))
         {
             avg += heightMap[x - step, y];
             counter++;
         }
 
         if(isInRange(x + step, y))
         {
             avg += heightMap[x + step, y];
             counter++;
         }
 
         if(isInRange(x, y + step))
         {
             avg += heightMap[x, y + step];
             counter++;
         }
 
         if(isInRange(x, y - step))
         {
             avg += heightMap[x, y - step];
             counter++;
         }
 
         return avg / counter;
     }
 
     void genTerrain()
     {
         heightMap[0, 0] = 1;// Random.Range(0, maxHeight);
         Debug.Log(heightMap[0, 0]);
 
         heightMap[0, size - 1] = 1;// Random.Range(0, maxHeight);
         Debug.Log(heightMap[0, size - 1]);
 
         heightMap[size - 1, 0] = 1;// Random.Range(0, maxHeight);
         Debug.Log(heightMap[size - 1, 0]);
 
         heightMap[size - 1, size - 1] = 1;//  Random.Range(0, maxHeight);
         Debug.Log(heightMap[size - 1, size - 1]);
 
         int step = size;
 
         while(step > 1)
         {
             Debug.Log(step);
 
             int halfStep = step / 2;
             float scale = step * roughness;
 
             for(int x = halfStep; x < size; x += step)
             {
                 for(int y = halfStep; y < size; y += step)
                 {
                     heightMap[x, y] = square(x, y, halfStep) + Random.value * 2 * scale - scale;
                     //Debug.Log("SquareStep");
                 }
             }
 
             for(int x = 0; x < size; x += halfStep)
             {
                 for(int y = (x + halfStep) % step; y < size; y += step)
                 {
                     heightMap[x, y] = diamond(x, y, halfStep) + Random.value * 2 * scale - scale;
                     //Debug.Log("DiamondStep");
                 }
             }
 
             step /= 2;
         }
     }
 }
 
 
                 
                capture.png 
                (312.2 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Diamond Square Algorithm Problems 0 Answers
How can i make a planet in unity 5? 2 Answers
Border around procedural height map 1 Answer
How does one convert a System.Drawing type Bitmap into a compatable terrain heightmap for unity 0 Answers
Terrain turning into slices when using a heightmap,Heightmap Generating Slices 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                