CinemachineExternalCamera.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #if UNITY_2018_3_OR_NEWER
  15. [ExecuteAlways]
  16. #else
  17. [ExecuteInEditMode]
  18. #endif
  19. [HelpURL(Documentation.BaseURL + "manual/CinemachineExternalCamera.html")]
  20. public class CinemachineExternalCamera : CinemachineVirtualCameraBase
  21. {
  22. /// <summary>The object that the camera is looking at.</summary>
  23. [Tooltip("The object that the camera is looking at. Setting this will improve the "
  24. + "quality of the blends to and from this camera")]
  25. [NoSaveDuringPlay]
  26. [VcamTargetProperty]
  27. public Transform m_LookAt = null;
  28. private Camera m_Camera;
  29. private CameraState m_State = CameraState.Default;
  30. /// <summary>Get the CameraState, as we are able to construct one from the Unity Camera</summary>
  31. public override CameraState State { get { return m_State; } }
  32. /// <summary>The object that the camera is looking at</summary>
  33. override public Transform LookAt
  34. {
  35. get { return m_LookAt; }
  36. set { m_LookAt = value; }
  37. }
  38. /// <summary>This vcam defines no targets</summary>
  39. override public Transform Follow { get; set; }
  40. /// <summary>Hint for blending positions to and from this virtual camera</summary>
  41. [Tooltip("Hint for blending positions to and from this virtual camera")]
  42. [FormerlySerializedAs("m_PositionBlending")]
  43. public BlendHint m_BlendHint = BlendHint.None;
  44. /// <summary>Internal use only. Do not call this method</summary>
  45. /// <param name="worldUp">Effective world up</param>
  46. /// <param name="deltaTime">Effective deltaTime</param>
  47. public override void InternalUpdateCameraState(Vector3 worldUp, float deltaTime)
  48. {
  49. // Get the state from the camera
  50. if (m_Camera == null)
  51. {
  52. #if UNITY_2019_2_OR_NEWER
  53. TryGetComponent(out m_Camera);
  54. #else
  55. m_Camera = GetComponent<Camera>();
  56. #endif
  57. }
  58. m_State = CameraState.Default;
  59. m_State.RawPosition = transform.position;
  60. m_State.RawOrientation = transform.rotation;
  61. m_State.ReferenceUp = m_State.RawOrientation * Vector3.up;
  62. if (m_Camera != null)
  63. m_State.Lens = LensSettings.FromCamera(m_Camera);
  64. if (m_LookAt != null)
  65. {
  66. m_State.ReferenceLookAt = m_LookAt.transform.position;
  67. Vector3 dir = m_State.ReferenceLookAt - State.RawPosition;
  68. if (!dir.AlmostZero())
  69. m_State.ReferenceLookAt = m_State.RawPosition + Vector3.Project(
  70. dir, State.RawOrientation * Vector3.forward);
  71. }
  72. ApplyPositionBlendMethod(ref m_State, m_BlendHint);
  73. InvokePostPipelineStageCallback(this, CinemachineCore.Stage.Finalize, ref m_State, deltaTime);
  74. }
  75. }
  76. }