CameraState.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using UnityEditor.AnimatedValues;
  2. using UnityEngine;
  3. namespace UnityEditor.Rendering.LookDev
  4. {
  5. /// <summary>
  6. /// Interface to comunicate with simple <see cref="Renderer"/>
  7. /// </summary>
  8. public interface ICameraUpdater
  9. {
  10. /// <summary>Method called To update the LookDev camera position</summary>
  11. /// <param name="camera">The camera</param>
  12. void UpdateCamera(Camera camera);
  13. }
  14. /// <summary>
  15. /// Class containing data regarding position, rotation and viewport size of a camera
  16. /// </summary>
  17. [System.Serializable]
  18. public class CameraState : ICameraUpdater
  19. {
  20. private static readonly Quaternion k_DefaultRotation = Quaternion.LookRotation(new Vector3(0.0f, 0.0f, 1.0f));
  21. private const float k_DefaultViewSize = 10f;
  22. private static readonly Vector3 k_DefaultPivot = Vector3.zero;
  23. private const float k_DefaultFoV = 90f;
  24. private const float k_NearFactor = 0.000005f;
  25. private const float k_MaxFar = 1000;
  26. /// <summary>The position of the camera pivot</summary>
  27. [field: SerializeField]
  28. public Vector3 pivot { get; set; } = k_DefaultPivot;
  29. /// <summary>The rotation of the camera arround the pivot</summary>
  30. [field: SerializeField]
  31. public Quaternion rotation { get; set; } = k_DefaultRotation;
  32. /// <summary>The size of the view</summary>
  33. [field: SerializeField]
  34. public float viewSize { get; set; } = k_DefaultViewSize;
  35. /// <summary>The distance from pivot</summary>
  36. public float distanceFromPivot
  37. // distance coeficient from vertical FOV should be
  38. // 1f / Mathf.Tan(kDefaultFoV * 0.5f * Mathf.Deg2Rad)
  39. // but with fixed FoV of 90, this coef is always equal to 1f
  40. => viewSize;
  41. /// <summary>The position of the camera</summary>
  42. public Vector3 position
  43. => pivot + rotation * new Vector3(0, 0, -distanceFromPivot);
  44. /// <summary>The field of view of the camera</summary>
  45. public float fieldOfView => k_DefaultFoV;
  46. /// <summary>The far clip distance from camera</summary>
  47. public float farClip => Mathf.Max(k_MaxFar, 2 * k_MaxFar * viewSize);
  48. /// <summary>The near clip distance from camera</summary>
  49. public float nearClip => farClip * k_NearFactor;
  50. /// <summary>The Forward vector in world space</summary>
  51. public Vector3 forward => rotation * Vector3.forward;
  52. /// <summary>The Up vector in world space</summary>
  53. public Vector3 up => rotation * Vector3.up;
  54. /// <summary>The Right vector in world space</summary>
  55. public Vector3 right => rotation * Vector3.right;
  56. internal Vector3 QuickReprojectionWithFixedFOVOnPivotPlane(Rect screen, Vector2 screenPoint)
  57. {
  58. if (screen.height == 0)
  59. return Vector3.zero;
  60. float aspect = screen.width / screen.height;
  61. //Note: verticalDistance is same than distance from pivot with fixed FoV 90°
  62. float verticalDistance = distanceFromPivot;
  63. Vector2 normalizedScreenPoint = new Vector2(
  64. screenPoint.x * 2f / screen.width - 1f,
  65. screenPoint.y * 2f / screen.height - 1f);
  66. return pivot
  67. - up * verticalDistance * normalizedScreenPoint.y
  68. - right * verticalDistance * aspect * normalizedScreenPoint.x;
  69. }
  70. //Pivot is always on center axis by construction
  71. internal Vector3 QuickProjectPivotInScreen(Rect screen)
  72. => new Vector3(screen.width * .5f, screen.height * .5f, distanceFromPivot);
  73. /// <summary>
  74. /// Update a Camera component and its transform with this state values
  75. /// </summary>
  76. /// <param name="camera">The camera to update</param>
  77. public void UpdateCamera(Camera camera)
  78. {
  79. camera.transform.rotation = rotation;
  80. camera.transform.position = position;
  81. camera.nearClipPlane = nearClip;
  82. camera.farClipPlane = farClip;
  83. camera.fieldOfView = fieldOfView;
  84. }
  85. /// <summary>
  86. /// Reset the State to its default values
  87. /// </summary>
  88. public void Reset()
  89. {
  90. pivot = k_DefaultPivot;
  91. rotation = k_DefaultRotation;
  92. viewSize = k_DefaultViewSize;
  93. }
  94. internal void SynchronizeFrom(CameraState other)
  95. {
  96. pivot = other.pivot;
  97. rotation = other.rotation;
  98. viewSize = other.viewSize;
  99. }
  100. }
  101. }