CinemachineComponentBase.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using UnityEngine;
  2. namespace Cinemachine
  3. {
  4. /// <summary>
  5. /// An abstract representation of a mutator acting on a Cinemachine Virtual Camera
  6. /// </summary>
  7. [DocumentationSorting(DocumentationSortingAttribute.Level.API)]
  8. public abstract class CinemachineComponentBase : MonoBehaviour
  9. {
  10. /// <summary>Useful constant for very small floats</summary>
  11. protected const float Epsilon = Utility.UnityVectorExtensions.Epsilon;
  12. /// <summary>Get the associated CinemachineVirtualCameraBase</summary>
  13. public CinemachineVirtualCameraBase VirtualCamera
  14. {
  15. get
  16. {
  17. if (m_vcamOwner == null)
  18. m_vcamOwner = GetComponent<CinemachineVirtualCameraBase>();
  19. if (m_vcamOwner == null && transform.parent != null)
  20. m_vcamOwner = transform.parent.GetComponent<CinemachineVirtualCameraBase>();
  21. return m_vcamOwner;
  22. }
  23. }
  24. CinemachineVirtualCameraBase m_vcamOwner;
  25. /// <summary>Returns the owner vcam's Follow target.</summary>
  26. public Transform FollowTarget
  27. {
  28. get
  29. {
  30. CinemachineVirtualCameraBase vcam = VirtualCamera;
  31. return vcam == null ? null : vcam.ResolveFollow(vcam.Follow);
  32. }
  33. }
  34. /// <summary>Returns the owner vcam's LookAt target.</summary>
  35. public Transform LookAtTarget
  36. {
  37. get
  38. {
  39. CinemachineVirtualCameraBase vcam = VirtualCamera;
  40. return vcam == null ? null : vcam.ResolveLookAt(vcam.LookAt);
  41. }
  42. }
  43. /// <summary>Get Follow target as ICinemachineTargetGroup, or null if target is not a group</summary>
  44. public ICinemachineTargetGroup AbstractFollowTargetGroup
  45. {
  46. get
  47. {
  48. CinemachineVirtualCameraBase vcam = VirtualCamera;
  49. return vcam == null ? null : vcam.AbstractFollowTargetGroup;
  50. }
  51. }
  52. /// <summary>Get Follow target as CinemachineTargetGroup, or null if target is not a CinemachineTargetGroup</summary>
  53. public CinemachineTargetGroup FollowTargetGroup => AbstractFollowTargetGroup as CinemachineTargetGroup;
  54. /// <summary>Get the position of the Follow target. Special handling: If the Follow target is
  55. /// a VirtualCamera, returns the vcam State's position, not the transform's position</summary>
  56. public Vector3 FollowTargetPosition
  57. {
  58. get
  59. {
  60. var vcam = VirtualCamera.FollowTargetAsVcam;
  61. if (vcam != null)
  62. return vcam.State.FinalPosition;
  63. Transform target = FollowTarget;
  64. if (target != null)
  65. return TargetPositionCache.GetTargetPosition(target);
  66. return Vector3.zero;
  67. }
  68. }
  69. /// <summary>Get the rotation of the Follow target. Special handling: If the Follow target is
  70. /// a VirtualCamera, returns the vcam State's rotation, not the transform's rotation</summary>
  71. public Quaternion FollowTargetRotation
  72. {
  73. get
  74. {
  75. var vcam = VirtualCamera.FollowTargetAsVcam;
  76. if (vcam != null)
  77. return vcam.State.FinalOrientation;
  78. Transform target = FollowTarget;
  79. if (target != null)
  80. return TargetPositionCache.GetTargetRotation(target);
  81. return Quaternion.identity;
  82. }
  83. }
  84. /// <summary>Get LookAt target as ICinemachineTargetGroup, or null if target is not a group</summary>
  85. public ICinemachineTargetGroup AbstractLookAtTargetGroup => VirtualCamera.AbstractLookAtTargetGroup;
  86. /// <summary>Get LookAt target as CinemachineTargetGroup, or null if target is not a CinemachineTargetGroup</summary>
  87. public CinemachineTargetGroup LookAtTargetGroup => AbstractLookAtTargetGroup as CinemachineTargetGroup;
  88. /// <summary>Get the position of the LookAt target. Special handling: If the LookAt target is
  89. /// a VirtualCamera, returns the vcam State's position, not the transform's position</summary>
  90. public Vector3 LookAtTargetPosition
  91. {
  92. get
  93. {
  94. var vcam = VirtualCamera.LookAtTargetAsVcam;
  95. if (vcam != null)
  96. return vcam.State.FinalPosition;
  97. Transform target = LookAtTarget;
  98. if (target != null)
  99. return TargetPositionCache.GetTargetPosition(target);
  100. return Vector3.zero;
  101. }
  102. }
  103. /// <summary>Get the rotation of the LookAt target. Special handling: If the LookAt target is
  104. /// a VirtualCamera, returns the vcam State's rotation, not the transform's rotation</summary>
  105. public Quaternion LookAtTargetRotation
  106. {
  107. get
  108. {
  109. var vcam = VirtualCamera.LookAtTargetAsVcam;
  110. if (vcam != null)
  111. return vcam.State.FinalOrientation;
  112. Transform target = LookAtTarget;
  113. if (target != null)
  114. return TargetPositionCache.GetTargetRotation(target);
  115. return Quaternion.identity;
  116. }
  117. }
  118. /// <summary>Returns the owner vcam's CameraState.</summary>
  119. public CameraState VcamState
  120. {
  121. get
  122. {
  123. CinemachineVirtualCameraBase vcam = VirtualCamera;
  124. return vcam == null ? CameraState.Default : vcam.State;
  125. }
  126. }
  127. /// <summary>Returns true if this object is enabled and set up to produce results.</summary>
  128. public abstract bool IsValid { get; }
  129. /// <summary>Override this to do such things as offset the RefereceLookAt.
  130. /// Base class implementation does nothing.</summary>
  131. /// <param name="curState">Input state that must be mutated</param>
  132. /// <param name="deltaTime">Current effective deltaTime</param>
  133. public virtual void PrePipelineMutateCameraState(ref CameraState curState, float deltaTime) {}
  134. /// <summary>What part of the pipeline this fits into</summary>
  135. public abstract CinemachineCore.Stage Stage { get; }
  136. /// <summary>Special for Body Stage compoments that want to be applied after Aim
  137. /// stage because they use the aim as inout for the procedural placement</summary>
  138. public virtual bool BodyAppliesAfterAim { get { return false; } }
  139. /// <summary>Mutates the camera state. This state will later be applied to the camera.</summary>
  140. /// <param name="curState">Input state that must be mutated</param>
  141. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than 0)</param>
  142. public abstract void MutateCameraState(ref CameraState curState, float deltaTime);
  143. /// <summary>Notification that this virtual camera is going live.
  144. /// Base class implementation does nothing.</summary>
  145. /// <param name="fromCam">The camera being deactivated. May be null.</param>
  146. /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
  147. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than or equal to 0)</param>
  148. /// <param name="transitionParams">Transition settings for this vcam</param>
  149. /// <returns>True if the vcam should do an internal update as a result of this call</returns>
  150. public virtual bool OnTransitionFromCamera(
  151. ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime,
  152. ref CinemachineVirtualCameraBase.TransitionParams transitionParams)
  153. { return false; }
  154. /// <summary>This is called to notify the component that a target got warped,
  155. /// so that the component can update its internal state to make the camera
  156. /// also warp seamlessy. Base class implementation does nothing.</summary>
  157. /// <param name="target">The object that was warped</param>
  158. /// <param name="positionDelta">The amount the target's position changed</param>
  159. public virtual void OnTargetObjectWarped(Transform target, Vector3 positionDelta) {}
  160. /// <summary>
  161. /// Force the virtual camera to assume a given position and orientation.
  162. /// Procedural placement then takes over.
  163. /// Base class implementation does nothing.</summary>
  164. /// <param name="pos">Worldspace pposition to take</param>
  165. /// <param name="rot">Worldspace orientation to take</param>
  166. public virtual void ForceCameraPosition(Vector3 pos, Quaternion rot) {}
  167. /// <summary>
  168. /// Report maximum damping time needed for this component.
  169. /// Only used in editor for timeline scrubbing.
  170. /// </summary>
  171. /// <returns>Highest damping setting in this component</returns>
  172. public virtual float GetMaxDampTime() { return 0; }
  173. /// <summary>Components that require user input should implement this and return true.</summary>
  174. public virtual bool RequiresUserInput => false;
  175. }
  176. }