CinemachineExternalCamera.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Cinemachine.Utility;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. namespace Cinemachine
  5. {
  6. /// <summary>
  7. /// This component will expose a non-cinemachine camera to the cinemachine system,
  8. /// allowing it to participate in blends.
  9. /// Just add it as a component alongside an existing Unity Camera component.
  10. /// </summary>
  11. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  12. [RequireComponent(typeof(Camera)), DisallowMultipleComponent]
  13. [AddComponentMenu("Cinemachine/CinemachineExternalCamera")]
  14. [ExecuteAlways]
  15. [HelpURL(Documentation.BaseURL + "manual/CinemachineExternalCamera.html")]
  16. public class CinemachineExternalCamera : CinemachineVirtualCameraBase
  17. {
  18. /// <summary>The object that the camera is looking at.</summary>
  19. [Tooltip("The object that the camera is looking at. Setting this will improve the "
  20. + "quality of the blends to and from this camera")]
  21. [NoSaveDuringPlay]
  22. [VcamTargetProperty]
  23. public Transform m_LookAt = null;
  24. private Camera m_Camera;
  25. private CameraState m_State = CameraState.Default;
  26. /// <summary>Get the CameraState, as we are able to construct one from the Unity Camera</summary>
  27. public override CameraState State { get { return m_State; } }
  28. /// <summary>The object that the camera is looking at</summary>
  29. override public Transform LookAt
  30. {
  31. get { return m_LookAt; }
  32. set { m_LookAt = value; }
  33. }
  34. /// <summary>This vcam defines no targets</summary>
  35. override public Transform Follow { get; set; }
  36. /// <summary>Hint for blending positions to and from this virtual camera</summary>
  37. [Tooltip("Hint for blending positions to and from this virtual camera")]
  38. [FormerlySerializedAs("m_PositionBlending")]
  39. public BlendHint m_BlendHint = BlendHint.None;
  40. /// <summary>Internal use only. Do not call this method</summary>
  41. /// <param name="worldUp">Effective world up</param>
  42. /// <param name="deltaTime">Effective deltaTime</param>
  43. public override void InternalUpdateCameraState(Vector3 worldUp, float deltaTime)
  44. {
  45. // Get the state from the camera
  46. if (m_Camera == null)
  47. {
  48. #if UNITY_2019_2_OR_NEWER
  49. TryGetComponent(out m_Camera);
  50. #else
  51. m_Camera = GetComponent<Camera>();
  52. #endif
  53. }
  54. m_State = CameraState.Default;
  55. m_State.RawPosition = transform.position;
  56. m_State.RawOrientation = transform.rotation;
  57. m_State.ReferenceUp = m_State.RawOrientation * Vector3.up;
  58. if (m_Camera != null)
  59. m_State.Lens = LensSettings.FromCamera(m_Camera);
  60. if (m_LookAt != null)
  61. {
  62. m_State.ReferenceLookAt = m_LookAt.transform.position;
  63. Vector3 dir = m_State.ReferenceLookAt - State.RawPosition;
  64. if (!dir.AlmostZero())
  65. m_State.ReferenceLookAt = m_State.RawPosition + Vector3.Project(
  66. dir, State.RawOrientation * Vector3.forward);
  67. }
  68. ApplyPositionBlendMethod(ref m_State, m_BlendHint);
  69. InvokePostPipelineStageCallback(this, CinemachineCore.Stage.Finalize, ref m_State, deltaTime);
  70. }
  71. }
  72. }