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 Vinnie420 · May 28, 2016 at 05:08 AM · unity 5perlin noiserandomspawningmap-generationperlin

improve generate tilemap with perlin noise

Hi, I am fiddling with perlinnoise to generate a tilemap using the following code: i want the map to contain mostly green tiles, and blobs here and there of resources iron , copper etc...

 using UnityEngine;
 using System.Collections;
 
 public class SpawnGrid : MonoBehaviour 
 {
     [Range(0.1f,100f)]
     public float seedX, seedY;
 
     public GameObject gridTile;
     public float gridX, gridY, perlinThresholdiron, perlinThresholdcoal, perlinThresholdcopper, perlinThresholdstone;
 
     void Start () 
     {
         for (float x = -gridX; x < gridX; x += 0.1f) 
         {
             for (float y = -gridY; y < gridY; y += 0.1f) 
             {
                 Vector3 spawnPos = new Vector3(x*10,y*10,0);
                 GameObject tile = Instantiate(gridTile,spawnPos,Quaternion.identity) as GameObject;
 
                 SpriteRenderer rend =             tile.GetComponent<SpriteRenderer>();
                 TileController tileController = tile.GetComponent<TileController>();
 
                 float perlinNum = Mathf.PerlinNoise(x/seedX, y/seedY); 
 
                 if (perlinNum < perlinThresholdiron) 
                 {
                     rend.color = Color.gray;
                     tileController.isResource = true;
                     tileController.tileType = ResourceType.iron;
 
                 }
                 else if (perlinNum > perlinThresholdiron && perlinNum < perlinThresholdcoal) 
                 {
                     rend.color = Color.black;
                     tileController.isResource = true;
                     tileController.tileType = ResourceType.coal;
                 }
                 else if (perlinNum > perlinThresholdcoal && perlinNum < perlinThresholdcopper) 
                 {
                     rend.color = Color.yellow;
                     tileController.isResource = true;
                     tileController.tileType = ResourceType.copper;
                 }
                 else if (perlinNum > perlinThresholdcopper && perlinNum < perlinThresholdstone) 
                 {
                     rend.color = Color.blue;
                     tileController.isResource = true;
                     tileController.tileType = ResourceType.stone;
                 }
                 else 
                 {
                     rend.color = Color.green;
                 }
             }
         }
     }
 }

I can move the seedX and Seedy sliders around to genrate different patterns, but they are always very repetitive and the resources are always "surrounding" eachother: alt text how can i change this code so that it generates blobs of resources of random size/shape (think factorio)

hope you guys understand what im trying to do

screen.jpg (220.8 kB)
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 AlucardJay · May 28, 2016 at 06:44 AM 1
Share

As you have found, perlin is mirrored around the origin. Add an offset to x and y to get values away from the origin, eg:

 float perlinNum = $$anonymous$$athf.PerlinNoise( (x+offset) / seedX, (y+offset) / seedY ); 

$$anonymous$$ake the offset larger than gridX/gridY and the mirroring will go away.

For separating the ores, consider sampling separate perlin with different seeds, eg:

 float perlinCopper = $$anonymous$$athf.PerlinNoise( (x+offset) / seedCopperX, (y+offset) / seedCopperY ); 
 float perlinIron = $$anonymous$$athf.PerlinNoise( (x+offset) / seedIronX, (y+offset) / seedIronY ); 

Then say if iron is more common than copper :

 if ( perlinIron > perlinThresholdiron )
 {
     // do iron
 }
 else if ( perlinCopper > perlinThresholdcopper )
 {
     // do copper
 }

How you manage what resource is more do$$anonymous$$ant is a design choice that is up to you.

avatar image Vinnie420 AlucardJay · Jun 01, 2016 at 11:25 AM 0
Share

thanks man! I ended up modifiying my code to your example and it works beautifully for my needs!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · May 28, 2016 at 06:46 AM

Perlin Noise and most noise generating functions are very repetitious by nature. However, in order to get the random-looking patterns Perlin Noise is known for (the "clouds" effect) you'll need to use Fractional Brownian Noise, which is a long name for adding Perlin a bunch of times, each time with a higher frequency and lower amplitude. This answer has a lot of good info and resources on Perlin Noise in general if you're interested in learning more about it and FBN.

For your application you can sum together several functions of PerlinNoise (to taste, it will take some experimentation) but as you have it now I believe you've got the use of the function wrong. Mathf.PerlinNoise gets the x and y value of a 2d perlin noise result. To modify the amplitude of that you'd just multiply a constant times the function as a whole, as if you were changing the 'height'. For the frequency you'd multiply x and y by a constant, in effect determining how close successive values are to each other. And for the random seed you would add a constant to each x and y based on your seed to offset the 2d lookup for each unique value. So it looks like this:

Perlin Noise = Amplitude * Mathf.PerlinNoise(Frequency * x + Seed, Frequency * y + Seed)

In short, sum up a bunch of the above with increasing frequency and decreasing amplitude and you should see something like the effect you're after. As an aside, note that Perlin Noise is not the most efficient method of random generation so if performance is a factor you might want to consider a custom implementation over Unity's function.

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

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 Implementation 2 Answers

How to use Perlin Noise for waves 0 Answers

Adding rivers to procedural generated 2D map 1 Answer

2d Top-Down Perlin noise map using Mathf.PerlinNoise 3 Answers

Perlin noise not being changed runtime 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