[펌] Render the view frustum of a camera in Unity
Unity3D/Editor / 2024. 10. 4. 13:46
Unity 2021.3.44f1
----
Unity Editor의 Scene View에서 Camera의 View Frustum을 볼 수 있게 해주는 스크립트.
View Frustum을 보고 싶은 카메라에 Component로 넣고 실행하면 Scene View에 보인다.
using UnityEngine;
public class FrustumViewer : MonoBehaviour
{
// 에디터에서만 작동하도록 define
#if UNITY_EDITOR
Camera m_Camera = null;
Vector3[] m_nearCorners = new Vector3[4]; //Approx'd nearplane corners
Vector3[] m_farCorners = new Vector3[4]; //Approx'd farplane corners
Plane[] m_camPlanes = null;
private void OnEnable()
{
m_Camera = this.GetComponent<Camera>();
}
void Update()
{
DrawFrustum(m_Camera);
}
void DrawFrustum(Camera _cam)
{
if (null == _cam)
return;
m_camPlanes = GeometryUtility.CalculateFrustumPlanes(_cam); //get planes from matrix
Plane temp = m_camPlanes[1]; m_camPlanes[1] = m_camPlanes[2]; m_camPlanes[2] = temp; //swap [1] and [2] so the order is better for the loop
for (int i = 0; i < 4; i++)
{
m_nearCorners[i] = Plane3Intersect(m_camPlanes[4], m_camPlanes[i], m_camPlanes[(i + 1) % 4]); //near corners on the created projection matrix
m_farCorners[i] = Plane3Intersect(m_camPlanes[5], m_camPlanes[i], m_camPlanes[(i + 1) % 4]); //far corners on the created projection matrix
}
for (int i = 0; i < 4; i++)
{
Debug.DrawLine(m_nearCorners[i], m_nearCorners[(i + 1) % 4], Color.red, Time.deltaTime, true); //near corners on the created projection matrix
Debug.DrawLine(m_farCorners[i], m_farCorners[(i + 1) % 4], Color.blue, Time.deltaTime, true); //far corners on the created projection matrix
Debug.DrawLine(m_nearCorners[i], m_farCorners[i], Color.green, Time.deltaTime, true); //sides of the created projection matrix
}
}
Vector3 Plane3Intersect(Plane p1, Plane p2, Plane p3)
{
//get the intersection point of 3 planes
return ((-p1.distance * Vector3.Cross(p2.normal, p3.normal)) +
(-p2.distance * Vector3.Cross(p3.normal, p1.normal)) +
(-p3.distance * Vector3.Cross(p1.normal, p2.normal))) /
(Vector3.Dot(p1.normal, Vector3.Cross(p2.normal, p3.normal)));
}
#endif
}
[참조] https://grrava.blogspot.com/2014/04/render-view-frustrum-of-camera-in-unity.html
반응형
'Unity3D > Editor' 카테고리의 다른 글
[Package] Unity Device Simulator Definitions For All iPhone 14 & 15 Models (0) | 2024.10.11 |
---|---|
[Editor] 에디터에서 Play 할 때, GameView Scale 초기화 문제 수정 (0) | 2024.09.06 |
[Editor] 에디터에서 Play 할 때, GameView Scale 초기화 문제 보정 (0) | 2024.09.06 |
[펌] Unity Editor detect when build fails (0) | 2023.09.20 |
[에디터확장] BurstDebugInformation_DoNotShipDeleter (0) | 2023.02.10 |