• 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
Question by 4t0m1c · May 07, 2015 at 08:05 PM · c#terrainperlin noiseprocedural-generation

Procedural Island Terrain Generation

Edit: Rewrote my question after trying a few things and made it more specific.

Hi, so I'm creating a mobile RTS game with procedurally generated maps. I've worked out how to create a terrain with a basic perlin noise on it, and tried to integrate http://gamedev.stackexchange.com/questions/54276/a-simple-method-to-create-island-map-mask method to creating an island procedurally. This is the result so far:

alt text

As you can see, the edges are great but the main area of the island is completely maxed at 1 and completely un-random in shape.

The image below from http://www-cs-students.stanford.edu/~amitp/game-programming/polygon-map-generation/ shows the kind of terrain I'm after. The tutorial there is great but would be too intensive, thus the post.

I want the Random Shaped island with Perlin noise generated land mass, surrounded by water.

This is the kind of shaping I want

edit: I've narrowed the problem down to the somewhere in the last 3 functions and I don't quite understand it enough to figure it out.

Here is my code. A script attached to a null with a button to activate Begin():

 using UnityEngine;
 using System.Collections;
 using System.Runtime.InteropServices;
 
 public class Gen_Perlin : MonoBehaviour {
 
     public float Tiling = 3.0f;
     public float min = 100000.0f;
     public float max = -100000.0f;
     private bool active = false;
     public int mapHeight = 50;
     
     public void Begin()
     {
         if (active == false) {
             TerrainData terrainData = new TerrainData ();
             const int size = 513;
             terrainData.heightmapResolution = size;
             terrainData.size = new Vector3 (2000, mapHeight, 2000);
         
             terrainData.heightmapResolution = 513;
             terrainData.baseMapResolution = 1024;
             terrainData.SetDetailResolution (1024, 1024);
 
             Terrain.CreateTerrainGameObject (terrainData);
             GameObject obj = GameObject.Find ("Terrain");
             obj.transform.parent = this.transform;
 
             if (obj.GetComponent<Terrain> ()) {
                 Debug.Log ("Gen_Begin- " + obj);
                 GenerateHeights (obj.GetComponent<Terrain> (), Tiling);
             }
         } else {
             GameObject obj = GameObject.Find ("Terrain");
             if (obj.GetComponent<Terrain> ()) {
                 Debug.Log ("Gen_Begin- " + obj);
                 GenerateHeights (obj.GetComponent<Terrain> (), Tiling);
             }
         }
     }
     
     public void GenerateHeights(Terrain terrain, float tileSize)
     {
         Debug.Log ("Start_Height_Gen");
         float[,] heights = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight];
         
         for (int i = 0; i < terrain.terrainData.heightmapWidth; i++)
         {
             for (int k = 0; k < terrain.terrainData.heightmapHeight; k++)
             {
                 heights[i, k] = Mathf.PerlinNoise(((float)i / (float)terrain.terrainData.heightmapWidth) * tileSize, ((float)k / (float)terrain.terrainData.heightmapHeight) * tileSize);
                 if (heights[i, k] > max) max = heights[i, k];
                 if (heights[i, k] < min) min = heights[i, k];
                 heights[i, k] = makeMask( terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight, i, k, heights[i, k] );
             }
         }
 
         Debug.Log ("Set_Height");
         terrain.terrainData.SetHeights(0, 0, heights);
         Debug.Log ("Done");
         //this.gameObject.AddComponent <Gen_Prefabs>();
     }
 
     public static float makeMask( int width, int height, int posX, int posY, float oldValue ) {
         int minVal = ( ( ( height + width ) / 2 ) / 100 * 2 );
         int maxVal = ( ( ( height + width ) / 2 ) / 100 * 20 );
         if( getDistanceToEdge( posX, posY, width, height ) <= minVal ) {
             return 0;
         } else if( getDistanceToEdge( posX, posY, width, height ) >= maxVal ) {
             return 1;
         } else {
             float factor = getFactor( getDistanceToEdge( posX, posY, width, height ), minVal, maxVal );
             return ( oldValue + oldValue ) * factor;
         }
     }
     
     private static float getFactor( int val, int min, int max ) {
         int full = max - min;
         int part = val - min;
         float factor = (float)part / (float)full;
         return factor;
     }
     
     public static int getDistanceToEdge( int x, int y, int width, int height ) {
         int[] distances = new int[]{ y, x, ( width - x ), ( height - y ) };
         int min = distances[ 0 ];
         foreach( var val in distances ) {
             if( val < min ) {
                 min = val;
             }
         }
         return min;
     }
 }
 
terra-gen-problem.jpg (71.4 kB)
Comment

People who like this

0 Show 9
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 siaran · May 07, 2015 at 08:45 PM 0
Share

It's not clear on exactly what you are stuck. The links you have in your question are answered, with code samples, that are in c#. Please be a bit more specific.

avatar image Surprisejedi · May 07, 2015 at 08:46 PM 0
Share

Can you show us what your code is? Have you attempted to even try writing code to fix this?

