• 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 ExtremePowers · Oct 19, 2014 at 07:30 PM · instantiatemeshlaggrassbillboard

Efficient mesh instantiation

Is there a effecient way to place 10000 meshes on a terrain, without making the game lag. This is because I want to make a terrain with grass on it, so I would need to instantiate around 10000 meshes for a decent look. Note: The terrain is a mesh and is generated at run-time so I can't use the built-in tools..

Comment
Add comment · Show 12
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 Wisearn · Oct 20, 2014 at 09:53 AM 0
Share

You can place them out over the span of several frames ins$$anonymous$$d of trying to create them all in the same frame.

For example, with a coroutine you can use a while loop with a yield return null; inside it so it will skip a frame for each iteration

This way you could instantiate maybe a couple of hundred to a thousand per frame ins$$anonymous$$d of waiting for everything.

avatar image ExtremePowers · Oct 21, 2014 at 01:53 PM 0
Share

I have a problem, my yield return null line says that it needs a semicolon which it has.

 function Grass() {
     var chunkGrass = 0;
     while (chunkGrass < grassPerChunk) {
             chunkGrass++;
             if (chunkGrass == grassPerChunk / 20) {
                 yield return null; //UCE0001: ';' expected. Insert a semicolon at the end.
             }
     }
 }
avatar image HarshadK · Oct 21, 2014 at 01:57 PM 0
Share

In JS you do not need to specify 'yield return null', It is used in C#. In JS just use:

 yield;
avatar image _dns_ · Oct 21, 2014 at 03:05 PM 1
Share

Hi, I don't know exactly what are your needs but 10k meshes or objects is a lot to manage for an engine. Why don't you create 1 mesh composed of 10k quads, each representing 1 grass leaf ?

avatar image ExtremePowers · Oct 21, 2014 at 04:07 PM 0
Share

@Harshad$$anonymous$$ Thx :)

@dns How would I do that? :)

avatar image _dns_ · Oct 21, 2014 at 04:42 PM 0
Share

Well, you need to create vertices and triangles like in some procedural geometry example (http://u3d.as/content/unity-technologies/procedural-examples/3zu), you may need to raycast on your terrain mesh so you can position those vertices/triangles on it with random rotations. You'll also have to choose a shader that renders both sides of the geometry. Note that doing so will produce a mesh that should be quite quick to draw but won't have "billboards" leaves. This is not such a simple script, maybe there are solutions in the asset store that can do what you want.

avatar image ExtremePowers · Oct 21, 2014 at 07:09 PM 0
Share

AFAI$$anonymous$$ you can't make a shader render both sides of the face, since 1 face only have 1 normal... Would i really have to create two quads facing different directions then?

avatar image _dns_ · Oct 21, 2014 at 07:36 PM 0
Share

That's right, lighting may be a problem. It depends how you want to do the grass lighting, with dynamic or static/precomputed lighting. $$anonymous$$aybe you can get a color from a point on the ground/terrain mesh after baking lights & shadows and use this value in vertex color of each grass leaf. If you want real time light & shadow on the grass, then it may be better to have 2 quads or a 2 pass shader (it look like it's possible: http://forum.unity3d.com/threads/double-sided-material.21778/). This depends on your needs, target platform & performance concerns. Having to double the geometry will not be a problem on PC/$$anonymous$$ac but may slow down a phone, you may want to make some tests or search about this (I don't know a lot about current mobile platforms, only read some stuff).

Another solution could be to spawn 10k billboard particles with zero speed&gravity & long/infinite life so they don't move at all. Then you position them on the terrain using raycast & http://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html and SetParticle (and no need to do this each frame as the particles won't move). Expect some complications as particle systems are not made to do that, but it could work.

avatar image ExtremePowers · Oct 22, 2014 at 11:02 AM 0
Share

@dns The thing with the particles sounds interesting, but the problem is that I have found a shader which allows shadows on particles since I don't want my grass to be lit all the time, the problem is that it is affected by the direction the light is co$$anonymous$$g from, which makes sense of course. but this make the grass billboard change color as I rotate my camera. Do you have an idea how I could fix this :)

avatar image _dns_ · Oct 22, 2014 at 03:40 PM 0
Share

The shader must use the normal to compute lighting on the particles. As you rotate the camera, the particle billboards rotate too to continually face it, then the light computation changes because the billboard & normal are rotated. $$anonymous$$aybe some "unlit" shaders are better, with alpha/transparency cutout or you'll also have sorting problems. I think you can still get shadows with it, or you'll have to customize your own shader.

Grass rendering is not an easy task, there are lots of different techniques that evolved with hardware capabilities. Re-doing it yourself will take lots of time doing research, tests, debugging before it "looks good"... This can be very interesting to do but if what you want to make is a game, I would recommend to use the maximum of what Unity can provide so you can focus on the game itself. I don't know what features your terrain is using that makes it different than the one Unity provides, maybe you can convert this terrain into Unity's one using heightmap import or by a script. You could then have "free" grass, trees, optimizations, documentation, examples... There are also more advanced terrain packages in the asset store that could be time saving for not so much money. Well, that's a "best practice" I'm following myself :-)

avatar image ExtremePowers · Oct 22, 2014 at 04:34 PM 0
Share

The problem is that unlit grass gets too bright at night.. And the reason I use a mesh ins$$anonymous$$d of a terrain is because it can have non-shared vertices.

avatar image _dns_ · Oct 22, 2014 at 04:53 PM 0
Share

$$anonymous$$aybe you can modify the default Unlit shader to add a simple color modulation with your current time-of-day color.

About the terrain, you can also modify Unity's terrain shader. I guess you need non-shared vertices to have different textures or UV for each triangle. You could generate a texture containing information to modify UV or index to sub-texture in an atlas. The pixel shader could read this texture and do different things than the default shader does. I do this for my actual project (on PC though, it may be expensive on mobile), it is a little complicated but I get all the other benefits of the built in terrain. Just in case, Unity's shaders are here: http://unity3d.com/unity/download/archive

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iwHiteRabbiT · Oct 21, 2014 at 04:28 PM

Have already made a scatter tool which did what you want. Bad idea ^^'. Instancing a lot of gameObject slow down considerately the framerate.

However, if you can't create a "single" mesh for your all grass, you could try to manage yourself by saving each mesh transformation matrix in a single array. After, in Update() for exemple, drawing yourself via DrawMesh (async drawing with full rendering pipeline) or DrawMeshNow (sync drawing without unity rendering pipeline).

With this, you never instanciate anything, just yield the creation of all the matrix.

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 Little_Turtle · Feb 01, 2017 at 09:05 AM

A better answer is to group grass in the same mesh. This way you create one mesh spawning many grass instances (like a sector) in a single object. Each grass might consist just of two triangles but you are free to create as many triangles as far away from each other as needed.

If you putting you combine your different grass textures in a clever way you can even put many different grass and flower combinations closely (or with some space in between) together so you can use a single pair of triangles to draw two or more grass groups together (if you use real 3d to draw the grass).

Also remember creating 10000 times a mesh is expensive, creating 10000*2 triangles within a mesh is cheap but also creating 10000 times an object is expensive but having 10000 existing objects being moved as necessary (grass that is not visible any more gets reused by just moving it to a new location).

This way you can have almost instant appearing grass without the costs of creating too many objects.

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

31 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

Related Questions

Grass on mesh 0 Answers

Reset prefab instead of destroying and instantiating again 1 Answer

How to create a new GameObject and assign a Mesh that is just being created in a same function to it? 0 Answers

Instantiating a gameobject causes a lagspike 2 Answers

Editor lags when coming out of play mode...? 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