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 Greenie · Jan 12, 2015 at 12:51 PM · javascriptterrainproceduralgeneration

Diamond Square Algorithm

Hello,

So I've been making a random terrain generator however the terrain it is producing is not excellent. I would like it to appear more random and realistic. Here is my code and a screenshot of what I'm getting.

 #pragma strict
     var width : int;
     var height : int;
     var desiredHeight : float;
     var step: int;
     var minNoise : float;
     var maxNoise : float;
 function Start () {
     var terrainDat = GetComponent(Terrain).terrainData;
     width = terrainDat.heightmapWidth;
     height = terrainDat.heightmapHeight;
     var xAnchor : int = 0;
     var yAnchor : int = 0;
     var heights = terrainDat.GetHeights(0,0,width,height);
 
     width = width-1;
     height = height-1;
     step = width;
     //corners
     heights[xAnchor,yAnchor] = desiredHeight;
     terrainDat.SetHeights(0,0,heights);
     heights[xAnchor,height] = desiredHeight;
     terrainDat.SetHeights(0,0,heights);
     heights[width,yAnchor] = desiredHeight;
     terrainDat.SetHeights(0,0,heights);
     heights[width,height] = desiredHeight;
     terrainDat.SetHeights(0,0,heights);
     
     for(var p = 0; p<=1400; p++){
     //print(width);
         //diamond
     heights[xAnchor+width/2,yAnchor+height/2] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(width,yAnchor)+terrainDat.GetHeight(xAnchor,height)+terrainDat.GetHeight(width,height))/4.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
     terrainDat.SetHeights(0,0,heights);
         //square
     heights[xAnchor+width/2,yAnchor] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(width,yAnchor))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
     terrainDat.SetHeights(0,0,heights);
     heights[xAnchor,yAnchor+height/2] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(xAnchor,height))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
     terrainDat.SetHeights(0,0,heights);
     heights[xAnchor+width/2,height] = (((terrainDat.GetHeight(width,height)+terrainDat.GetHeight(width,yAnchor))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
     terrainDat.SetHeights(0,0,heights);
     heights[width,yAnchor+height/2] = (((terrainDat.GetHeight(width,height)+terrainDat.GetHeight(xAnchor,height))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
     terrainDat.SetHeights(0,0,heights);
         //check next
     if(xAnchor+width >= 64 && yAnchor+height >=64){
         //print("y");
         width = width/2;
         height = height/2;
         step = width;
         xAnchor=0;
         yAnchor=0;
     }
     else{
         //print("n");
         
         if(xAnchor+width >=64){
             yAnchor = yAnchor + height;
             xAnchor = 0;
         }
         else{xAnchor = xAnchor + width;}
     }
     }
 }

alt text

unity.png (518.7 kB)
Comment
Add comment · Show 4
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 GameVortex · Jan 12, 2015 at 12:53 PM 0
Share

And do you have a question? A specific problem? A technical challenge? Anything related to Unity Answers at all?

avatar image Greenie · Jan 12, 2015 at 01:15 PM 0
Share

0 Yeah I suppose my question is what part of the algorithm am I getting wrong. It seems to do what others do? Just not as well

avatar image Owen-Reynolds · Jan 12, 2015 at 04:31 PM 0
Share

The thing is, Diamond square is just a generic terrain algorithm, and will plug right into the way Unity handles terrain. $$anonymous$$aking it work differently has nothing to do with Unity. The best place to look for answers is generic DiamondSquare resources.

avatar image Greenie · Jan 12, 2015 at 08:54 PM 0
Share

$$anonymous$$anaged to get it looking much better. $$anonymous$$y issue was the averages were being taken at the same place every time. Thanks for answers guys

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Greenie · Jan 12, 2015 at 01:04 PM

Well yes how can I change my code to make it more realistic?

Comment
Add comment · Show 1 · 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 GameVortex · Jan 12, 2015 at 01:11 PM 0
Share

That is not really within the scope of Unity Answers as we focus on specific technical questions. However it does seem like you have slightly misunderstood the concept for the Diamond Square algorithm. Read a little more about it here: http://www.paulboxley.com/blog/2011/03/terrain-generation-mark-one#

It looks like you have set the height on a terrain which has many more vertices than what your algorithm calculated on.

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

28 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

Related Questions

How to procedural generation 0 Answers

2D Procedural Terrain Generation 1 Answer

Procedural Terrain Generation on a Map the Scale of the Milky Way 2 Answers

World Generation with Interesting Terrain 0 Answers

3d Perlin Noise? 3 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