Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CallumCarmicheal/6c7efc824c436401b0920d52fb1e3b25 to your computer and use it in GitHub Desktop.
Save CallumCarmicheal/6c7efc824c436401b0920d52fb1e3b25 to your computer and use it in GitHub Desktop.
This will disable Meshes that cant be seen by the camera to free up some rendering calls (does not disable the object so scripts will still work!)
/// NAME Unity3d: Stop useless rendering calls
/// PROJ Tested on the Standard Assets demo
/// AUTH Callum Carmicheal
/// DESC This will disable Meshes that cant be seen by the
/// camera to free up some rendering calls (does not disable
/// the object so scripts will still work!)
/// NOTE This does not disable meshes that are behind something yet!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Unloader : MonoBehaviour {
private Component[] cachedObjects;
public GameObject unrenderParent; // The parent to all objects that get cached and unrendered!
public bool cacheObjectsNow;
// Use this for initialization
void Start () { CacheObjects(); }
// Update is called once per frame
void Update () {
// Check if the objects are needed
// to be recached.
if (cacheObjectsNow)
CacheObjects();
// Check for the current rendering
// camera and pass it to processObjects as cam
//
// Please also note, using the Camera.current
// is mainly intended for low level access,
// this can be avoided by manually added each
// camera you use and checking if they are
// enabled, if not stay lazy and use this instead.
if (Camera.main.isActiveAndEnabled)
processObjects(Camera.main);
else processObjects(Camera.current);
}
void processObjects(Camera cam) {
// Check if we have cached components
if (cachedObjects == null) {
Debug.LogError("var cachedObjects: NULL");
return;
}
// This has been placed into one method
// to lower the overhead, may it be a ms etc
// everything helps when rendering a game.
// Since this will be alittle intensive
// its best to cut down where posibile.
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);
// Store the variables outside of the loop to cutdown on
// memory and register cost!
bool hasMRs = false;
MeshRenderer[] meshRenders = null;
//
foreach (Component obj in cachedObjects) {
// Check if the object has a Mesh
meshRenders = null;
hasMRs = false;
try {
// Get all the colliders attached to the object!
meshRenders = obj.GetComponents<MeshRenderer>();
hasMRs = (meshRenders != null || meshRenders.Length != 0);
//Debug.Log("var meshRenders: Size = " + meshRenders.Length);
} catch { /* Error occured while trying to get the object for some reason? */ }
// Continue if the component does not have
// a MeshRenderer!
if (!hasMRs) {
Debug.LogWarning(name + ": No MeshRenderer");
continue;
}
foreach (var mr in meshRenders) {
if (GeometryUtility.TestPlanesAABB(planes, mr.bounds)) {
// Check if object is visible if not show it
if (!mr.enabled) {
mr.enabled = true;
Debug.Log(name + ": Visible");
}
} else {
// Check if the object is hidden, if not hide it!
if (mr.enabled) {
mr.enabled = false;
Debug.Log(name + ": Hidden");
}
}
}
}
}
void CacheObjects() {
cacheObjectsNow = false;
cachedObjects = unrenderParent.GetComponentsInChildren<Component>();
Debug.Log("cachedObjects: Size = " + cachedObjects.Length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment