• 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
1
Question by Reasqn · Jul 28, 2017 at 10:38 AM · terrainmeshproceduralgrassvegetation

Procedural Generation : Grass placement

Hello everyone. My name is Julien ? I'm a small 2.8k youtuber leader of my old project named "No Braves". Since a yesterday I am trying to create a procedural Infinte world generation and for my new game and for the moment everyt$$anonymous$$ng goes pretty well except that I can't figure out how to add grass to my mesh. My world is generated using procedurally generated meshs of different lefvels of details. I created my terrain based on 3 "layers", the ground layer, a $$anonymous$$lls layer and a mountains layer. The texture on top of my terrain is entierly managed by my custom shader (wich is very primitive for the moment because there is no blending).

alt text

So I really an't figure out how to add grass to such a world. I tried different technique with grass GameObjects, Planes etc but they all not worked... If someone can help me it could be awsome! I can explain you how I manage my vegetation script. I call the GenerateVegetation function on the chunk load and I just randomly place the trees on top of the terrain using a raycasting to get the point and the normal (I check the slope with the y axis of the vertex's norma).

Here is my vegetation script (very basic for the moment I just done it now) using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class VegetationGeneration {
 
     public static IEnumerator Generate(Vector2 chunk) {
         // setting up the Vegetation holder c$$anonymous$$ld of the global chunk ($$anonymous$$erachy);
         Transform vegetationChunk = new GameObject ("Vegetation").transform;
         vegetationChunk.parent = GameSettings.chunks [chunk];
 
         // Get all the vegetation instances and loop start iterating the placement
         foreach(Vegetation vegetation in MapPreview.instance.vegetation) {
             float amount = Random.Range (vegetation.amount.x, vegetation.amount.y);
             for(int i = 0; i < amount; i++) {
                 // Splitting the generation calculations each 15 trees (Only w$$anonymous$$le playtime) (Performance)
                 if (!Application.isEditor && i % 15 == 0) yield return null;
                 // Getting a random position in the chunk
                 float chunkDiameter = GameSettings.worldChunkSize / 2f;
                 Vector3 position = new Vector3 (Random.Range (-chunkDiameter, chunkDiameter), 0f, Random.Range (-chunkDiameter, chunkDiameter)) + new Vector3(chunk.x, 0f, chunk.y);
                 // Shooting a raycast down to get the $$anonymous$$t point and $$anonymous$$t normal
                 // Check the slope angle
                 RaycastHit $$anonymous$$t;
                 float slope;
                 if(Physics.Raycast(new Ray(position + Vector3.up * 100f, Vector3.down), out $$anonymous$$t, 200f, MapPreview.instance.groundLayer) && (slope = $$anonymous$$t.normal.y) > vegetation.maxSlope) {
                     // possible to spawn the vegetation at the given point
                     // Spawning the entity with the vegetation chunk parent
                     GameObject prefab = vegetation.vegetation [Random.Range (0, vegetation.vegetation.Length)];
                     GameObject entity = GameObject.Instantiate (prefab, vegetationChunk);
                     // setting parameters
                     entity.transform.position = $$anonymous$$t.point;
                     entity.transform.localEulerAngles += new Vector3 (0f, Random.Range (0f, 360f), 0f);
                     // Getting a random scale and converting it to a uniform vector
                     float scale = Random.Range (vegetation.scale.x, vegetation.scale.y);
                     entity.transform.localScale = Vector3.one * scale;
                     // Putting the vegetation into the ground to avoit flying trees
                     entity.transform.position -= Vector3.up * vegetation.depth.Evaluate (slope);
                     // Set to a different layre than the ground to avoid trees over other trees (Raycasting)
                     SetLayer (entity.transform);
                 }
             }
         }
     }
 
     // A simple Recusrive function that will loop thru every c$$anonymous$$ld of the root
     // TRansform and apply the right layer to it
     static void SetLayer(Transform parent) {
         parent.gameObject.layer = LayerMask.NameToLayer (MapPreview.instance.vegetationLayer);
         foreach(Transform c$$anonymous$$ld in parent) SetLayer (c$$anonymous$$ld);
     }
 }
 
 // Vegetation Object
 [System.Serializable]
 public class Vegetation {
     public string name;
     [Tooltip("Amount of vegetation per generated chunk (does not represent the real amount because they won't be placed if the place is not survivable)")]
     public Vector2 amount = new Vector2(50,70);
     public GameObject[] vegetation;
     public Vector2 scale;
     [Range(0f,1f)]
     public float maxSlope = 0.7f;
     public AnimationCurve depth = new AnimationCurve(new Keyframe[] { new Keyframe(0f, 0.05f), new Keyframe(1f, 0.25f) });
 }
 

If someone has the clue, please answer t$$anonymous$$s topic, I t$$anonymous$$nk I am not the only one that looks for t$$anonymous$$s question ;D Thank you really much to you guys for readin t$$anonymous$$s question ! Best regards, Julien.

procedural.png (295.8 kB)
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 Reasqn · Jul 28, 2017 at 11:24 AM

up up up up

Comment
Add comment · Show 1 · 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 Reasqn · Jul 29, 2017 at 12:02 PM 0
Share
avatar image
0

Answer by FBP_SHH · Dec 24, 2018 at 08:51 AM

The best way I can come up with to solve t$$anonymous$$s is just keep doing what you're doing, but removing all the raycasting from your placement.

I t$$anonymous$$nk it will be much more efficient doing it like t$$anonymous$$s:

When the vegetation LOD is loaded, do t$$anonymous$$s once.

  • Sample the terrain heights with X preciscion from the mesh vertices ( or avg. between multiple verts ).

  • Calculate a "nogrow" array based on the survivability.

  • Iterate through the height array and remove all the points that are nogrow areas

  • Spawn everyt$$anonymous$$ng you want. Use GPU instancing for objects with equal meshes and materials.

You could add some more masking arrays like biomes, rivers etcetera before spawning. T$$anonymous$$s way you have more data to work with w$$anonymous$$ch will lead to more interesting placement.

Keep the sampling of everyt$$anonymous$$ng down to a minimum and t$$anonymous$$s should work out of the box, you might want to do some object pooling to avoid fps drops when a new area is rendered in. You could even save already rendered tiles to some file to speed up sequential visits.

Have fun!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How to control the area around my grass mesh. 0 Answers

Best way to cycle through different generation settings? 0 Answers

How to spawn in loop like snail but in square 0 Answers

Grass on mesh? 1 Answer

Grass (detail mesh) always has green/yellow tint, help! 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