Quick Flow
eye + target(or forward) + up -> view basis / view matrix
FOV + aspect + near/far -> projection / frustum
screen pixel -> near/far point unproject
near -> far direction -> world-space picking rayTarget은 world position이고 forward는 방향입니다. Forward를 받는 API 조합이라면 target = eye + forward로 바꿉니다. Up과 forward가 평행에 가까우면 camera basis가 불안정해집니다.
Frustum
Frustum은 perspective camera가 볼 수 있는 six-plane volume입니다. Near·far, vertical FOV와 aspect가 범위를 정합니다. Bounding sphere·box가 여섯 plane 밖에 있는지 검사하면 화면에 들어오지 않는 object를 draw 전에 제외할 수 있습니다.
Orthographic camera는 거리에 따른 화면 크기가 변하지 않는 box-shaped volume을 사용합니다. Frustum culling과 화면 picking은 camera projection 종류와 같은 convention을 사용해야 합니다.
Screen Picking
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, 1000f))
{
Debug.Log(hit.point);
}화면의 (x, y)만으로 world point 하나가 정해지지 않으므로 depth 또는 ray와 교차할 plane·geometry가 필요합니다. 직접 만들 때는 screen을 NDC로 바꾸고 inverse view-projection으로 near/far point를 unproject합니다.
자주 틀리는 점
- UI 좌표와 render target pixel 좌표의 원점·scale을 혼동하지 않습니다.
- Screen point에 임의의
z=0을 넣고 원하는 world surface가 나온다고 가정하지 않습니다. - Near를 지나치게 작게, far를 지나치게 크게 잡아 depth precision을 잃지 않습니다.
- Camera 뒤쪽 point의 screen projection 결과를 화면 안으로 판정하지 않습니다.
참고 링크
2 sources