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 /
avatar image
0
Question by kamon_241 · Jan 20, 2016 at 01:21 AM · c#perlin noiseterrain gen

Difficulty with Perlin Noise based Terrain Generation

First off I know that this sort of question has been asked 101 times already on here, but I've honestly read a good 80% of them, and none offer me anymore information than I currently have.

I'm prototyping out a game which requires a really simple low-res terrain, and I eventually stumbled onto a few Perlin Noice tutorials. This is the function I have currently:

public void GenerateTerrain(float tileSize){

     TerrainData TD = selfTerrain.terrainData;
     //Higher = More Hills/Mountains
     float HM = Random.Range(0,10);
     //Lower = Higher Mountains/Hills
     float divRange = Random.Range(12,25);
     int height = TD.heightmapHeight;
     int width = TD.heightmapWidth;
 
     float[,] heights =  new float[width,height];

     for(int i = 0; i < width; i++){
         for(int k = 0; k < height; k++){
             heights[i,k] = Mathf.PerlinNoise((i/width) * tileSize, (k/height) * tileSize) / divRange;
         }
     }
     Debug.Log("DivRange: " + divRange + " , " + "HTiling: " + HM);
     TD.SetHeights(0,0,heights);
 }

but instead of making me some nice ripply terrain it applies the same height to the whole terrain object, essentially just moving it up and down. The happens event when I use Random.Range to just put a random height onto every location in the heightmap.

If its something stupid, please don't rub it in, I've been up for probably a bit too long.

Cheers in Advance

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

2 Replies

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

Answer by incorrect · Jan 20, 2016 at 02:38 AM

Actually the problem is that maths for integers and floats work differently. Dividing int by int will not get you a float you need. Keep it in mind next time.

 heights[i,k] = Mathf.PerlinNoise((1f*i/width) * tileSize, (1f*k/height) * tileSize) / divRange;

This will work like expected. Multiplying and int by 1f (float 1) will make it a float and you will get your expected result. Enjoy your noises! :)

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 incorrect · Jan 20, 2016 at 02:40 AM 0
Share

And here is a bit of info about division. General program$$anonymous$$g is boring, but necessary.

avatar image
0

Answer by cjdev · Jan 20, 2016 at 01:26 AM

I'm assuming tileSize is an int here, but the problem is likely that Perlin Noise doesn't work when it's input is a perfect whole number, an integer. Try multiplying your x and y by some random float value.

Comment
Add comment · Show 2 · 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 incorrect · Jan 20, 2016 at 02:34 AM 0
Share

It's close. ;)

avatar image cjdev · Jan 20, 2016 at 03:33 AM 1
Share

It works as you expect with the float because of floating point imprecision, which is why it's no longer a perfect whole number. The formula for Perlin Noise is using a modulo operation of 1 with the float value coordinates where the remainder is also used to provide the "seed" for a semi-random lookup table.

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

perlin noise terrain is spikey 0 Answers

Multiple Cars not working 1 Answer

Perlin noise gives the same result every time 1 Answer

Distribute terrain in zones 3 Answers

Make a square-based 2d terrain generator 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