Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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: alt text

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges