CinemachineNewVirtualCamera.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #if CINEMACHINE_EXPERIMENTAL_VCAM
  2. using UnityEngine;
  3. using System;
  4. using System.Linq;
  5. namespace Cinemachine
  6. {
  7. /// <summary>
  8. ///
  9. /// NOTE: THIS CLASS IS EXPERIMENTAL, AND NOT FOR PUBLIC USE
  10. ///
  11. /// Lighter-weight version of the CinemachineVirtualCamera.
  12. ///
  13. /// </summary>
  14. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  15. [DisallowMultipleComponent]
  16. [ExecuteAlways]
  17. [AddComponentMenu("Cinemachine/CinemachineNewVirtualCamera")]
  18. public class CinemachineNewVirtualCamera : CinemachineVirtualCameraBase
  19. {
  20. /// <summary>Object for the camera children to look at (the aim target)</summary>
  21. [Tooltip("Object for the camera children to look at (the aim target).")]
  22. [NoSaveDuringPlay]
  23. [VcamTargetProperty]
  24. public Transform m_LookAt = null;
  25. /// <summary>Object for the camera children wants to move with (the body target)</summary>
  26. [Tooltip("Object for the camera children wants to move with (the body target).")]
  27. [NoSaveDuringPlay]
  28. [VcamTargetProperty]
  29. public Transform m_Follow = null;
  30. /// <summary>Specifies the LensSettings of this Virtual Camera.
  31. /// These settings will be transferred to the Unity camera when the vcam is live.</summary>
  32. [Tooltip("Specifies the lens properties of this Virtual Camera. This generally mirrors the "
  33. + "Unity Camera's lens settings, and will be used to drive the Unity camera when the vcam is active.")]
  34. public LensSettings m_Lens = LensSettings.Default;
  35. /// <summary> Collection of parameters that influence how this virtual camera transitions from
  36. /// other virtual cameras </summary>
  37. public TransitionParams m_Transitions;
  38. /// <summary>Updates the child rig cache</summary>
  39. protected override void OnEnable()
  40. {
  41. base.OnEnable();
  42. InvalidateComponentCache();
  43. }
  44. void Reset()
  45. {
  46. DestroyComponents();
  47. }
  48. /// <summary>Validates the settings avter inspector edit</summary>
  49. protected override void OnValidate()
  50. {
  51. base.OnValidate();
  52. m_Lens.Validate();
  53. }
  54. /// <summary>The camera state, which will be a blend of the child rig states</summary>
  55. override public CameraState State { get { return m_State; } }
  56. /// <summary>The camera state, which will be a blend of the child rig states</summary>
  57. protected CameraState m_State = CameraState.Default;
  58. /// <summary>Get the current LookAt target. Returns parent's LookAt if parent
  59. /// is non-null and no specific LookAt defined for this camera</summary>
  60. override public Transform LookAt
  61. {
  62. get { return ResolveLookAt(m_LookAt); }
  63. set { m_LookAt = value; }
  64. }
  65. /// <summary>Get the current Follow target. Returns parent's Follow if parent
  66. /// is non-null and no specific Follow defined for this camera</summary>
  67. override public Transform Follow
  68. {
  69. get { return ResolveFollow(m_Follow); }
  70. set { m_Follow = value; }
  71. }
  72. /// <summary>This is called to notify the vcam that a target got warped,
  73. /// so that the vcam can update its internal state to make the camera
  74. /// also warp seamlessy.</summary>
  75. /// <param name="target">The object that was warped</param>
  76. /// <param name="positionDelta">The amount the target's position changed</param>
  77. public override void OnTargetObjectWarped(Transform target, Vector3 positionDelta)
  78. {
  79. if (target == Follow)
  80. {
  81. transform.position += positionDelta;
  82. m_State.RawPosition += positionDelta;
  83. }
  84. UpdateComponentCache();
  85. for (int i = 0; i < m_Components.Length; ++i)
  86. {
  87. if (m_Components[i] != null)
  88. m_Components[i].OnTargetObjectWarped(target, positionDelta);
  89. }
  90. base.OnTargetObjectWarped(target, positionDelta);
  91. }
  92. /// <summary>
  93. /// Force the virtual camera to assume a given position and orientation
  94. /// </summary>
  95. /// <param name="pos">Worldspace pposition to take</param>
  96. /// <param name="rot">Worldspace orientation to take</param>
  97. public override void ForceCameraPosition(Vector3 pos, Quaternion rot)
  98. {
  99. PreviousStateIsValid = false;
  100. transform.position = pos;
  101. transform.rotation = rot;
  102. m_State.RawPosition = pos;
  103. m_State.RawOrientation = rot;
  104. UpdateComponentCache();
  105. for (int i = 0; i < m_Components.Length; ++i)
  106. if (m_Components[i] != null)
  107. m_Components[i].ForceCameraPosition(pos, rot);
  108. base.ForceCameraPosition(pos, rot);
  109. }
  110. /// <summary>
  111. /// Query components and extensions for the maximum damping time.
  112. /// </summary>
  113. /// <returns>Highest damping setting in this vcam</returns>
  114. public override float GetMaxDampTime()
  115. {
  116. float maxDamp = base.GetMaxDampTime();
  117. UpdateComponentCache();
  118. for (int i = 0; i < m_Components.Length; ++i)
  119. if (m_Components[i] != null)
  120. maxDamp = Mathf.Max(maxDamp, m_Components[i].GetMaxDampTime());
  121. return maxDamp;
  122. }
  123. /// <summary>If we are transitioning from another FreeLook, grab the axis values from it.</summary>
  124. /// <param name="fromCam">The camera being deactivated. May be null.</param>
  125. /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
  126. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than or equal to 0)</param>
  127. public override void OnTransitionFromCamera(
  128. ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime)
  129. {
  130. base.OnTransitionFromCamera(fromCam, worldUp, deltaTime);
  131. InvokeOnTransitionInExtensions(fromCam, worldUp, deltaTime);
  132. bool forceUpdate = false;
  133. if (m_Transitions.m_InheritPosition && fromCam != null
  134. && !CinemachineCore.Instance.IsLiveInBlend(this))
  135. {
  136. ForceCameraPosition(fromCam.State.FinalPosition, fromCam.State.FinalOrientation);
  137. }
  138. UpdateComponentCache();
  139. for (int i = 0; i < m_Components.Length; ++i)
  140. {
  141. if (m_Components[i] != null
  142. && m_Components[i].OnTransitionFromCamera(
  143. fromCam, worldUp, deltaTime, ref m_Transitions))
  144. forceUpdate = true;
  145. }
  146. if (forceUpdate)
  147. {
  148. InternalUpdateCameraState(worldUp, deltaTime);
  149. InternalUpdateCameraState(worldUp, deltaTime);
  150. }
  151. else
  152. UpdateCameraState(worldUp, deltaTime);
  153. if (m_Transitions.m_OnCameraLive != null)
  154. m_Transitions.m_OnCameraLive.Invoke(this, fromCam);
  155. }
  156. /// <summary>Internal use only. Called by CinemachineCore at designated update time
  157. /// so the vcam can position itself and track its targets. All 3 child rigs are updated,
  158. /// and a blend calculated, depending on the value of the Y axis.</summary>
  159. /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
  160. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than 0)</param>
  161. override public void InternalUpdateCameraState(Vector3 worldUp, float deltaTime)
  162. {
  163. UpdateTargetCache();
  164. FollowTargetAttachment = 1;
  165. LookAtTargetAttachment = 1;
  166. // Initialize the camera state, in case the game object got moved in the editor
  167. m_State = PullStateFromVirtualCamera(worldUp, ref m_Lens);
  168. // Do our stuff
  169. SetReferenceLookAtTargetInState(ref m_State);
  170. InvokeComponentPipeline(ref m_State, worldUp, deltaTime);
  171. ApplyPositionBlendMethod(ref m_State, m_Transitions.m_BlendHint);
  172. // Push the raw position back to the game object's transform, so it
  173. // moves along with the camera.
  174. if (Follow != null)
  175. transform.position = State.RawPosition;
  176. if (LookAt != null)
  177. transform.rotation = State.RawOrientation;
  178. // Signal that it's all done
  179. InvokePostPipelineStageCallback(this, CinemachineCore.Stage.Finalize, ref m_State, deltaTime);
  180. PreviousStateIsValid = true;
  181. }
  182. /// <summary>
  183. /// Returns true, when the vcam has extensions or components that require input.
  184. /// </summary>
  185. internal override bool RequiresUserInput()
  186. {
  187. return base.RequiresUserInput() ||
  188. m_Components != null && m_Components.Any(t => t != null && t.RequiresUserInput);
  189. }
  190. private Transform mCachedLookAtTarget;
  191. private CinemachineVirtualCameraBase mCachedLookAtTargetVcam;
  192. /// <summary>Set the state's refeenceLookAt target to our lookAt, with some smarts
  193. /// in case our LookAt points to a vcam</summary>
  194. protected void SetReferenceLookAtTargetInState(ref CameraState state)
  195. {
  196. Transform lookAtTarget = LookAt;
  197. if (lookAtTarget != mCachedLookAtTarget)
  198. {
  199. mCachedLookAtTarget = lookAtTarget;
  200. mCachedLookAtTargetVcam = null;
  201. if (lookAtTarget != null)
  202. mCachedLookAtTargetVcam = lookAtTarget.GetComponent<CinemachineVirtualCameraBase>();
  203. }
  204. if (lookAtTarget != null)
  205. {
  206. if (mCachedLookAtTargetVcam != null)
  207. state.ReferenceLookAt = mCachedLookAtTargetVcam.State.FinalPosition;
  208. else
  209. state.ReferenceLookAt = TargetPositionCache.GetTargetPosition(lookAtTarget);
  210. }
  211. }
  212. protected CameraState InvokeComponentPipeline(
  213. ref CameraState state, Vector3 worldUp, float deltaTime)
  214. {
  215. UpdateComponentCache();
  216. // Extensions first
  217. InvokePrePipelineMutateCameraStateCallback(this, ref state, deltaTime);
  218. // Apply the component pipeline
  219. for (CinemachineCore.Stage stage = CinemachineCore.Stage.Body;
  220. stage <= CinemachineCore.Stage.Finalize; ++stage)
  221. {
  222. var c = m_Components[(int)stage];
  223. if (c != null)
  224. c.PrePipelineMutateCameraState(ref state, deltaTime);
  225. }
  226. CinemachineComponentBase postAimBody = null;
  227. for (CinemachineCore.Stage stage = CinemachineCore.Stage.Body;
  228. stage <= CinemachineCore.Stage.Finalize; ++stage)
  229. {
  230. var c = m_Components[(int)stage];
  231. if (c != null)
  232. {
  233. if (stage == CinemachineCore.Stage.Body && c.BodyAppliesAfterAim)
  234. {
  235. postAimBody = c;
  236. continue; // do the body stage of the pipeline after Aim
  237. }
  238. c.MutateCameraState(ref state, deltaTime);
  239. }
  240. InvokePostPipelineStageCallback(this, stage, ref state, deltaTime);
  241. if (stage == CinemachineCore.Stage.Aim)
  242. {
  243. if (c == null)
  244. state.BlendHint |= CameraState.BlendHintValue.IgnoreLookAtTarget; // no aim
  245. // If we have saved a Body for after Aim, do it now
  246. if (postAimBody != null)
  247. {
  248. postAimBody.MutateCameraState(ref state, deltaTime);
  249. InvokePostPipelineStageCallback(this, CinemachineCore.Stage.Body, ref state, deltaTime);
  250. }
  251. }
  252. }
  253. return state;
  254. }
  255. // Component Cache - serialized only for copy/paste
  256. [SerializeField, HideInInspector, NoSaveDuringPlay]
  257. CinemachineComponentBase[] m_Components;
  258. /// For inspector
  259. internal CinemachineComponentBase[] ComponentCache
  260. {
  261. get
  262. {
  263. UpdateComponentCache();
  264. return m_Components;
  265. }
  266. }
  267. /// <summary>Call this when CinemachineCompponentBase compponents are added
  268. /// or removed. If you don't call this, you may get null reference errors.</summary>
  269. public void InvalidateComponentCache()
  270. {
  271. m_Components = null;
  272. }
  273. /// <summary>Bring the component cache up to date if needed</summary>
  274. protected void UpdateComponentCache()
  275. {
  276. #if UNITY_EDITOR
  277. // Special case: if we have serialized in with some other game object's
  278. // components, then we have just been pasted so we should clone them
  279. for (int i = 0; m_Components != null && i < m_Components.Length; ++i)
  280. {
  281. if (m_Components[i] != null && m_Components[i].gameObject != gameObject)
  282. {
  283. var copyFrom = m_Components;
  284. DestroyComponents();
  285. CopyComponents(copyFrom);
  286. break;
  287. }
  288. }
  289. #endif
  290. if (m_Components != null && m_Components.Length == (int)CinemachineCore.Stage.Finalize + 1)
  291. return; // up to date
  292. m_Components = new CinemachineComponentBase[(int)CinemachineCore.Stage.Finalize + 1];
  293. var existing = GetComponents<CinemachineComponentBase>();
  294. for (int i = 0; existing != null && i < existing.Length; ++i)
  295. m_Components[(int)existing[i].Stage] = existing[i];
  296. for (int i = 0; i < m_Components.Length; ++i)
  297. {
  298. if (m_Components[i] != null)
  299. {
  300. if (CinemachineCore.sShowHiddenObjects)
  301. m_Components[i].hideFlags &= ~HideFlags.HideInInspector;
  302. else
  303. m_Components[i].hideFlags |= HideFlags.HideInInspector;
  304. }
  305. }
  306. OnComponentCacheUpdated();
  307. }
  308. /// <summary>Notification that the component cache has just been update,
  309. /// in case a subclass needs to do something extra</summary>
  310. protected virtual void OnComponentCacheUpdated() {}
  311. /// <summary>Destroy all the CinmachineComponentBase components</summary>
  312. protected void DestroyComponents()
  313. {
  314. var existing = GetComponents<CinemachineComponentBase>();
  315. for (int i = 0; i < existing.Length; ++i)
  316. {
  317. #if UNITY_EDITOR
  318. UnityEditor.Undo.DestroyObjectImmediate(existing[i]);
  319. #else
  320. UnityEngine.Object.Destroy(existing[i]);
  321. #endif
  322. }
  323. InvalidateComponentCache();
  324. }
  325. #if UNITY_EDITOR
  326. // This gets called when user pastes component values
  327. void CopyComponents(CinemachineComponentBase[] copyFrom)
  328. {
  329. foreach (CinemachineComponentBase c in copyFrom)
  330. {
  331. if (c != null)
  332. {
  333. Type type = c.GetType();
  334. var copy = UnityEditor.Undo.AddComponent(gameObject, type);
  335. UnityEditor.Undo.RecordObject(copy, "copying pipeline");
  336. System.Reflection.BindingFlags bindingAttr
  337. = System.Reflection.BindingFlags.Public
  338. | System.Reflection.BindingFlags.NonPublic
  339. | System.Reflection.BindingFlags.Instance;
  340. System.Reflection.FieldInfo[] fields = type.GetFields(bindingAttr);
  341. for (int i = 0; i < fields.Length; ++i)
  342. if (!fields[i].IsStatic)
  343. fields[i].SetValue(copy, fields[i].GetValue(c));
  344. }
  345. }
  346. }
  347. #endif
  348. /// Legacy support for an old API. GML todo: deprecate these methods
  349. /// <summary>Get the component set for a specific stage.</summary>
  350. /// <param name="stage">The stage for which we want the component</param>
  351. /// <returns>The Cinemachine component for that stage, or null if not defined</returns>
  352. public CinemachineComponentBase GetCinemachineComponent(CinemachineCore.Stage stage)
  353. {
  354. var cache = ComponentCache;
  355. var i = (int)stage;
  356. return i >= 0 && i < cache.Length ? cache[i] : null;
  357. }
  358. /// <summary>Get an existing component of a specific type from the cinemachine pipeline.</summary>
  359. public T GetCinemachineComponent<T>() where T : CinemachineComponentBase
  360. {
  361. var components = ComponentCache;
  362. foreach (var c in components)
  363. if (c is T)
  364. return c as T;
  365. return null;
  366. }
  367. /// <summary>Add a component to the cinemachine pipeline.</summary>
  368. public T AddCinemachineComponent<T>() where T : CinemachineComponentBase
  369. {
  370. var components = ComponentCache;
  371. T c = gameObject.AddComponent<T>();
  372. var oldC = components[(int)c.Stage];
  373. if (oldC != null)
  374. {
  375. oldC.enabled = false;
  376. RuntimeUtility.DestroyObject(oldC);
  377. }
  378. InvalidateComponentCache();
  379. return c;
  380. }
  381. /// <summary>Remove a component from the cinemachine pipeline.</summary>
  382. public void DestroyCinemachineComponent<T>() where T : CinemachineComponentBase
  383. {
  384. var components = ComponentCache;
  385. foreach (var c in components)
  386. {
  387. if (c is T)
  388. {
  389. c.enabled = false;
  390. RuntimeUtility.DestroyObject(c);
  391. InvalidateComponentCache();
  392. return;
  393. }
  394. }
  395. }
  396. // This prevents the sensor size from dirtying the scene in the event of aspect ratio change
  397. internal override void OnBeforeSerialize()
  398. {
  399. if (!m_Lens.IsPhysicalCamera)
  400. m_Lens.SensorSize = Vector2.one;
  401. }
  402. }
  403. }
  404. #endif