large number of identical objects in scene?

Hi there,

I am making a racing game which has a large number of fence sections to be applied at different rotations and positions along the track. I found some performance issues when all the fences were placed along the track - approximately 2k fence mesh assets.
I have looked at solutions such as billboarding etc however I am unsure of what would be the best option in this case? No colliders are needed the fences are static and all have an identical texture.

Any advice would be appreciated!

I think your bottleneck is the number of drawcalls caused by having 2k+ fences. In your situation, where the fences are static and do not move relative to each other, it’s possible to consolidate the drawcalls by combining the fences into a single mesh using Mesh.CombineMeshes. This causes Unity to batch them all into a single mesh so you can “bulk draw” them, so to speak.

Beware that this has the disadvantage that the combined length of the fence mesh will evaluate to visible for all fence segments, since I assume some smaller portion of it will always be within the camera’s view frustum. This means frustum culling becomes less efficient as the engine must spend time rendering polygons of a larger mesh that aren’t in the scene, because a few polygons of the combined mesh are. But it might still be better than 1 draw call per fence segment.

Perhaps you can come up with some hybrid solution where you use Mesh.CombineMeshes on many fence segments (but not all), so you combine stretches of, say, 100 fence meshes into 1 mesh? Then you’ll reduce your 2k fence prefabs to 20 meshes, the origin of each is 100 fence prefabs, position one after another along your track.

A completely alternative solution which doesn’t involve Mesh.CombineMeshes might be to dynamically instantiate/destroy fence prefabs based on what portion of the track the camera is currently viewing, so, when the car drives forward, past fences get destroyed and fences just ahead of the camera get instantiated.

This sounds like a job for CombineChildren.cs from the StandardAssets:

Attach your fence objects to one parent (they still can have individual hierarchies), and attach the script to that parent. It will optimize the meshes for rendering, reducing the number of draw calls to one, if they all use the same material.

There is a catch, that the sum of all vertices must not exceed 65000 (it will print an error in the Console if it does, and not render the objects). If this happens, simply distribute your fences over several parent objects with the script attached to each.

If you can’t see all the fences at one time, maybe moving the fences along with the camera (you’ll have to work out some way of snapping the fences to the correct positions as the camera moves) then you could reduce the number of fence items in the scene at any one time.