CinemachineComponentBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.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.LookAt;
  41. }
  42. }
  43. private Transform mCachedFollowTarget;
  44. private CinemachineVirtualCameraBase mCachedFollowTargetVcam;
  45. private ICinemachineTargetGroup mCachedFollowTargetGroup;
  46. void UpdateFollowTargetCache()
  47. {
  48. mCachedFollowTargetVcam = null;
  49. mCachedFollowTargetGroup = null;
  50. mCachedFollowTarget = FollowTarget;
  51. if (mCachedFollowTarget != null)
  52. {
  53. mCachedFollowTargetVcam = mCachedFollowTarget.GetComponent<CinemachineVirtualCameraBase>();
  54. mCachedFollowTargetGroup = mCachedFollowTarget.GetComponent<ICinemachineTargetGroup>();
  55. }
  56. }
  57. /// <summary>Get Follow target as ICinemachineTargetGroup, or null if target is not a group</summary>
  58. public ICinemachineTargetGroup AbstractFollowTargetGroup
  59. {
  60. get
  61. {
  62. if (FollowTarget != mCachedFollowTarget)
  63. UpdateFollowTargetCache();
  64. return mCachedFollowTargetGroup;
  65. }
  66. }
  67. /// <summary>Get Follow target as CinemachineTargetGroup, or null if target is not a CinemachineTargetGroup</summary>
  68. public CinemachineTargetGroup FollowTargetGroup
  69. {
  70. get { return AbstractFollowTargetGroup as CinemachineTargetGroup; }
  71. }
  72. /// <summary>Get the position of the Follow target. Special handling: If the Follow target is
  73. /// a VirtualCamera, returns the vcam State's position, not the transform's position</summary>
  74. public Vector3 FollowTargetPosition
  75. {
  76. get
  77. {
  78. Transform target = FollowTarget;
  79. if (target != mCachedFollowTarget)
  80. UpdateFollowTargetCache();
  81. if (mCachedFollowTargetVcam != null)
  82. return mCachedFollowTargetVcam.State.FinalPosition;
  83. if (target != null)
  84. return TargetPositionCache.GetTargetPosition(target);
  85. return Vector3.zero;
  86. }
  87. }
  88. /// <summary>Get the rotation of the Follow target. Special handling: If the Follow target is
  89. /// a VirtualCamera, returns the vcam State's rotation, not the transform's rotation</summary>
  90. public Quaternion FollowTargetRotation
  91. {
  92. get
  93. {
  94. Transform target = FollowTarget;
  95. if (target != mCachedFollowTarget)
  96. {
  97. mCachedFollowTargetVcam = null;
  98. mCachedFollowTarget = target;
  99. if (target != null)
  100. mCachedFollowTargetVcam = target.GetComponent<CinemachineVirtualCameraBase>();
  101. }
  102. if (mCachedFollowTargetVcam != null)
  103. return mCachedFollowTargetVcam.State.FinalOrientation;
  104. if (target != null)
  105. return TargetPositionCache.GetTargetRotation(target);
  106. return Quaternion.identity;
  107. }
  108. }
  109. private Transform mCachedLookAtTarget;
  110. private CinemachineVirtualCameraBase mCachedLookAtTargetVcam;
  111. private ICinemachineTargetGroup mCachedLookAtTargetGroup;
  112. void UpdateLookAtTargetCache()
  113. {
  114. mCachedLookAtTargetVcam = null;
  115. mCachedLookAtTargetGroup = null;
  116. mCachedLookAtTarget = LookAtTarget;
  117. if (mCachedLookAtTarget != null)
  118. {
  119. mCachedLookAtTargetVcam = mCachedLookAtTarget.GetComponent<CinemachineVirtualCameraBase>();
  120. mCachedLookAtTargetGroup = mCachedLookAtTarget.GetComponent<ICinemachineTargetGroup>();
  121. }
  122. }
  123. /// <summary>Get LookAt target as ICinemachineTargetGroup, or null if target is not a group</summary>
  124. public ICinemachineTargetGroup AbstractLookAtTargetGroup
  125. {
  126. get
  127. {
  128. if (LookAtTarget != mCachedLookAtTarget)
  129. UpdateLookAtTargetCache();
  130. return mCachedLookAtTargetGroup;
  131. }
  132. }
  133. /// <summary>Get LookAt target as CinemachineTargetGroup, or null if target is not a CinemachineTargetGroup</summary>
  134. public CinemachineTargetGroup LookAtTargetGroup
  135. {
  136. get { return AbstractLookAtTargetGroup as CinemachineTargetGroup; }
  137. }
  138. /// <summary>Get the position of the LookAt target. Special handling: If the LookAt target is
  139. /// a VirtualCamera, returns the vcam State's position, not the transform's position</summary>
  140. public Vector3 LookAtTargetPosition
  141. {
  142. get
  143. {
  144. Transform target = LookAtTarget;
  145. if (target != mCachedLookAtTarget)
  146. UpdateLookAtTargetCache();
  147. if (mCachedLookAtTargetVcam != null)
  148. return mCachedLookAtTargetVcam.State.FinalPosition;
  149. if (target != null)
  150. return TargetPositionCache.GetTargetPosition(target);
  151. return Vector3.zero;
  152. }
  153. }
  154. /// <summary>Get the rotation of the LookAt target. Special handling: If the LookAt target is
  155. /// a VirtualCamera, returns the vcam State's rotation, not the transform's rotation</summary>
  156. public Quaternion LookAtTargetRotation
  157. {
  158. get
  159. {
  160. Transform target = LookAtTarget;
  161. if (target != mCachedLookAtTarget)
  162. UpdateLookAtTargetCache();
  163. if (mCachedLookAtTargetVcam != null)
  164. return mCachedLookAtTargetVcam.State.FinalOrientation;
  165. if (target != null)
  166. return TargetPositionCache.GetTargetRotation(target);
  167. return Quaternion.identity;
  168. }
  169. }
  170. /// <summary>Returns the owner vcam's CameraState.</summary>
  171. public CameraState VcamState
  172. {
  173. get
  174. {
  175. CinemachineVirtualCameraBase vcam = VirtualCamera;
  176. return vcam == null ? CameraState.Default : vcam.State;
  177. }
  178. }
  179. /// <summary>Returns true if this object is enabled and set up to produce results.</summary>
  180. public abstract bool IsValid { get; }
  181. /// <summary>Override this to do such things as offset the RefereceLookAt.
  182. /// Base class implementation does nothing.</summary>
  183. /// <param name="curState">Input state that must be mutated</param>
  184. /// <param name="deltaTime">Current effective deltaTime</param>
  185. public virtual void PrePipelineMutateCameraState(ref CameraState curState, float deltaTime) {}
  186. /// <summary>What part of the pipeline this fits into</summary>
  187. public abstract CinemachineCore.Stage Stage { get; }
  188. /// <summary>Special for Body Stage compoments that want to be applied after Aim
  189. /// stage because they use the aim as inout for the procedural placement</summary>
  190. public virtual bool BodyAppliesAfterAim { get { return false; } }
  191. /// <summary>Mutates the camera state. This state will later be applied to the camera.</summary>
  192. /// <param name="curState">Input state that must be mutated</param>
  193. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than 0)</param>
  194. public abstract void MutateCameraState(ref CameraState curState, float deltaTime);
  195. /// <summary>Notification that this virtual camera is going live.
  196. /// Base class implementation does nothing.</summary>
  197. /// <param name="fromCam">The camera being deactivated. May be null.</param>
  198. /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
  199. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than or equal to 0)</param>
  200. /// <param name="transitionParams">Transition settings for this vcam</param>
  201. /// <returns>True if the vcam should do an internal update as a result of this call</returns>
  202. public virtual bool OnTransitionFromCamera(
  203. ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime,
  204. ref CinemachineVirtualCameraBase.TransitionParams transitionParams)
  205. { return false; }
  206. /// <summary>This is called to notify the component that a target got warped,
  207. /// so that the component can update its internal state to make the camera
  208. /// also warp seamlessy. Base class implementation does nothing.</summary>
  209. /// <param name="target">The object that was warped</param>
  210. /// <param name="positionDelta">The amount the target's position changed</param>
  211. public virtual void OnTargetObjectWarped(Transform target, Vector3 positionDelta) {}
  212. /// <summary>
  213. /// Force the virtual camera to assume a given position and orientation.
  214. /// Procedural placement then takes over.
  215. /// Base class implementation does nothing.</summary>
  216. /// <param name="pos">Worldspace pposition to take</param>
  217. /// <param name="rot">Worldspace orientation to take</param>
  218. public virtual void ForceCameraPosition(Vector3 pos, Quaternion rot) {}
  219. /// <summary>
  220. /// Report maximum damping time needed for this component.
  221. /// Only used in editor for timeline scrubbing.
  222. /// </summary>
  223. /// <returns>Highest damping setting in this component</returns>
  224. public virtual float GetMaxDampTime() { return 0; }
  225. }
  226. }