Skip to content

Instantly share code, notes, and snippets.

@Draco18s
Created February 15, 2016 02:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Draco18s/1824f249f66fb3dc003f to your computer and use it in GitHub Desktop.
Save Draco18s/1824f249f66fb3dc003f to your computer and use it in GitHub Desktop.
public static class GameObjectExtensions {
public static bool IsVisibleFrom(this GameObject obj, Camera camera) {
Bounds b = obj.GetComponent<Collider>().bounds;
b.Expand(0.5f);
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, b);
}
public static bool IsVisibleFrom(this GameObject obj, Plane[] planes) {
Bounds b = obj.GetComponent<Collider>().bounds;
b.Expand(0.5f);
return GeometryUtility.TestPlanesAABB(planes, b);
}
}
/** Math utility. Put inside a static helper class somewhere**/
public static Plane[] CalculateFrustum(Vector3 origin, Vector3 direction, float fovRadians, float viewRatio, float distance) {
Vector3 nearCenter = origin + direction * 0.3f;
Vector3 farCenter = origin + direction * distance;
Vector3 camRight = Vector3.Cross(direction,Vector3.up) * -1;
Vector3 camUp = Vector3.Cross(direction,camRight);
float nearHeight = 2 * Mathf.Tan(fovRadians / 2) * 0.3f;
float farHeight = 2 * Mathf.Tan(fovRadians / 2) * distance;
float nearWidth = nearHeight * viewRatio;
float farWidth = farHeight * viewRatio;
Vector3 farTopLeft = farCenter + camUp*(farHeight*0.5f) - camRight*(farWidth*0.5f);//new Vector3(-camRight.x * (farWidth*0.5f), (farHeight*0.5f), -camRight.z * (farWidth*0.5f));
//Vector3 farTopRight = farCenter + camUp*(farHeight*0.5f) + camRight*(farWidth*0.5f);//new Vector3( camRight.x * (farWidth*0.5f), (farHeight*0.5f), camRight.z * (farWidth*0.5f));
Vector3 farBottomLeft = farCenter - camUp*(farHeight*0.5f) - camRight*(farWidth*0.5f);//new Vector3(-camRight.x * (farWidth*0.5f), -(farHeight*0.5f), -camRight.z * (farWidth*0.5f));
Vector3 farBottomRight = farCenter - camUp*(farHeight*0.5f) + camRight*(farWidth*0.5f);//new Vector3( camRight.x * (farWidth*0.5f), -(farHeight*0.5f), camRight.z * (farWidth*0.5f));
Vector3 nearTopLeft = nearCenter + camUp*(nearHeight*0.5f) - camRight*(nearWidth*0.5f);//new Vector3(-camRight.x * (nearWidth*0.5f), farCenter.y + (nearHeight*0.5f), -camRight.z * (nearWidth*0.5f));
Vector3 nearTopRight = nearCenter + camUp*(nearHeight*0.5f) + camRight*(nearWidth*0.5f);//new Vector3( camRight.x * (nearWidth*0.5f), farCenter.y + (nearHeight*0.5f), camRight.z * (nearWidth*0.5f));
//Vector3 nearBottomLeft = nearCenter - camUp*(nearHeight*0.5f) - camRight*(nearWidth*0.5f);//new Vector3(-camRight.x * (nearWidth*0.5f), farCenter.y - (nearHeight*0.5f), -camRight.z * (nearWidth*0.5f));
Vector3 nearBottomRight = nearCenter - camUp*(nearHeight*0.5f) + camRight*(nearWidth*0.5f);//new Vector3( camRight.x * (nearWidth*0.5f), farCenter.y - (nearHeight*0.5f), camRight.z * (nearWidth*0.5f));
Plane[] planes = {
new Plane(nearTopLeft,farTopLeft,farBottomLeft),
new Plane(nearTopRight,nearBottomRight,farBottomRight),
new Plane(farBottomLeft,farBottomRight,nearBottomRight),
new Plane(farTopLeft,nearTopLeft,nearTopRight),
new Plane(nearBottomRight,nearTopRight,nearTopLeft),
new Plane(farBottomRight,farBottomLeft,farTopLeft)};
return planes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment