PerspectiveCameraViewVolumePointsCalculator.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. namespace O3DWB
  4. {
  5. public class PerspectiveCameraViewVolumePointsCalculator : CameraViewVolumePointsCalculator
  6. {
  7. #region Public Methods
  8. public override Vector3[] CalculateWorldSpaceVolumePoints(Camera camera)
  9. {
  10. // Cache needed data
  11. Transform cameraTransform = camera.transform;
  12. Vector3 cameraPosition = cameraTransform.position;
  13. Vector3 cameraRight = cameraTransform.right;
  14. Vector3 cameraUp = cameraTransform.up;
  15. Vector3 cameraLook = cameraTransform.forward;
  16. float halfFovAngle = camera.fieldOfView * 0.5f * Mathf.Deg2Rad;
  17. float angleTangent = Mathf.Tan(halfFovAngle);
  18. float xToZRatio = angleTangent * camera.aspect; // Multiply by the aspect ratio to take the screen distortion into account
  19. float yToZRatio = angleTangent;
  20. float xRatioMulNearPlane = xToZRatio * camera.nearClipPlane;
  21. float xRatioMulFarPlane = xToZRatio * camera.farClipPlane;
  22. float yRatioMulNearPlane = yToZRatio * camera.nearClipPlane;
  23. float yRatioMulFarPlane = yToZRatio * camera.farClipPlane;
  24. // Calculate the volume points on the near plane
  25. Vector3[] worldSpaceVolumePoints = new Vector3[8];
  26. worldSpaceVolumePoints[(int)CameraViewVolumePoint.TopLeftOnNearPlane] = cameraPosition + cameraLook * camera.nearClipPlane - cameraRight * xRatioMulNearPlane + cameraUp * yRatioMulNearPlane;
  27. worldSpaceVolumePoints[(int)CameraViewVolumePoint.TopRightOnNearPlane] = cameraPosition + cameraLook * camera.nearClipPlane + cameraRight * xRatioMulNearPlane + cameraUp * yRatioMulNearPlane;
  28. worldSpaceVolumePoints[(int)CameraViewVolumePoint.BottomRightOnNearPlane] = cameraPosition + cameraLook * camera.nearClipPlane + cameraRight * xRatioMulNearPlane - cameraUp * yRatioMulNearPlane;
  29. worldSpaceVolumePoints[(int)CameraViewVolumePoint.BottomLeftOnNearPlane] = cameraPosition + cameraLook * camera.nearClipPlane - cameraRight * xRatioMulNearPlane - cameraUp * yRatioMulNearPlane;
  30. // Calculate the volume points on the far plane
  31. worldSpaceVolumePoints[(int)CameraViewVolumePoint.TopLeftOnFarPlane] = cameraPosition + cameraLook * camera.farClipPlane - cameraRight * xRatioMulFarPlane + cameraUp * yRatioMulFarPlane;
  32. worldSpaceVolumePoints[(int)CameraViewVolumePoint.TopRightOnFarPlane] = cameraPosition + cameraLook * camera.farClipPlane + cameraRight * xRatioMulFarPlane + cameraUp * yRatioMulFarPlane;
  33. worldSpaceVolumePoints[(int)CameraViewVolumePoint.BottomRightOnFarPlane] = cameraPosition + cameraLook * camera.farClipPlane + cameraRight * xRatioMulFarPlane - cameraUp * yRatioMulFarPlane;
  34. worldSpaceVolumePoints[(int)CameraViewVolumePoint.BottomLeftOnFarPlane] = cameraPosition + cameraLook * camera.farClipPlane - cameraRight * xRatioMulFarPlane - cameraUp * yRatioMulFarPlane;
  35. return worldSpaceVolumePoints;
  36. }
  37. #endregion
  38. }
  39. }
  40. #endif