• 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 YoshiMad08 · Mar 07, 2013 at 07:20 PM · terrainalgorithmgenerator

How can I make my algorithm make cloudy heightmaps?

My game relies heavily on my terrain generation algorithm. The only problem is, is that my terrain is jagged. Suddenly, a mountain starts. And then it is perfectly flat. I want flat ground, but INTERESTING flat ground. and my mountain aren't mountainous. The heightmap is below.

alt text

Seriously blocky. My algorithm is this. (Yes, I've seen a thread with an algorithm JUST like this one. I did not steal. Hand on heart.)

 function Start () {
     var res = 513;
     var tData = GetComponent(Terrain).terrainData;
     tData.heightmapResolution = res;
     var heights = new float[res, res];
     for (var i = 0; i < res*res; i++) heights[i%res, i/res] = Random.Range(.51, .519);
     tData.SetHeights (0, 0, heights);
     var flat = new float[50, 50];
     for (i = 0; i < 50*50; i++) flat[i%50, i/50] = .5;
     for (i = 0; i < 75; i++) {
         tData.SetHeights (Random.Range(0, res-50), Random.Range(0, res-50), flat);
     }
 }

Yes, this is my first game. Any support would be delightful. Thanks, -YoshiMad08

Comment
Add comment · Show 3
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 robertbu · Mar 07, 2013 at 08:39 PM 0
Share

I kind of see what you want, but a picture would be very helpful. The first thing that comes to $$anonymous$$d is to use an image to create the map, but that may not work if you want to dynamically generate these maps during the game. As a point of comparison, how does this image compare to what you want?

alt text

terrain.jpg (29.2 kB)
avatar image YoshiMad08 · Mar 11, 2013 at 06:08 PM 0
Share

Hmm... Well, I think I can whip up a photo in a second, and if that picture had a tad more... turbulence(?), it would be more suitable. I don't want it generated in-game, we aren't talking $$anonymous$$inecraft here. :) I just want randomly generated terrain at runtime.

avatar image YoshiMad08 · Apr 05, 2013 at 10:25 AM 0
Share

Ah, I think I know what I need. Heightmap interpolation. Anyone know how to do that in CODE please? Javascript is prefered. Thanks :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by AlucardJay · Apr 05, 2013 at 04:07 PM

The reason you have jaggies is because you are using such a large range for each sample. Perlin Noise interpolates.

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.PerlinNoise.html

Here is a script that takes two samples of perlin, then combines them.

This modifies terrain, so be sure to use a New Terrain. I won't be held responsible for lost terrain.

Step 1 : Backup your terrain ! Right click it, and hit Export Package...

Create a new scene, create a New Terrain, attach this script to the terrain or an empty gameObject.

Hit play, then Left-Mouse-Click to add noise to the terrain.

Modify the values of one of the octaves, then click LMB.

Then start playing around with the offsets and click LMB.

 // April 6th 2013
 // TwoSamplePerlinTerrain.js by Jay Kay (Alucard Jay)
 #pragma strict
 
 public var terrain : Terrain;
 private var terrainData : TerrainData;
 private var heightmapWidth : int;
 private var heightmapHeight : int;
 
 public var sampleOneOctave : float = 2.0;
 public var sampleTwoOctave : float = 5.0;
 
 public var sampleOneOffset : Vector2 = Vector2.zero;
 public var sampleTwoOffset : Vector2 = Vector2.zero;
 
 function Start() 
 {
     if ( !terrain )
     {
         terrain = Terrain.activeTerrain;
     }
     
     terrainData = terrain.terrainData;
     
     heightmapWidth = terrain.terrainData.heightmapWidth;
     heightmapHeight = terrain.terrainData.heightmapHeight;
 }
 
 function Update() 
 {
     if ( Input.GetMouseButtonDown(0) )
     {
         GeneratePerlinTerrain();
     }
 }
 
 function GeneratePerlinTerrain() 
 {
     var heightmapData : float[,] = terrainData.GetHeights( 0, 0, heightmapWidth, heightmapHeight );
     
     for ( var y : int = 0; y < heightmapHeight; y ++ )
     {
         for ( var x : int = 0; x < heightmapWidth; x ++ )
         {
             var perlinSampleOne : Vector2 = new Vector2( ( ( sampleOneOctave / parseFloat( heightmapWidth ) ) * parseFloat( x ) ) + sampleOneOffset.x, ( ( sampleOneOctave / parseFloat( heightmapHeight ) ) * parseFloat( y ) ) + sampleOneOffset.y );
             var perlinHeightOne : float = Mathf.PerlinNoise( perlinSampleOne.x, perlinSampleOne.y );
             
             var perlinSampleTwo : Vector2 = new Vector2( ( ( sampleTwoOctave / parseFloat( heightmapWidth ) ) * parseFloat( x ) ) + sampleTwoOffset.x, ( ( sampleTwoOctave / parseFloat( heightmapHeight ) ) * parseFloat( y ) ) + sampleTwoOffset.y );
             var perlinHeightTwo : float = Mathf.PerlinNoise( perlinSampleTwo.x, perlinSampleTwo.y );
             
             heightmapData[y,x] = ( perlinHeightOne + perlinHeightTwo ) * 0.5;
         }
     }
     
     terrainData.SetHeights( 0, 0, heightmapData );
 }

There are more examples out there. Some good suggestions on the answer to this question : http://answers.unity3d.com/questions/340971/is-it-possible-to-do-this-for-a-random-terrain-app.html

Also there is a script in the Unity Wiki.

Comment
Add comment · 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
1

Answer by janzdott · Apr 05, 2013 at 04:43 PM

I agree with alucardj. You should use perlin noise. It works very well for terrain generation. I don't like Mathf.PerlinNoise. it never worked for me. I wrote a perlin noise algorithm that I can send you if you'd like. It works very well

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 YoshiMad08 · May 16, 2013 at 01:59 PM 0
Share

Thanks to all who have answered/commented on this question! About the perlin noise... yes please. :3 You have all helped me a lot - thanks. :D

avatar image YoshiMad08 · May 17, 2013 at 05:53 PM 0
Share

SUCCESS! alucardj thanks so freakin' much! I now have a pretty smooth terrain generator, and I'm making a main menu where you can change the Perlin Octaves, but i'll just call it Seeds 1/2. :D Thanks so much everyone :D

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

terrain generator (looking for a product) 2 Answers

Why won't my terrain load. 0 Answers

Mass terrain generation 1 Answer

Anyone got a Voxel generator I can use? 0 Answers

Code Loops Indefinitely 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