OrthoCameraViewVolumePointsCalculator.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. namespace O3DWB
  4. {
  5. public class OrthoCameraViewVolumePointsCalculator : 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 halfVolumeVerticalSize = camera.orthographicSize;
  17. float halfVolumeHorizontalSize = halfVolumeVerticalSize * camera.aspect; // Multiply by the aspect ratio to take the screen distortion into account
  18. // Calculate the volume points on the near plane
  19. Vector3[] worldSpaceVolumePoints = new Vector3[8];
  20. worldSpaceVolumePoints[(int)CameraViewVolumePoint.TopLeftOnNearPlane] = cameraPosition + cameraLook * camera.nearClipPlane - cameraRight * halfVolumeHorizontalSize + cameraUp * halfVolumeVerticalSize;
  21. worldSpaceVolumePoints[(int)CameraViewVolumePoint.TopRightOnNearPlane] = cameraPosition + cameraLook * camera.nearClipPlane + cameraRight * halfVolumeHorizontalSize + cameraUp * halfVolumeVerticalSize;
  22. worldSpaceVolumePoints[(int)CameraViewVolumePoint.BottomRightOnNearPlane] = cameraPosition + cameraLook * camera.nearClipPlane + cameraRight * halfVolumeHorizontalSize - cameraUp * halfVolumeVerticalSize;
  23. worldSpaceVolumePoints[(int)CameraViewVolumePoint.BottomLeftOnNearPlane] = cameraPosition + cameraLook * camera.nearClipPlane - cameraRight * halfVolumeHorizontalSize - cameraUp * halfVolumeVerticalSize;
  24. // Calculate the volume points on the far plane
  25. worldSpaceVolumePoints[(int)CameraViewVolumePoint.TopLeftOnFarPlane] = cameraPosition + cameraLook * camera.farClipPlane - cameraRight * halfVolumeHorizontalSize + cameraUp * halfVolumeVerticalSize;
  26. worldSpaceVolumePoints[(int)CameraViewVolumePoint.TopRightOnFarPlane] = cameraPosition + cameraLook * camera.farClipPlane + cameraRight * halfVolumeHorizontalSize + cameraUp * halfVolumeVerticalSize;
  27. worldSpaceVolumePoints[(int)CameraViewVolumePoint.BottomRightOnFarPlane] = cameraPosition + cameraLook * camera.farClipPlane + cameraRight * halfVolumeHorizontalSize - cameraUp * halfVolumeVerticalSize;
  28. worldSpaceVolumePoints[(int)CameraViewVolumePoint.BottomLeftOnFarPlane] = cameraPosition + cameraLook * camera.farClipPlane - cameraRight * halfVolumeHorizontalSize - cameraUp * halfVolumeVerticalSize;
  29. return worldSpaceVolumePoints;
  30. }
  31. #endregion
  32. }
  33. }
  34. #endif