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 ExtremePowers · Aug 09, 2014 at 06:18 PM · perlin noisesetheights

Procedural terrain

I have been working on an procedural terrain, which eventually is going to be infinite. I have run into a couple of problems. Some of which I can't figure out how to fix. The main issue right now is this: https://www.dropbox.com/s/utctinn94h8onk4/Untitled.png This is the current code:

 function GenerateTile(nw : int, nh : int) {
     if (GameObject.Find("Chunk "+nw+","+nh)) { 
         return;
     }
     var td = new TerrainData();
     td.heightmapResolution = 513;
     td.size = new Vector3(500,200,500);
 
     var ChunkPosX : float = td.size.x * nw;
     var ChunkPosY : float = td.size.z * nh;
 
     var t = Terrain.CreateTerrainGameObject(td);
     var hw : float = Terrain.activeTerrain.terrainData.heightmapWidth;
     var hh : float = Terrain.activeTerrain.terrainData.heightmapHeight;
     var heights = new float[hw, hh];
     for (var x = 0; x < hw; x++) {
         for (var y = 0; y < hh; y++) {
             //           World Pos Divided by heightmap height
             var xCoord = ((ChunkPosX + x) / (Mathf.PI * scale));
             var yCoord = ((ChunkPosY + y) / (Mathf.PI * scale));
             heights[x,y] += Mathf.PerlinNoise(xCoord, yCoord);
         }
     }
     Terrain.activeTerrain.terrainData.SetHeights(0,0,heights);
     t.transform.position = new Vector3(ChunkPosX, 0,ChunkPosY);
     t.name = "Chunk "+nw+","+nh;
 }


Comment
Add comment · Show 2
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 tanoshimi · Aug 09, 2014 at 06:55 PM 0
Share

The main issue right now is... What?

avatar image ExtremePowers · Aug 09, 2014 at 06:58 PM 0
Share

The terrains fit diagonally but not vertically, so It can't be used for tiling as the tiles doesn't create a smooth terrain.

1 Reply

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

Answer by Scribe · Aug 09, 2014 at 11:10 PM

your main problem seems to be that you use the terrain size to find the perlin start point however you should be using a multiple of your heightmap resolution, also I'm not quite certain why (I haven't done much work with terrain heightmaps) but for some reason I had to swap the position they were mapped to in the float list:

 function GenerateTile(nx : int, nz : int) {
     if (GameObject.Find("Chunk "+nx+","+nz)) { 
         return;
     }
     var td = new TerrainData();
     td.heightmapResolution = 513;
     td.size = new Vector3(500,200,500);
 
     var t = Terrain.CreateTerrainGameObject(td);
     var hw : float = Terrain.activeTerrain.terrainData.heightmapWidth;
 
     var ChunkPosX : float = td.size.x * nx;
     var ChunkPosZ : float = td.size.z * nz;
 
     var startPosX : int = hw*(nx-1);
     var startPosZ : int = hw*(nz-1);
 
     var heights = new float[hw, hw];
     for (var x = 0; x < hw; x++) {
         for (var z = 0; z < hw; z++) {
             var xCoord = ((startPosX + x)/scale);
             var zCoord = ((startPosZ + z)/scale);
             heights[z, x] = Mathf.PerlinNoise(xCoord, zCoord);
         }
     }
     Terrain.activeTerrain.terrainData.SetHeights(0,0,heights);
     t.transform.position = new Vector3(ChunkPosX, 0,ChunkPosZ);
     t.name = "Chunk "+nx+","+nz;
 }


Hope that helps!

Scribe

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 ExtremePowers · Aug 10, 2014 at 04:58 PM 0
Share

It did help alot, but there is still something wrong, the terrains seem to have a little offset something like 0.5 ingame offset. Here's a picture: https://www.dropbox.com/s/qxlufpcx707tbph/Untitled.png Note: The picture is taken from something about 2 in game units away, so it isn't something huge its just a little offset.

avatar image Scribe · Aug 10, 2014 at 09:04 PM 0
Share

Ahh I had stupidly thought that from terrain chunk to terrain chunk the perlin noise position would increase such as one ending at a relative position of 512 and the next one starting at a position of 513 but of course the edge height overlaps so you need to $$anonymous$$us 1 from the calculations using the resolution... so basically change line 15 and 16 a la:

 var startPosX : int = (hw-1)*(nx-1);
 var startPosZ : int = (hw-1)*(nz-1);

Also thanks for marking as answered, it is much appreciated by me and the site!

Scribe

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to use Perlin Noise to generate terrain tiles? 3 Answers

Which are the max Y position (height) of SetHeights and how to control it? 1 Answer

Why larger numbers in perlin noise results in smoother results 1 Answer

Procedural Island Terrain Generation 2 Answers

Why is perlin noise generating flat 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