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 /
  • Help Room /
avatar image
0
Question by AdvsNoob · Aug 05, 2017 at 03:49 AM · scripting problemprogramminggenerationtreesperlin noise

Question about procedural forest/grass generation

Hello,

I have been looking into this topic for quite some time now and can't really find a solotion other then randomly placing objects in a specified area using Random.Range..

What I would like to do is generate plots for trees and grass to spawn using something like peril noise but I can't figure it out. Can someone just simply explain to me how you would generate a forest using something like peril noise similar to this example:

Map Example

Were all the dots are trees.

Current system I have just generates trees way to far apart and makes the world seem bare:

alt text

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 jonasmortensen · Aug 05, 2017 at 12:22 PM

This script generates a forest based on some noise texture. The noise texture can be though of as a "likelyness" map describing what the change of a tree spawning is at a given position. This can be controlled with the density variable. With a density of 1 the script produces this result:alt text

The script picks a random size based on two values. This can of course be changed as well as doing the same for rotation.

 using UnityEngine;
 
 public class ForestGenerator : MonoBehaviour {
 
     public GameObject tree;
     public float minTreeSize;
     public float maxTreeSize;
     public Texture2D noiseImage;
     public float forestSize;
     public float treeDensity;
 
     private float baseDensity = 5.0f;
 
     // Use this for initialization
     void Start () {
         Generate();
     }
 
     public void Generate() {
 
         for(int y = 0; y < forestSize; y++) {
 
             for(int x = 0; x < forestSize; x++) {
 
                 float chance = noiseImage.GetPixel(x, y).r / (baseDensity/treeDensity);
 
                 if (ShouldPlaceTree(chance)) {
                     float size = Random.Range(minTreeSize, maxTreeSize);
 
                     GameObject newTree = Instantiate(tree);
                     newTree.transform.localScale = Vector3.one * size;
                     newTree.transform.position = new Vector3(x, 0, y);
                     newTree.transform.parent = transform;
                 }
             }
         }
     }
 
     //Returns true or false given some chance from 0 to 1
     public bool ShouldPlaceTree(float chance) {
         if (Random.Range(0.0f, 1.0f) <= chance) {
             return true;
         }
         return false;
     }
 }
 

Hope it helps as inspiration ;)


forest.png (445.1 kB)
Comment
Add comment · Show 4 · 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 jonasmortensen · Aug 05, 2017 at 12:29 PM 0
Share

The texture used in the image is a simple googled perlin noise. You could do anything else that returns some float value from 0-1 given a position.

You might want to check out Coherent Noise. Havent played with it yet but i think it rocks.

$$anonymous$$anual/Documentation

Unity Package download

avatar image AdvsNoob · Aug 05, 2017 at 12:43 PM 0
Share

So for the size of the actual grid were the objects spawn at is that the forest size? Because I'm trying to get it to spawn within a specified area. But YES this really does help!

avatar image AdvsNoob AdvsNoob · Aug 05, 2017 at 12:52 PM 0
Share

Because when I try to set it to the map size (196) it just crashes haha.

avatar image jonasmortensen AdvsNoob · Aug 06, 2017 at 09:19 AM 0
Share

Yes that is the forest size.

Does it crash on lower sizes? I just tried the script on my computer on a 500 size forest. It had a terrible frame rate but did'nt crash. Try reducing the density.

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

157 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 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

Input Command Issues 0 Answers

Unity HUD Tracking and Target Selection 0 Answers

How do I detect UI Raycasts using the Vive controllers? 0 Answers

My gameobject stops moving after entering the trigger?? 2 Answers

Tile based river generation 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