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