avatar image 4t0m1c · May 07, 2015 at 08:52 PM 0
Share

Yes sorry about that, I cannot figure out how to implement the segments of code from the examples.. There are 3 functions and one bit I'm not sure where that fits.. I tried searching then for ways to apply a mask to a height map with no solution.. So I'm stuck. Any workaround or context of how to implement the sampled code would be greatly appreciated.

avatar image siaran · May 07, 2015 at 09:07 PM 0
Share

considering the code in those links is in c#, you should be able to just directly copy most of it?

and what do you mean with applying a mask to a height map? Set all heights outside (or would that be inside?) the mask area to a predetermined value?

avatar image siaran · May 08, 2015 at 02:13 PM 1
Share

instead of setting your height to what makeMask returns, multiply the current height with that result.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by siaran · May 07, 2015 at 09:34 PM

from what I can tell, I think (not sure) that the normalisation loop he's talking about is just part of the code he's written to generate his perlin noise for him, and he's putting his makemask method inside that loop so that he doesn't need to go through it again.

As for applying a mask to a heightmap, that seems quite simple then:

(assuming your mask has the same dimensions as your heightmap, which it really should or it doesn't make sense)

just loop through the indices of your heightmap, and check your mask at those same indices (same size so they exist), and if that part of your height mask is the 'black' part of it you set the heightmap to 0 and if it's not you leave it as it is. Looking at his code that's actually basically what the MakeMask method does, except it uses a factor to slightly change the height at the 'edges' of the generated mask.

Comment

People who like this

0 Show 0 · 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

Answer by 4t0m1c · May 08, 2015 at 11:50 PM

So thanks to @siaran, the working version of the basic generation with 1st phase Perlin noise is as follows:

alt text

 using UnityEngine;
 using System.Collections;
 using System.Runtime.InteropServices;

 public class Gen_Perlin : MonoBehaviour {

 public float Tiling = 0.5f;
 private bool active = false;
 public int mapHeight = 10;
 
 public void Begin()
 {
     if (active == false) {
         TerrainData terrainData = new TerrainData ();
         const int size = 513;
         terrainData.heightmapResolution = size;
         terrainData.size = new Vector3 (2000, mapHeight, 2000);
     
         terrainData.heightmapResolution = 513;
         terrainData.baseMapResolution = 1024;
         terrainData.SetDetailResolution (1024, 1024);

         Terrain.CreateTerrainGameObject (terrainData);
         GameObject obj = GameObject.Find ("Terrain");
         obj.transform.parent = this.transform;

         if (obj.GetComponent<Terrain> ()) {
             GenerateHeights (obj.GetComponent<Terrain> (), Tiling);
         }
     } else {
         GameObject obj = GameObject.Find ("Terrain");
         if (obj.GetComponent<Terrain> ()) {
             GenerateHeights (obj.GetComponent<Terrain> (), Tiling);
         }
     }
 }

 public void GenerateHeights(Terrain terrain, float tileSize)
 {
     Debug.Log ("Start_Height_Gen");
     float[,] heights = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight];
     
     for (int i = 0; i < terrain.terrainData.heightmapWidth; i++)
     {
         for (int k = 0; k < terrain.terrainData.heightmapHeight; k++)
         {
             heights[i, k] = 0.25f + Mathf.PerlinNoise(((float)i / (float)terrain.terrainData.heightmapWidth) * tileSize, ((float)k / (float)terrain.terrainData.heightmapHeight) * tileSize);
             heights[i, k] *= makeMask( terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight, i, k, heights[i, k] );
         }
     }
     terrain.terrainData.SetHeights(0, 0, heights);
 }

 public static float makeMask( int width, int height, int posX, int posY, float oldValue ) {
     int minVal = ( ( ( height + width ) / 2 ) / 100 * 2 );
     int maxVal = ( ( ( height + width ) / 2 ) / 100 * 10 );
     if( getDistanceToEdge( posX, posY, width, height ) <= minVal ) {
         return 0;
     } else if( getDistanceToEdge( posX, posY, width, height ) >= maxVal ) {
         return oldValue;
     } else {
         float factor = getFactor( getDistanceToEdge( posX, posY, width, height ), minVal, maxVal );
         return oldValue * factor;
     }
 }
 
 private static float getFactor( int val, int min, int max ) {
     int full = max - min;
     int part = val - min;
     float factor = (float)part / (float)full;
     return factor;
 }
 
 public static int getDistanceToEdge( int x, int y, int width, int height ) {
     int[] distances = new int[]{ y, x, ( width - x ), ( height - y ) };
     int min = distances[ 0 ];
     foreach( var val in distances ) {
         if( val < min ) {
             min = val;
         }
     }
     return min;
 }

}


terra-gen-fix.jpg (82.8 kB)
Comment

People who like this

0 Show 0 · 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

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

21 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

Related Questions

Distribute terrain in zones 3 Answers

Procedual Terrain 1 Answer

Attempting terrain generation 1 Answer

perlin noise terrain is spikey 0 Answers

[C#] Wondering what is amiss with my 1D Perlin Noise Terrain Generator? 2 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