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 Kirbsssys · Oct 24, 2021 at 07:38 PM · procedural generationperlin noise

How do I make biomes with perlin noise

I wanna know how I can change my perlin noise values or something at certain points to make changes in the environment to make biomes, like minecraft with the planes and hills, I wanna be able to do something like that. Here's the code.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class GenerateInfiniteWorld : MonoBehaviour
 {
 public GameObject player;
 public GameObject blockGameObject;

 public int worldSizeX;
 public int worldSizeZ;

 public float offsetX;
 public float offsetZ;

 public float noiseHeight;

 public float gridOffset;
 private Vector3 startPosition;

 private Hashtable blockContainer = new Hashtable();

 void Start()
 {

     offsetX = UnityEngine.Random.Range(1f, 2086040f);
     offsetZ = UnityEngine.Random.Range(1f, 2086040f);

     float upTime = Time.realtimeSinceStartup;


     for (int x = -worldSizeX - 15; x < worldSizeX + 15; x++)
     {
         for (int z = -worldSizeZ - 15; z < worldSizeZ + 15; z++)
         {
             Vector3 pos = new Vector3(x * 1 + startPosition.x, generateNoise(x + xPlayerLocation, z + zPlayerLocation, 8f) * noiseHeight, z * 1 + startPosition.z);

             string bName = $"Block-{((int)pos.x).ToString()},0,{((int)pos.z).ToString()}";

             GameObject block = Instantiate(blockGameObject, pos, Quaternion.identity) as GameObject;
             block.transform.SetParent(this.transform);
             block.name = bName;
             BlockData bData = new BlockData(block, upTime);
             blockContainer.Add(block, bData);
         }
     }
 }

 private void Update()
 {
     if (Mathf.Abs(xPlayerMove) >= 1 || Mathf.Abs(zPlayerMove) >= 1)
     {
         float upTime = Time.realtimeSinceStartup;
         for (int x = -worldSizeX; x < worldSizeX * 1.733; x++)
         {
             for (int z = -worldSizeZ; z < worldSizeZ * 1.733; z++)
             {
                 Vector3 pos = new Vector3(x * 1 + (xPlayerLocation - 10), generateNoise(x + xPlayerLocation, z + zPlayerLocation, 8f) * noiseHeight, z * 1 + (zPlayerLocation - 10));

                 string bName = $"Block-{((int)pos.x).ToString()},0,{((int)pos.z).ToString()}";

                 if (!blockContainer.ContainsKey(bName))
                 {
                     GameObject block = Instantiate(blockGameObject, pos, Quaternion.identity) as GameObject;
                     block.transform.SetParent(this.transform);
                     block.name = bName;
                     BlockData bData = new BlockData(block, upTime);
                     blockContainer.Add(block, bData);
                 }
                 else
                     ((BlockData)blockContainer[bName]).upTime = upTime;
             }
         }
         Hashtable newBlockContainer = new Hashtable();
         foreach (BlockData bData in blockContainer.Values)
         {
             if (Math.Abs(bData.upTime - upTime) > 0.0)
                 Destroy(bData.blockObj);
             else
                 newBlockContainer.Add(bData.blockObj.name, bData);
         }
         blockContainer = newBlockContainer;
         startPosition = player.transform.position;
     }

 }

 public int xPlayerMove
 {
     get
     {
         return (int)(player.transform.position.x - startPosition.x);
     }
 }

 public int zPlayerMove
 {
     get
     {
         return (int)(player.transform.position.z - startPosition.z);
     }
 }
 public int xPlayerLocation
 {
     get
     {
         return (int)Mathf.Floor(player.transform.position.x);
     }
 }

 public int zPlayerLocation
 {
     get
     {
         return (int)Mathf.Floor(player.transform.position.z);
     }
 }

 private float generateNoise(int x, int z, float detailScale)
 {
     float xNoise = (x + this.transform.position.x) / detailScale;
     float zNoise = (z + this.transform.position.y) / detailScale;

     return Mathf.PerlinNoise(xNoise + offsetX, zNoise + offsetZ);
 }
 }

 class BlockData
 {
     public GameObject blockObj;
     public float upTime;

 public BlockData(GameObject blockObj, float upTime)
 {
     this.blockObj = blockObj;
     this.upTime = upTime;
  }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Oct 25, 2021 at 11:01 AM

You could use a large noise map to generate the location of the biomes. This could be a large scaled perlin noise, or even Voronoi noise, which is used by minecraft for regions and rivers I believe.


If you were using simple perlin noise, generate a large noise map first, and if the values are between a certain range, use a smaller perlin noise of your choise for that region. For example, where noise is of high values, you can have mountainous/snowy biomes, with much more hilly biomes, whereas areas of low noise could be desert/grass biomes, with mostly smooth and low-lying land.

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 Kirbsssys · Oct 26, 2021 at 02:19 AM 0
Share

Only problem, I'm not smart and have no clue how to do that

avatar image Llama_w_2Ls Kirbsssys · Oct 26, 2021 at 08:46 AM 0
Share

Create a class called noise, with fields such as scale, amplitude, height etc. These control what the biome looks like. Create multiple instances of these with differing scales and heights etc. so that they look different. You could also assign different blocks to the noise classes to make them separate biomes.


Then, use one noise instance as the biome map. Make the scale enormous, and whenever the noise is within a range, choose one of the noise biomes to use in that area. This will make your map consist of multiple biomes. Here's some code:

 [System.Serializable]
 public class Noise
 {
      public float Scale;
      public float Amplitude;
      public float Height;
 
      public float Sample2D(float x, float y)
      {
           float noiseValue = Mathf.PerlinNoise(x / Scale, y / Scale);
            return noiseValue * Amplitude + Height;
      }
 }



Create multiple instances of Noise class in the editor and set their parameters

 public class WorldGen : MonoBehaviour
 {
      public Noise BiomeNoise; // Main noise, make it have a large scale
 
      public Noise GrassNoise;
      public Noise MountainNoise;
      
      void Start()
      {
           var blocks = YOUR_BLOCKS; // Get this per chunk maybe
 
           foreach (var block in blocks)
           {
                  float biome = BiomeNoise.Sample2D(block.pos.x, block.pos.y);
                  
                  if (biome < GrassThreshold)
                      // Sample from Grass Noise
                      // GrassThreshold = 2f for example
 
                 else
                     // Sample from Mountain Noise
            }
      }
 }

@Kirbsssys

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

131 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Procedural Generation in Unity 0 Answers

Adding rivers to procedural generated 2D map 1 Answer

Unexpected period length with Unity Mathematics pnoise method 0 Answers

Better perlin noise map generation. 0 Answers

Strange texture2d behaviour 0 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