• 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 chanfort · Apr 12, 2015 at 01:23 AM · terrainrandom

Evenly distributing trees on terrain

I was recently trying to write algorithm, which would distribute trees (and some other objects) evenly on the terrain. I used nearest neighbour checking conditions - if NN is more far away from the point, which I am checking, than critical distance, then put the tree there. If NN is more close than critical distance, don't place tree and re-roll random position point. This simple approach allows to distribute trees quite nicely.

However, than going to higher densities, failures (non-suitable points, which needs re-rolling) are increasing dramatically, or in other words it needs much longer times to cover terrain nicely. Are there some well known better algorithms, which would distribute points evenly (at least in Vector2 plane at the beginning)?

Comment

People who like this

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

Answer by Addyarb · Apr 12, 2015 at 03:06 AM

I wrote a script that I'll try to find. I used it for randomly generating any sort of forest (Dense or thin).

I attached a rigidbody with a very high mass (~2000) to the very bottom of the tree, and also attached a trigger collider (I used capsule but I'm sure box would work just as well for low density). The rigidbody keeps the tree upright, but also lets it take the angle of the terrain.

Then, I used a for loop to generate trees at a random location between 0,0 (or your chosen starting Vector2) and 100,100 (or your ending vector2). Now, I dropped the trees at a height of about 20 above the highest hill in my terrain. This may vary for you, but I'm sure you can come up with some code that will check the highest point and add 20.

I added a tree script that, after 10 seconds, checked to see if it was colliding with a couple of things. (Mainly rocks, other trees, water, etc.) If it was, it moved itself to the edge of the map and waited to be "respawned" (or in this case, repositioned). Provided it wasn't colliding with anything, I then set the rigidbody to Kinematic so that it didn't move. I also checked the rotation to make sure it was relatively upright. If not, I reset the position to (0,0,0).

I also added a terrain detection script which got the dominant texture underneath the tree. This would let the tree know what sort of soil it was under, which can either tell the tree script to reposition it (if it's on stone, gravel, places a tree wouldn't normally grow) or to change the tree type (swamp terrain? swamp tree. Forest terrain? Pine tree).

Hope this gives you some good ideas! It may not be the most efficient way, but it certainly worked for me, and this was all done in around 10 seconds while the scene loaded.

Comment

People who like this

0 Show 5 · 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 chanfort · Apr 12, 2015 at 01:21 PM 0
Share

How many trees you are loading in these 10 seconds?

avatar image Addyarb · Apr 12, 2015 at 04:15 PM 0
Share

About 200. If it's more than that I will put a .1/.2 second delay between spawns To decrease any lagging (even though it's happening when a loading screen is up).

avatar image chanfort · Apr 12, 2015 at 04:47 PM 0
Share

oh, that's very low number - I am getting like 10000 trees placed on terrain with nearest neighbours in like 10 - 20 seconds. But I think there should be even much faster ways, especially when trying to add more objects in already crowded areas.

avatar image Addyarb · Apr 12, 2015 at 04:50 PM 0
Share

You are looking for something like mass place trees at runtime then?

avatar image chanfort · Apr 12, 2015 at 05:50 PM 0
Share

something similar, but I also thinking to adopt it not just for trees, but for some manual objects on terrain placement. I would prefer algorithms, which would give just list of positions (Vector2 or Vector3), as everything else (including placement) I will do my way.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Invisible trees? 1 Answer

How to randomly spawn objects above the ground? 1 Answer

Procedural terrain tiles 0 Answers

Any Reason this would make Unity Freeze up? 0 Answers

Placing Buildings on Terrain 1 Answer


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