Occlusion Culling and LOD for Unity Indie

Good day! As a hobby, picking the Unity engine. The hobby is clearly non-profit, so I do not feel any special need for the Pro version. Pro, of course, is more prettier than Indie, but for development, for example, for Android / iOS, the second is quite enough. Except for one - optimization and related tools.

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

Pro has a bad thing called Occlusion Culling. Briefly - only objects falling into the field of view of the camera (screenshots under the cut) are drawn. I looked again at the cost of Pro, scratched the back of my head, took offense, 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 loop in Update () is not good, but the idea is worthy of respect.

My needs are completely satisfied by the small drawing range, I do not need low-poly models. Of the heavy objects - only native trees, so I slightly shortened the idea. Why not draw a heavy object as you approach the player?

At the output, we have a script that is hung on the desired object and has only one parameter - Distance. The code is simple, can be used 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, which is responsible for drawing the model itself. Create a collider - in this case box, with an edge edge distance - and make it a trigger. If someone with the Player tag breaks into the collider, he works and draws the model.

It is most convenient to select the distance in Window> Layouts> 2 by 3. mode.

Before starting, deactivate the MeshRenderer of the object on which the script is hung.

Good luck.

Also popular now: