📜 ⬆️ ⬇️

Occlusion Culling and LOD for Unity Indie

Good day! As a hobby, I pick the Unity engine. My hobby is clearly non-commercial, so I don’t feel much need for a Pro version. Pro, of course, is more indie, but for development, for example, for Android / iOS, the second one is quite enough. Except for one - optimization and related tools.

Spreading cranberries, created by the engine has a half - two thousand triangles. The high poly lock / typewriter makes the picture on the average phone twitch schizophrenically, and the beautiful shader for water will freeze for a long time in the pose of a Roden thinker. It is clear that when a pair of such objects appears in the frame, everything will be terribly buggy and slow.

In Pro, there is an extremely necessary thing, called Occlusion Culling . Briefly - only objects that are in the camera’s field of view are drawn (screenshots under the cut). I looked again at the cost of the Pro, scratched my head, was offended and left to look first and then write crutches.

Without OcclusionCulling:
')
image

With OcclusionCulling:

image

A good example of what smart people have come up with: Simple LOD Manager . The bottom line is that when approaching objects, the low-poly model (cube) is replaced by a high-poly model (for example, a detailed building). Of course, using a cycle in Update () is not good, but the idea is worthy of respect.

My needs are completely satisfied with a small drawing distance, I do not need low-poly models. Of the heavy objects, only native trees are found, so I’ve a bit of an idea. Why not draw a heavy object as the player approaches it?

At the output we have a script that hangs on the desired object and has only one parameter - Distance. The code is simple, you can use it in test scenes.

public var distance = 100f; private var meshRnd: MeshRenderer; private var _collider: BoxCollider; function Awake() { meshRnd = gameObject.GetComponentInParent(MeshRenderer); _collider = gameObject.AddComponent(BoxCollider); _collider.isTrigger = true; _collider.size = Vector3(distance, distance, distance); } function OnTriggerEnter (player : Collider) { if(player.tag=="Player") meshRnd.enabled = true; } function OnTriggerExit (player: Collider) { if(player.tag=="Player") meshRnd.enabled = false; } 

We catch the MeshRenderer component of the object that is responsible for drawing the model itself. Create a collider - in this case, box, with the edge of distance - and make it a trigger. If someone with the Player tag breaks into the collider, it will trigger and draw the model.

The distance is most conveniently selected in Window> Layouts> 2 by 3 mode.

Deactivate the MeshRenderer of the object on which the script is hung before starting.

Good luck.

Source: https://habr.com/ru/post/245489/


All Articles