using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.Cinemachine
{
///
/// This component will expose a non-cinemachine camera to the cinemachine system,
/// allowing it to participate in blends.
/// Just add it as a component alongside an existing Unity Camera component.
///
[RequireComponent(typeof(Camera)), DisallowMultipleComponent]
[AddComponentMenu("Cinemachine/Cinemachine External Camera")]
[ExecuteAlways]
[HelpURL(Documentation.BaseURL + "manual/CinemachineExternalCamera.html")]
public class CinemachineExternalCamera : CinemachineVirtualCameraBase
{
/// The object that the camera is looking at. Setting this may improve the
/// quality of the blends to and from this camera
[Tooltip("The object that the camera is looking at. Setting this may improve the "
+ "quality of the blends to and from this camera")]
[NoSaveDuringPlay]
[FormerlySerializedAs("m_LookAt")]
public Transform LookAtTarget = null;
/// Hint for transitioning to and from this virtual camera
[Tooltip("Hint for transitioning to and from this virtual camera")]
[FormerlySerializedAs("m_PositionBlending")]
[FormerlySerializedAs("m_BlendHint")]
public CinemachineCore.BlendHints BlendHint = 0;
Camera m_Camera;
CameraState m_State = CameraState.Default;
/// Get the CameraState, as we are able to construct one from the Unity Camera
public override CameraState State => m_State;
/// The object that the camera is looking at
override public Transform LookAt
{
get => LookAtTarget;
set => LookAtTarget = value;
}
/// This vcam defines no targets
[HideInInspector]
public override Transform Follow { get; set; }
/// Internal use only. Do not call this method
/// Effective world up
/// Effective deltaTime
public override void InternalUpdateCameraState(Vector3 worldUp, float deltaTime)
{
// Get the state from the camera
if (m_Camera == null)
TryGetComponent(out m_Camera);
m_State = CameraState.Default;
m_State.RawPosition = transform.position;
m_State.RawOrientation = transform.rotation;
m_State.ReferenceUp = m_State.RawOrientation * Vector3.up;
if (m_Camera != null)
m_State.Lens = LensSettings.FromCamera(m_Camera);
if (LookAtTarget != null)
{
m_State.ReferenceLookAt = LookAtTarget.transform.position;
Vector3 dir = m_State.ReferenceLookAt - State.RawPosition;
if (!dir.AlmostZero())
m_State.ReferenceLookAt = m_State.RawPosition + Vector3.Project(
dir, State.RawOrientation * Vector3.forward);
}
m_State.BlendHint = (CameraState.BlendHints)BlendHint;
InvokePostPipelineStageCallback(this, CinemachineCore.Stage.Finalize, ref m_State, deltaTime);
}
}
}