- Home /
Optimizing Thousands of game objects...
I've been procedurally placing cubes/rocks/spheres on my terrain and I've racked up about 1500 draw calls or so. I'm wondering what are the best ways to go about optimizing this.
I do realize there have been a few questions very similar to this. But I thought I'd ask it again to see if I can get new ideas or more elaborate explanations
Answer by Graham-Dunnett · Jan 04, 2013 at 01:46 PM
Basically you are asking about not drawing objects that are not visible. There are two approaches that spring to mind:
Use Occlusion culling. Unity has a built in system, available to Pro users, which can help determine when objects are occluded (not visible). This requires that the objects don't moving, so works only for static objects.
Use a home-grown solution. You know more about your game level than anyone else. Perhaps you "know" that objects in a certain part of the scene cannot be visible from certain other areas. In my zombie-horror-survival game, if my player is in the kitchen, she cannot see zombies in the basement or bedrooms. If she is in the landing area, near the stairs, then she can see zombies in the kitchen and bedrooms. (Well, only if they are by the doors.) This is all invented of course, but I mean in such a game you can know in advance and based on the scene areas of visibility. So, using colliders/triggers/AABBs you can do some simple occlusion logic. (If the player is in box7, she can only see zombies in boxes 2, 5 and 8). Authoring these boxes is the hard part.
I would propose that in your game, which is terrain based, there may be areas hidden by hills, so some simple occlusion system should be easy to experiment with, and might even be a 2d problem, so you only need to look at axis aligned squares.
Alternatively, every frame fire a ray at a random selection of boxes/spheres/rocks. If a collision happens with another object, the rock/whatever cannot be seen, so stop drawing it. Else the cube/rock is visible. You cannot possibly do this for all 1500 objects every frame, which is why I suggest choosing some random objects to test against. This will probably cause objects that are visible to not be drawn. So you may need some other approach to resolve this.
Note that Unity will always perform camera culling, so any object that is not in the field of view of the camera will not be drawn.
Thanks for your reply, the game is actually an 3D First person camera/shooter type game, so Terrain would be like halo or any game like that. But I've been attempting or hoping I could populate my terrain procedurally with rocks and things by applying rigidbody to all of those rocks and things. and basically have it rain down and settle itself upon my terrain. And then I would remove the rigidbody component once it has settled. I've been successful with this idea up to the part of the camera seeing a thousand rocks/objects or so atop of the terrain.
I was hoping this would be a really neat way of populating my terrain with rocks and objects. I haven't seen anyone using this method before, and maybe the trouble of trying to optimize those thousands of objects is the reason why I havent seen this method.
And if you were curious, I believe my game does require individual rocks layered upon a terrain. Either that or I need to find a way to make holes in a typical terrain mesh, but even that idea may not give me exactly want for my game concept.
I'll tryout those methods you mentioned.
Your answer
Follow this Question
Related Questions
Optimizing vegetation. 3 Answers
Prebuilt gameobject bundles without Prefab instancing behavior. 0 Answers
i cant see anything in the game! 6 Answers
Make a simple tree 1 Answer
How do I fix my terrain, its too reflective/plastic? 2 Answers