Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Hiwyn · Dec 29, 2014 at 01:25 PM · terrainheightmapalgorithm

Diamond Square algorithm height problem

I'm currently trying to implement the Diamond Square algorithm in unity. When I generate a terrain I always end up to flat areas at max height like so : alt text

Here is my code :

     // Check if the point (x, y) exist
     private bool isInRange(int x, int y)
     {
         if(x >= 0 && x < resolution && y >= 0 && y < resolution)
             return true;
 
         else
             return false;
     }
 
     // Do the average of the square corners height
     private float sqrAvr(int x, int y, int step)
     {
         float average = 0.0f;
         float counter = 0.0f;
 
         if(isInRange(x - step, y - step))
         {
             average += heightMap[x - step, y - step];
             counter++;
         }
 
         if(isInRange(x - step, y + step))
         {
             average += heightMap[x - step, y + step];
             counter++;
         }
 
         if(isInRange(x + step, y + step))
         {
             average += heightMap[x + step, y + step];
             counter++;
         }
 
         if(isInRange(x + step, y - step))
         {
             average += heightMap[x + step, y - step];
             counter++;
         }
 
         return average / counter;
     }
 
     // Do the average of the diamond corners height
     private float diaAvr(int x, int y, int step)
     {
         float average = 0.0f;
         float counter = 0.0f;
         
         if(isInRange(x - step, y))
         {
             average += heightMap[x - step, y];
             counter++;
         }
         
         if(isInRange(x + step, y))
         {
             average += heightMap[x + step, y];
             counter++;
         }
         
         if(isInRange(x, y + step))
         {
             average += heightMap[x, y + step];
             counter++;
         }
         
         if(isInRange(x, y - step))
         {
             average += heightMap[x, y - step];
             counter++;
         }
         
         return average / counter;
     }
 
     private void generateTerrain()
     {
         // Corners initialisation
         heightMap[0, 0] = Random.Range(0, maxHeight);
         heightMap[0, resolution - 1] = Random.Range(0, maxHeight);
         heightMap[resolution - 1, 0] = Random.Range(0, maxHeight);
         heightMap[resolution - 1, resolution - 1] = Random.Range(0, maxHeight);
 
         // Generation of the rest of the map
         int step = resolution;
 
         while(step > 1)
         {
             Debug.Log(step);
 
             int halfStep = step / 2;
             float scale = step * roughness;
 
             // Square part
             for(int x = halfStep ; x < resolution ; x += step)
                 for(int y = halfStep ; y < resolution ; y += step)
                     heightMap[x, y] = sqrAvr(x, y, halfStep) + Random.value * 2 * scale - scale;
 
             // Diamond part
             for(int x = 0 ; x < resolution ; x += halfStep)
                 for(int y = (x + halfStep) % step ; y < resolution ; y += step)
                     heightMap[x, y] = diaAvr(x, y, halfStep) + Random.value * 2 * scale - scale;
 
             step /= 2;
         }
     }
 
     public void Start()
     {
         terrain = Terrain.activeTerrain;
         heightMap = new float[resolution, resolution];
         generateTerrain();
         terrain.terrainData.SetHeights(0, 0, heightMap);
     }

Hope I can find an answer here. :)

genterrain.png (88.5 kB)
Comment
Add comment · Show 1
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
avatar image taxvi · Dec 29, 2014 at 01:41 PM 0
Share

I think your maxHeight should be equal to the terrain's max height. I'm not even sure how to work with terrains but hopefully sticking this line in the Start() function will get you somewhere:

 maxHeight = Terrain.activeTerrain.terrainData.heightmapHeight;

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Owen-Reynolds · Dec 29, 2014 at 05:59 PM

It does, as taxvi alludes to, look as if you're generating heights in a much greater range than the terrain can be. Like what we're seeing is a tiny slice of the real terrain.

A cheap way to test might be to scale and slide the final result, at line 113.5. heightMap[i,j]=heightMap[i,j]*scale+slide;. Try with a very small terrain. Trail and error should find the range.

That, or just manually set heights 0-255 along the sides, and see what it makes (it you get a tiny slope up, then all flat, the range must be 0-1.)

Or maybe jack up the numbers on your Terrain object and see if it looks less slopey.

Comment
Add comment · Show 5 · Share
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
avatar image Hiwyn · Dec 30, 2014 at 03:52 AM 0
Share

Yep, after some test I found that the height must be between 0 and 1. Now it's much better : alt text

Thank you very much taxvi and Owen Reynolds ! :)

genterrain3.png (215.7 kB)
avatar image lwq · Aug 29, 2016 at 04:38 PM 0
Share

Hi Owen I'm facing similar issues as above. But I still can't get it to work and my code is an adaptation of above.

avatar image lwq · Aug 29, 2016 at 04:41 PM 0
Share

alt text

capture.png (57.4 kB)
avatar image Owen-Reynolds lwq · Aug 29, 2016 at 05:12 PM 0
Share

If the Answer here didn't help (the range is 0-1) then this seems like a new Q.

I think you should post it to the HelpRoom area. And put your code as text (formatted as code -- you'll see if it looks right.) That way someone could paste and test it if they wanted.

avatar image lwq Owen-Reynolds · Aug 30, 2016 at 02:32 AM 0
Share

Hi Owen, I've just posted one. Here's the link http://answers.unity3d.com/questions/1236569/procedural-terrain-height-issue.html

Cheers

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity splitting up terrains into equal heightmap resolution ones. 0 Answers

Issue with importing raw heightmaps (made in photoshop) 5 Answers

Generate heightmap from Terrain object with code 1 Answer

Terrain Heightmap Can't Attach 1 Answer

Applying a Large Texture Map to a Terrain 1 Answer


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