본문 바로가기
개발/Unity

Unity) 카메라 화면 안에 있는지 여부 판단(object In the camera)

by 테샤르 2020. 8. 21.

카메라 화면 안에 있는지 여부 판단(object In the camera)

카메라를 기준으로 카메라밖에 있는 오브젝트인지 판단을 해서 ObjectPool 작업을 진행하려고 한다.

예전에는 화면 밖의 어떤 오브젝트 or 포지션을 기준으로 작업했었는데. 이번에는 카메라 화면 안에 있는지 판단해서 없으면 Object를 Pool에게 되돌려 주려고 한다.

 

작업된 코드는 다음과 같다.

public bool IsTargetVisible(Camera _camera, Transform _transform)
    {
        var planes = GeometryUtility.CalculateFrustumPlanes(_camera);
        var point = _transform.position;
        foreach (var plane in planes)
        {
            if (plane.GetDistanceToPoint(point) < 0)
                return false;
        }
        return true;
    }

카메라의 시야 절 두체(View Frustum)를 통해서 그것이 형성하는 평면을 반환해서 

그 평면이 현재 특정 transform의 포인트 좌표와 비교해서 화면 밖에 있는지 판단한다는 코드이다.

반응형

 

URL :https://docs.unity3d.com/kr/530/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html

 

Unity - 스크립팅 API: GeometryUtility.CalculateFrustumPlanes

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. 닫기

docs.unity3d.com

 ★

 

반응형

댓글