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 PinkAnigav · Jun 27, 2017 at 02:03 PM · 2dperlin noisegenerator

Perlin Noise world generator, instantiating smaller tiles together.

I'm referring to this YouTube video of Arend Peter Teaches: Coding Perlin Noise in Unity

In the tutorial, he uses 100x100 tile to generate the world, but I would like to use a real Terraria tile which is 16x16, if I just fit in the 16x16 .png tiles to the prefab, the world generates the prefabs with lots of gaps in between.

I'm attaching the 2 scripts used in the video:

 using UnityEngine;
 using System.Collections;
 
 public class Generator : MonoBehaviour {
     
     public GameObject dirtPrefab; 
     public GameObject grassPrefab; 
     
     int minX = -160; // affects -x length of world generation
     int maxX = 160; // affects x length of world generation
     int minY = -10; // affects -y height of world generation
     int maxY = 10;  // affects y height of world generation
     
     PerlinNoise noise;
     
     void Start ()
     {
         noise = new PerlinNoise(Random.Range(1000000,10000000));
         Regenerate();
     }
     
     private void Regenerate()
     {
         float width = dirtPrefab.transform.lossyScale.x;
         float height = dirtPrefab.transform.lossyScale.y;
         
         for (int i = minX; i < maxX; i++)
             {
             //columns (x values
             int columnHeight = 2 + noise.getNoise(i - minX, maxY - minY - 2);
 
             for(int j = minY; j < minY + columnHeight; j++)
                 {
                 //rows (y values)
                 GameObject block = (j == minY + columnHeight - 1)?grassPrefab:dirtPrefab;
 
                 Instantiate(block, new Vector2(i * width, j * height), Quaternion.identity);
                 }
             }
     }
 }

and

 using UnityEngine;
 using System.Collections;
 
 public class PerlinNoise {
     
     long seed;
     
     public PerlinNoise(long seed)
     {
         this.seed = seed;
     }
     
     private int random(long x, int range)
     {
         return (int)(((x+seed)^5) % range);
     }
     
     public int getNoise(int x, int range)
     {
         int chunkSize = 16; //affects surface smoothness
         
         float noise = 0; //affects height of generation
         
         range /= 1;
         
         while(chunkSize > 5)
         {
             int chunkIndex = x / chunkSize;
             
             float prog = (x % chunkSize) / (chunkSize * 1f);
             
             float left_random = random(chunkIndex, range);
             float right_random = random(chunkIndex + 1, range);
             
             
             noise += (1-prog) * left_random + prog * right_random;
             
             chunkSize /= 2;
             range /= 2;
             
             range = Mathf.Max(1,range);
         }
         
         return (int)Mathf.Round(noise);
     }
 }
 


What should I be looking to change within the codes?

And a brief explanation as to why would be very much appreciated!

Thank you in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by brahini98 · Dec 31, 2017 at 04:58 PM

como hago si quiero que genere piedra debajo de la tierra ,amigo quiero añadirle otra capa debajo de piedra, q podria hacer

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
0

Answer by Zodiaq · Apr 04, 2018 at 09:15 PM

I know this is old but i answer just in case someone checks. i++ and j++ are the steps for the next tile, it adds 1 every time which is the size of the tile used in the video(100x100). Yours is 16x16 means 0.16 (float) each, same for height and width so it becomes like this:

 private void Regenerate()
     {
         float width = 0.16f;
         float height = 0.16f;
 
         for (float i = minX; i < maxX; i += 0.16f)
         {
             //columns (x values
             int columnHeight = noise.getNoise((int)i - minX, maxY - minY);
 
             for (float j = minY; j < minY + columnHeight; j +=0.16f)
             {
                 //rows (y values)
                 GameObject block = (j == minY + columnHeight - 1) ? grassPrefab : dirtPrefab;
 
                 Instantiate(block, new Vector2(i * width, j * height), Quaternion.identity);
             }
         }
     }
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

121 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

Related Questions

[C#] Wondering what is amiss with my 1D Perlin Noise Terrain Generator? 2 Answers

Making a 2D random cave generator. No errors in script or console, but nothing happens. Can anyone tell me why? 2 Answers

2D Efficient Realtime Terrain Generation 0 Answers

Advice on 2D Surface Terrain Generation Using Noise 1 Answer

Procedural terrain- of the 2D circular variety. 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