Created
January 12, 2018 02:15
-
-
Save ginxx009/8c634ecebd7c56c1e2ab52485aec343d to your computer and use it in GitHub Desktop.
OnBecameVisible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Renderer someObject; | |
Frustum f = Camera.main.GetFrustum(); | |
if (f.TestBounds(someObject.bounds) == EFrustumIntersection.Outside) | |
{ | |
// "someObject" is not rendered by the main camera | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
public class EditorCameraControls : EditorWindow | |
{ | |
[MenuItem("Tools/EditorCameraControls")] | |
private static void Init() | |
{ | |
GetWindow<EditorCameraControls>(); | |
} | |
int state = -1; | |
bool autoDisable = false; | |
private void OnEnable() | |
{ | |
bool allOff = true; | |
bool allOn = true; | |
foreach (var cam in SceneView.GetAllSceneCameras()) | |
{ | |
allOff &= !cam.gameObject.activeSelf; | |
allOn &= cam.gameObject.activeSelf; | |
} | |
if (allOn && !allOff) | |
state = 1; | |
else if (!allOn && allOff) | |
state = 0; | |
else | |
state = -1; | |
EditorApplication.playmodeStateChanged += OnPlaymodeChange; | |
} | |
private void OnDisable() | |
{ | |
EditorApplication.playmodeStateChanged -= OnPlaymodeChange; | |
} | |
void SetCamState(bool aState) | |
{ | |
foreach (var cam in SceneView.GetAllSceneCameras()) | |
{ | |
cam.gameObject.SetActive(aState); | |
} | |
state = aState?1:0; | |
Repaint(); | |
} | |
private void OnGUI() | |
{ | |
if (state == 1) | |
GUI.color = Color.green; | |
else if(state == 0) | |
GUI.color = Color.red; | |
else | |
GUI.color = Color.yellow; | |
GUILayout.BeginHorizontal(); | |
if (GUILayout.Button("Enable")) | |
{ | |
SetCamState(true); | |
SceneView.RepaintAll(); | |
} | |
if (GUILayout.Button("Disable")) | |
{ | |
SetCamState(false); | |
} | |
GUILayout.EndHorizontal(); | |
GUI.color = Color.white; | |
autoDisable = GUILayout.Toggle(autoDisable,"AutoDisable","Button"); | |
} | |
void OnPlaymodeChange() | |
{ | |
if (autoDisable) | |
{ | |
SetCamState(!EditorApplication.isPlaying); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public enum EFrustumIntersection | |
{ | |
Outside, | |
Inside, | |
Intersecting | |
} | |
public struct Frustum | |
{ | |
public Vector4 l; | |
public Vector4 r; | |
public Vector4 b; | |
public Vector4 t; | |
public Vector4 n; | |
public Vector4 f; | |
public Plane left { get { return new Plane { normal = l, distance = l.w }; } } | |
public Plane right { get { return new Plane { normal = r, distance = r.w }; } } | |
public Plane bottom { get { return new Plane { normal = b, distance = b.w }; } } | |
public Plane top { get { return new Plane { normal = t, distance = t.w }; } } | |
public Plane near { get { return new Plane { normal = n, distance = n.w }; } } | |
public Plane far { get { return new Plane { normal = f, distance = f.w }; } } | |
public Vector4 this[int aIndex] | |
{ | |
get | |
{ | |
switch(aIndex) | |
{ | |
case 0: return l; | |
case 1: return r; | |
case 2: return b; | |
case 3: return t; | |
case 4: return n; | |
case 5: return f; | |
default: throw new System.ArgumentOutOfRangeException("aIndex", "value must be between 0 and 5"); | |
} | |
} | |
} | |
public EFrustumIntersection TestBounds(Bounds aBounds) | |
{ | |
var min = aBounds.min; | |
var max = aBounds.max; | |
var x = new Vector2(min.x, max.x); | |
var y = new Vector2(min.y, max.y); | |
var z = new Vector2(min.z, max.z); | |
var res = EFrustumIntersection.Inside; | |
for (int i = 0; i < 6; i++) | |
{ | |
var plane = this[i]; | |
int xi = plane.x > 0 ? 1 : 0; | |
int yi = plane.y > 0 ? 1 : 0; | |
int zi = plane.z > 0 ? 1 : 0; | |
if (plane.x * x[xi] + plane.y * y[yi] + plane.z * z[zi] + plane.w < 0) | |
return EFrustumIntersection.Outside; | |
if (plane.x * x[1-xi] + plane.y * y[1-yi] + plane.z * z[1-zi] + plane.w < 0) | |
res = EFrustumIntersection.Intersecting; | |
} | |
return res; | |
} | |
public EFrustumIntersection TestPoint(Vector3 aPoint) | |
{ | |
for (int i = 0; i < 6; i++) | |
{ | |
var plane = this[i]; | |
float d = plane.x * aPoint.x + plane.y * aPoint.y + plane.z * aPoint.z + plane.w; | |
if (d < 0) | |
return EFrustumIntersection.Outside; | |
} | |
return EFrustumIntersection.Inside; | |
} | |
} | |
public static class VisibilityTools | |
{ | |
public static Frustum GetFrustum(this Camera aCam) | |
{ | |
var m = aCam.projectionMatrix * aCam.worldToCameraMatrix; | |
Frustum res; | |
var x = m.GetRow(0); | |
var y = m.GetRow(1); | |
var z = m.GetRow(2); | |
var w = m.GetRow(3); | |
res.l = GetNormalizedPlane(w + x); | |
res.r = GetNormalizedPlane(w - x); | |
res.b = GetNormalizedPlane(w + y); | |
res.t = GetNormalizedPlane(w - y); | |
res.n = GetNormalizedPlane(w + z); | |
res.f = GetNormalizedPlane(w - z); | |
return res; | |
} | |
private static Vector4 GetNormalizedPlane(Vector4 aVec) | |
{ | |
return aVec*(1f / Mathf.Sqrt(aVec.x * aVec.x + aVec.y * aVec.y + aVec.z * aVec.z)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment