using System;
using System.Collections.Generic;
using System.Linq;
using Cinemachine.Utility;
using UnityEngine;
using UnityEngine.Serialization;
namespace Cinemachine
{
///
/// Base class for a Monobehaviour that represents a Virtual Camera within the Unity scene.
///
/// This is intended to be attached to an empty Transform GameObject.
/// Inherited classes can be either standalone virtual cameras such
/// as CinemachineVirtualCamera, or meta-cameras such as
/// CinemachineClearShot or CinemachineFreeLook.
///
/// A CinemachineVirtualCameraBase exposes a Priority property. When the behaviour is
/// enabled in the game, the Virtual Camera is automatically placed in a queue
/// maintained by the static CinemachineCore singleton.
/// The queue is sorted by priority. When a Unity camera is equipped with a
/// CinemachineBrain behaviour, the brain will choose the camera
/// at the head of the queue. If you have multiple Unity cameras with CinemachineBrain
/// behaviours (say in a split-screen context), then you can filter the queue by
/// setting the culling flags on the virtual cameras. The culling mask of the
/// Unity Camera will then act as a filter for the brain. Apart from this,
/// there is nothing that prevents a virtual camera from controlling multiple
/// Unity cameras simultaneously.
///
[SaveDuringPlay]
public abstract class CinemachineVirtualCameraBase : MonoBehaviour, ICinemachineCamera, ISerializationCallbackReceiver
{
/// Inspector control - Use for hiding sections of the Inspector UI.
[HideInInspector, SerializeField, NoSaveDuringPlay]
public string[] m_ExcludedPropertiesInInspector = new string[] { "m_Script" };
/// Inspector control - Use for enabling sections of the Inspector UI.
[HideInInspector, SerializeField, NoSaveDuringPlay]
public CinemachineCore.Stage[] m_LockStageInInspector;
/// Version that was last streamed, for upgrading legacy
public int ValidatingStreamVersion
{
get { return m_OnValidateCalled ? m_ValidatingStreamVersion : CinemachineCore.kStreamingVersion; }
private set { m_ValidatingStreamVersion = value; }
}
private int m_ValidatingStreamVersion = 0;
private bool m_OnValidateCalled = false;
[HideInInspector, SerializeField, NoSaveDuringPlay]
private int m_StreamingVersion;
/// The priority will determine which camera becomes active based on the
/// state of other cameras and this camera. Higher numbers have greater priority.
///
[NoSaveDuringPlay]
[Tooltip("The priority will determine which camera becomes active based on the state of "
+ "other cameras and this camera. Higher numbers have greater priority.")]
public int m_Priority = 10;
/// A sequence number that represents object activation order of vcams.
/// Used for priority sorting.
internal int m_ActivationId;
///
/// This must be set every frame at the start of the pipeline to relax the virtual camera's
/// attachment to the target. Range is 0...1.
/// 1 is full attachment, and is the normal state.
/// 0 is no attachment, and virtual camera will behave as if no Follow
/// targets are set.
///
[NonSerialized]
public float FollowTargetAttachment;
///
/// This must be set every frame at the start of the pipeline to relax the virtual camera's
/// attachment to the target. Range is 0...1.
/// 1 is full attachment, and is the normal state.
/// 0 is no attachment, and virtual camera will behave as if no LookAt
/// targets are set.
///
[NonSerialized]
public float LookAtTargetAttachment;
///
/// How often to update a virtual camera when it is in Standby mode
///
public enum StandbyUpdateMode
{
/// Only update if the virtual camera is Live
Never,
/// Update the virtual camera every frame, even when it is not Live
Always,
/// Update the virtual camera occasionally, the exact frequency depends
/// on how many other virtual cameras are in Standby
RoundRobin
};
/// When the virtual camera is not live, this is how often the virtual camera will
/// be updated. Set this to tune for performance. Most of the time Never is fine, unless
/// the virtual camera is doing shot evaluation.
///
[Tooltip("When the virtual camera is not live, this is how often the virtual camera will be updated. "
+ "Set this to tune for performance. Most of the time Never is fine, "
+ "unless the virtual camera is doing shot evaluation.")]
public StandbyUpdateMode m_StandbyUpdate = StandbyUpdateMode.RoundRobin;
///
/// Query components and extensions for the maximum damping time.
/// Base class implementation queries extensions.
/// Only used in editor for timeline scrubbing.
///
/// Highest damping setting in this vcam
public virtual float GetMaxDampTime()
{
float maxDamp = 0;
if (mExtensions != null)
for (int i = 0; i < mExtensions.Count; ++i)
maxDamp = Mathf.Max(maxDamp, mExtensions[i].GetMaxDampTime());
return maxDamp;
}
/// Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.
/// This method takes the target attachment into account. For general
/// damping without consideration of target attachment, use Damper.Damp()
/// The amount that will be damped
/// The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage
/// The time over which to damp
/// The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.
public float DetachedFollowTargetDamp(float initial, float dampTime, float deltaTime)
{
dampTime = Mathf.Lerp(Mathf.Max(1, dampTime), dampTime, FollowTargetAttachment);
deltaTime = Mathf.Lerp(0, deltaTime, FollowTargetAttachment);
return Damper.Damp(initial, dampTime, deltaTime);
}
/// Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.
/// This method takes the target attachment into account. For general
/// damping without consideration of target attachment, use Damper.Damp()
/// The amount that will be damped
/// The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage
/// The time over which to damp
/// The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.
public Vector3 DetachedFollowTargetDamp(Vector3 initial, Vector3 dampTime, float deltaTime)
{
dampTime = Vector3.Lerp(Vector3.Max(Vector3.one, dampTime), dampTime, FollowTargetAttachment);
deltaTime = Mathf.Lerp(0, deltaTime, FollowTargetAttachment);
return Damper.Damp(initial, dampTime, deltaTime);
}
/// Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.
/// This method takes the target attachment into account. For general
/// damping without consideration of target attachment, use Damper.Damp()
/// The amount that will be damped
/// The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage
/// The time over which to damp
/// The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.
public Vector3 DetachedFollowTargetDamp(Vector3 initial, float dampTime, float deltaTime)
{
dampTime = Mathf.Lerp(Mathf.Max(1, dampTime), dampTime, FollowTargetAttachment);
deltaTime = Mathf.Lerp(0, deltaTime, FollowTargetAttachment);
return Damper.Damp(initial, dampTime, deltaTime);
}
/// Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.
/// This method takes the target attachment into account. For general
/// damping without consideration of target attachment, use Damper.Damp()
/// The amount that will be damped
/// The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage
/// The time over which to damp
/// The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.
public float DetachedLookAtTargetDamp(float initial, float dampTime, float deltaTime)
{
dampTime = Mathf.Lerp(Mathf.Max(1, dampTime), dampTime, LookAtTargetAttachment);
deltaTime = Mathf.Lerp(0, deltaTime, LookAtTargetAttachment);
return Damper.Damp(initial, dampTime, deltaTime);
}
/// Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.
/// This method takes the target attachment into account. For general
/// damping without consideration of target attachment, use Damper.Damp()
/// The amount that will be damped
/// The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage
/// The time over which to damp
/// The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.
public Vector3 DetachedLookAtTargetDamp(Vector3 initial, Vector3 dampTime, float deltaTime)
{
dampTime = Vector3.Lerp(Vector3.Max(Vector3.one, dampTime), dampTime, LookAtTargetAttachment);
deltaTime = Mathf.Lerp(0, deltaTime, LookAtTargetAttachment);
return Damper.Damp(initial, dampTime, deltaTime);
}
/// Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.
/// This method takes the target attachment into account. For general
/// damping without consideration of target attachment, use Damper.Damp()
/// The amount that will be damped
/// The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage
/// The time over which to damp
/// The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.
public Vector3 DetachedLookAtTargetDamp(Vector3 initial, float dampTime, float deltaTime)
{
dampTime = Mathf.Lerp(Mathf.Max(1, dampTime), dampTime, LookAtTargetAttachment);
deltaTime = Mathf.Lerp(0, deltaTime, LookAtTargetAttachment);
return Damper.Damp(initial, dampTime, deltaTime);
}
///
/// A delegate to hook into the state calculation pipeline.
/// This will be called after each pipeline stage, to allow others to hook into the pipeline.
/// See CinemachineCore.Stage.
///
/// The extension to add.
public virtual void AddExtension(CinemachineExtension extension)
{
if (mExtensions == null)
mExtensions = new List();
else
mExtensions.Remove(extension);
mExtensions.Add(extension);
}
/// Remove a Pipeline stage hook callback.
/// The extension to remove.
public virtual void RemoveExtension(CinemachineExtension extension)
{
if (mExtensions != null)
mExtensions.Remove(extension);
}
/// The extensions connected to this vcam
internal List mExtensions { get; private set; }
///
/// Invokes the PostPipelineStageDelegate for this camera, and up the hierarchy for all
/// parent cameras (if any).
/// Implementaion must be sure to call this after each pipeline stage, to allow
/// other services to hook into the pipeline.
/// See CinemachineCore.Stage.
///
/// The virtual camera being processed
/// The current pipeline stage
/// The current virtual camera state
/// The current applicable deltaTime
protected void InvokePostPipelineStageCallback(
CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage,
ref CameraState newState, float deltaTime)
{
if (mExtensions != null)
{
for (int i = 0; i < mExtensions.Count; ++i)
{
var e = mExtensions[i];
if (e == null)
{
// Object was deleted (possibly because of Undo in the editor)
mExtensions.RemoveAt(i);
--i;
}
else if (e.enabled)
e.InvokePostPipelineStageCallback(vcam, stage, ref newState, deltaTime);
}
}
CinemachineVirtualCameraBase parent = ParentCamera as CinemachineVirtualCameraBase;
if (parent != null)
parent.InvokePostPipelineStageCallback(vcam, stage, ref newState, deltaTime);
}
///
/// Invokes the PrePipelineMutateCameraStateCallback for this camera,
/// and up the hierarchy for all parent cameras (if any).
/// Implementaion must be sure to call this after each pipeline stage, to allow
/// other services to hook into the pipeline.
/// See CinemachineCore.Stage.
///
/// The virtual camera being processed
/// The current virtual camera state
/// The current applicable deltaTime
protected void InvokePrePipelineMutateCameraStateCallback(
CinemachineVirtualCameraBase vcam, ref CameraState newState, float deltaTime)
{
if (mExtensions != null)
{
for (int i = 0; i < mExtensions.Count; ++i)
{
var e = mExtensions[i];
if (e == null)
{
// Object was deleted (possibly because of Undo in the editor)
mExtensions.RemoveAt(i);
--i;
}
else if (e.enabled)
e.PrePipelineMutateCameraStateCallback(vcam, ref newState, deltaTime);
}
}
CinemachineVirtualCameraBase parent = ParentCamera as CinemachineVirtualCameraBase;
if (parent != null)
parent.InvokePrePipelineMutateCameraStateCallback(vcam, ref newState, deltaTime);
}
///
/// Invokes the OnTransitionFromCamera for all extensions on this camera
///
/// The camera being deactivated. May be null.
/// Default world Up, set by the CinemachineBrain
/// Delta time for time-based effects (ignore if less than or equal to 0)
/// True to request a vcam update of internal state
protected bool InvokeOnTransitionInExtensions(
ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime)
{
bool forceUpdate = false;
if (mExtensions != null)
{
for (int i = 0; i < mExtensions.Count; ++i)
{
var e = mExtensions[i];
if (e == null)
{
// Object was deleted (possibly because of Undo in the editor)
mExtensions.RemoveAt(i);
--i;
}
else if (e.enabled && e.OnTransitionFromCamera(fromCam, worldUp, deltaTime))
forceUpdate = true;
}
}
return forceUpdate;
}
/// Get the name of the Virtual Camera. Base implementation
/// returns the owner GameObject's name.
public string Name => name;
/// Gets a brief debug description of this virtual camera, for use when displayiong debug info
public virtual string Description => "";
/// Get the Priority of the virtual camera. This determines its placement
/// in the CinemachineCore's queue of eligible shots.
public int Priority {
get => m_Priority;
set => m_Priority = value;
}
/// Hint for blending to and from this virtual camera
public enum BlendHint
{
/// Standard linear position and aim blend
None,
/// Spherical blend about LookAt target position if there is a LookAt target, linear blend between LookAt targets
SphericalPosition,
/// Cylindrical blend about LookAt target position if there is a LookAt target (vertical co-ordinate is linearly interpolated), linear blend between LookAt targets
CylindricalPosition,
/// Standard linear position blend, radial blend between LookAt targets
ScreenSpaceAimWhenTargetsDiffer
}
/// Applies a position blend hint to a camera state
/// The state to apply the hint to
/// The hint to apply
protected void ApplyPositionBlendMethod(ref CameraState state, BlendHint hint)
{
switch (hint)
{
default:
break;
case BlendHint.SphericalPosition:
state.BlendHint |= CameraState.BlendHintValue.SphericalPositionBlend;
break;
case BlendHint.CylindricalPosition:
state.BlendHint |= CameraState.BlendHintValue.CylindricalPositionBlend;
break;
case BlendHint.ScreenSpaceAimWhenTargetsDiffer:
state.BlendHint |= CameraState.BlendHintValue.RadialAimBlend;
break;
}
}
/// The GameObject owner of the Virtual Camera behaviour.
public GameObject VirtualCameraGameObject
{
get
{
if (this == null)
return null; // object deleted
return gameObject;
}
}
/// Returns false if the object has been deleted
public bool IsValid => !(this == null);
/// The CameraState object holds all of the information
/// necessary to position the Unity camera. It is the output of this class.
public abstract CameraState State { get; }
/// Support for meta-virtual-cameras. This is the situation where a
/// virtual camera is in fact the public face of a private army of virtual cameras, which
/// it manages on its own. This method gets the VirtualCamera owner, if any.
/// Private armies are implemented as Transform children of the parent vcam.
public ICinemachineCamera ParentCamera
{
get
{
if (!mSlaveStatusUpdated || !Application.isPlaying)
UpdateSlaveStatus();
return m_parentVcam;
}
}
/// Check whether the vcam a live child of this camera.
/// This base class implementation always returns false.
/// The Virtual Camera to check
/// If truw, will only return true if this vcam is the dominat live child
/// True if the vcam is currently actively influencing the state of this vcam
public virtual bool IsLiveChild(ICinemachineCamera vcam, bool dominantChildOnly = false) { return false; }
/// Get the LookAt target for the Aim component in the Cinemachine pipeline.
public abstract Transform LookAt { get; set; }
/// Get the Follow target for the Body component in the Cinemachine pipeline.
public abstract Transform Follow { get; set; }
/// Set this to force the next update to ignore state from the previous frame.
/// This is useful, for example, if you want to cancel damping or other time-based processing.
public virtual bool PreviousStateIsValid { get; set; }
///
/// Update the camera's state.
/// The implementation must guarantee against multiple calls per frame, and should
/// use CinemachineCore.UpdateVirtualCamera(ICinemachineCamera, Vector3, float), which
/// has protection against multiple calls per frame.
///
/// Default world Up, set by the CinemachineBrain
/// Delta time for time-based effects (ignore if less than 0)
public void UpdateCameraState(Vector3 worldUp, float deltaTime)
{
CinemachineCore.Instance.UpdateVirtualCamera(this, worldUp, deltaTime);
}
/// Internal use only. Do not call this method.
/// Called by CinemachineCore at designated update time
/// so the vcam can position itself and track its targets.
/// Do not call this method. Let the framework do it at the appropriate time
/// Default world Up, set by the CinemachineBrain
/// Delta time for time-based effects (ignore if less than 0)
public abstract void InternalUpdateCameraState(Vector3 worldUp, float deltaTime);
/// Collection of parameters that influence how this virtual camera transitions from
/// other virtual cameras
[Serializable]
public struct TransitionParams
{
/// Hint for blending positions to and from this virtual camera
[Tooltip("Hint for blending positions to and from this virtual camera")]
[FormerlySerializedAs("m_PositionBlending")]
public BlendHint m_BlendHint;
/// When this virtual camera goes Live, attempt to force the position to be the same as the current position of the Unity Camera
[Tooltip("When this virtual camera goes Live, attempt to force the position to be the same as the current position of the Unity Camera")]
public bool m_InheritPosition;
/// This event fires when the virtual camera goes Live
[Tooltip("This event fires when the virtual camera goes Live")]
public CinemachineBrain.VcamActivatedEvent m_OnCameraLive;
}
/// Notification that this virtual camera is going live.
/// Base class implementation must be called by any overridden method.
/// The camera being deactivated. May be null.
/// Default world Up, set by the CinemachineBrain
/// Delta time for time-based effects (ignore if less than or equal to 0)
public virtual void OnTransitionFromCamera(
ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime)
{
if (!gameObject.activeInHierarchy)
PreviousStateIsValid = false;
}
/// Maintains the global vcam registry. Always call the base class implementation.
protected virtual void OnDestroy()
{
CinemachineCore.Instance.CameraDestroyed(this);
}
/// Base class implementation makes sure the priority queue remains up-to-date.
protected virtual void OnTransformParentChanged()
{
CinemachineCore.Instance.CameraDisabled(this);
CinemachineCore.Instance.CameraEnabled(this);
UpdateSlaveStatus();
UpdateVcamPoolStatus();
}
bool m_WasStarted;
/// Derived classes should call base class implementation.
protected virtual void Start()
{
m_WasStarted = true;
}
///
/// Returns true, when the vcam has an extension that requires user input.
///
internal virtual bool RequiresUserInput()
{
return mExtensions != null && mExtensions.Any(extension => extension != null && extension.RequiresUserInput);
}
///
/// Called on inactive object when being artificially activated by timeline.
/// This is necessary because Awake() isn't called on inactive gameObjects.
///
internal void EnsureStarted()
{
if (!m_WasStarted)
{
m_WasStarted = true;
var extensions = GetComponentsInChildren();
for (int i = 0; i < extensions.Length; ++i)
extensions[i].EnsureStarted();
}
}
#if UNITY_EDITOR
[UnityEditor.Callbacks.DidReloadScripts]
static void OnScriptReload()
{
var vcams = Resources.FindObjectsOfTypeAll(
typeof(CinemachineVirtualCameraBase)) as CinemachineVirtualCameraBase[];
foreach (var vcam in vcams)
vcam.LookAtTargetChanged = vcam.FollowTargetChanged = true;
}
#endif
///
/// Locate the first component that implements AxisState.IInputAxisProvider.
///
/// The first AxisState.IInputAxisProvider or null if none
public AxisState.IInputAxisProvider GetInputAxisProvider()
{
var components = GetComponentsInChildren();
for (int i = 0; i < components.Length; ++i)
{
var provider = components[i] as AxisState.IInputAxisProvider;
if (provider != null)
return provider;
}
return null;
}
/// Enforce bounds for fields, when changed in inspector.
/// Call base class implementation at the beginning of overridden method.
/// After base method is called, ValidatingStreamVersion will be valid.
protected virtual void OnValidate()
{
m_OnValidateCalled = true;
ValidatingStreamVersion = m_StreamingVersion;
m_StreamingVersion = CinemachineCore.kStreamingVersion;
}
/// Base class implementation adds the virtual camera from the priority queue.
protected virtual void OnEnable()
{
UpdateSlaveStatus();
UpdateVcamPoolStatus(); // Add to queue
if (!CinemachineCore.Instance.IsLive(this))
PreviousStateIsValid = false;
CinemachineCore.Instance.CameraEnabled(this);
InvalidateCachedTargets();
// Sanity check - if another vcam component is enabled, shut down
var vcamComponents = GetComponents();
for (int i = 0; i < vcamComponents.Length; ++i)
{
if (vcamComponents[i].enabled && vcamComponents[i] != this)
{
Debug.LogError(Name
+ " has multiple CinemachineVirtualCameraBase-derived components. Disabling "
+ GetType().Name + ".");
enabled = false;
}
}
}
/// Base class implementation makes sure the priority queue remains up-to-date.
protected virtual void OnDisable()
{
UpdateVcamPoolStatus(); // Remove from queue
CinemachineCore.Instance.CameraDisabled(this);
}
/// Base class implementation makes sure the priority queue remains up-to-date.
protected virtual void Update()
{
if (m_Priority != m_QueuePriority)
{
UpdateVcamPoolStatus(); // Force a re-sort
}
}
private bool mSlaveStatusUpdated = false;
private CinemachineVirtualCameraBase m_parentVcam = null;
private void UpdateSlaveStatus()
{
mSlaveStatusUpdated = true;
m_parentVcam = null;
Transform p = transform.parent;
if (p != null)
{
#if UNITY_2019_2_OR_NEWER
p.TryGetComponent(out m_parentVcam);
#else
m_parentVcam = p.GetComponent();
#endif
}
}
/// Returns this vcam's LookAt target, or if that is null, will retrun
/// the parent vcam's LookAt target.
/// This vcam's LookAt value.
/// The same value, or the parent's if null and a parent exists.
public Transform ResolveLookAt(Transform localLookAt)
{
Transform lookAt = localLookAt;
if (lookAt == null && ParentCamera != null)
lookAt = ParentCamera.LookAt; // Parent provides default
return lookAt;
}
/// Returns this vcam's Follow target, or if that is null, will retrun
/// the parent vcam's Follow target.
/// This vcam's Follow value.
/// The same value, or the parent's if null and a parent exists.
public Transform ResolveFollow(Transform localFollow)
{
Transform follow = localFollow;
if (follow == null && ParentCamera != null)
follow = ParentCamera.Follow; // Parent provides default
return follow;
}
private int m_QueuePriority = int.MaxValue;
private void UpdateVcamPoolStatus()
{
CinemachineCore.Instance.RemoveActiveCamera(this);
if (m_parentVcam == null && isActiveAndEnabled)
CinemachineCore.Instance.AddActiveCamera(this);
m_QueuePriority = m_Priority;
}
/// When multiple virtual cameras have the highest priority, there is
/// sometimes the need to push one to the top, making it the current Live camera if
/// it shares the highest priority in the queue with its peers.
///
/// This happens automatically when a
/// new vcam is enabled: the most recent one goes to the top of the priority subqueue.
/// Use this method to push a vcam to the top of its priority peers.
/// If it and its peers share the highest priority, then this vcam will become Live.
public void MoveToTopOfPrioritySubqueue()
{
UpdateVcamPoolStatus(); // Force a re-sort
}
/// This is called to notify the component that a target got warped,
/// so that the component can update its internal state to make the camera
/// also warp seamlessy.
/// The object that was warped
/// The amount the target's position changed
public virtual void OnTargetObjectWarped(Transform target, Vector3 positionDelta)
{
// inform the extensions
if (mExtensions != null)
{
for (int i = 0; i < mExtensions.Count; ++i)
mExtensions[i].OnTargetObjectWarped(target, positionDelta);
}
}
///
/// Force the virtual camera to assume a given position and orientation
///
/// Worldspace pposition to take
/// Worldspace orientation to take
public virtual void ForceCameraPosition(Vector3 pos, Quaternion rot)
{
// inform the extensions
if (mExtensions != null)
{
for (int i = 0; i < mExtensions.Count; ++i)
mExtensions[i].ForceCameraPosition(pos, rot);
}
}
// Doesn't really belong here but putting it here to avoid changing
// the API (belongs in the caller of CreateBlend). GML todo: Fix in next version.
float m_blendStartPosition;
// This is a total hack, because of missing API call. GML todo: Fix in next version.
bool GetInheritPosition(ICinemachineCamera cam)
{
if (cam is CinemachineVirtualCamera)
return (cam as CinemachineVirtualCamera).m_Transitions.m_InheritPosition;
if (cam is CinemachineFreeLook)
return (cam as CinemachineFreeLook).m_Transitions.m_InheritPosition;
return false;
}
/// Create a blend between 2 virtual cameras, taking into account
/// any existing active blend, with special case handling if the new blend is
/// effectively an undo of the current blend
/// Outgoing virtual camera
/// Incoming virtual camera
/// Definition of the blend to create
/// The current active blend
/// The new blend
protected CinemachineBlend CreateBlend(
ICinemachineCamera camA, ICinemachineCamera camB,
CinemachineBlendDefinition blendDef,
CinemachineBlend activeBlend)
{
if (blendDef.BlendCurve == null || blendDef.BlendTime <= 0 || (camA == null && camB == null))
{
m_blendStartPosition = 0;
return null;
}
if (activeBlend != null)
{
// Special case: if backing out of a blend-in-progress
// with the same blend in reverse, adjust the blend time
// to cancel out the progress made in the opposite direction
if (activeBlend != null && !activeBlend.IsComplete && activeBlend.CamA == camB && activeBlend.CamB == camA)
{
// How far have we blended? That is what we must undo
var progress = m_blendStartPosition
+ (1 - m_blendStartPosition) * activeBlend.TimeInBlend / activeBlend.Duration;
blendDef.m_Time *= progress;
m_blendStartPosition = 1 - progress;
}
else
m_blendStartPosition = 0;
if (GetInheritPosition(camB))
camA = null; // otherwise we get a pop when camB is moved
else
camA = new BlendSourceVirtualCamera(activeBlend);
}
if (camA == null)
camA = new StaticPointVirtualCamera(State, "(none)");
return new CinemachineBlend(
camA, camB, blendDef.BlendCurve, blendDef.BlendTime, 0);
}
///
/// Create a camera state based on the current transform of this vcam
///
/// Current World Up direction, as provided by the brain
/// Lens settings to serve as base, will be combined with lens from brain, if any
///
protected CameraState PullStateFromVirtualCamera(Vector3 worldUp, ref LensSettings lens)
{
CameraState state = CameraState.Default;
state.RawPosition = TargetPositionCache.GetTargetPosition(transform);
state.RawOrientation = TargetPositionCache.GetTargetRotation(transform);
state.ReferenceUp = worldUp;
CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(this);
if (brain != null)
lens.SnapshotCameraReadOnlyProperties(brain.OutputCamera);
state.Lens = lens;
return state;
}
private Transform m_CachedFollowTarget;
private CinemachineVirtualCameraBase m_CachedFollowTargetVcam;
private ICinemachineTargetGroup m_CachedFollowTargetGroup;
private Transform m_CachedLookAtTarget;
private CinemachineVirtualCameraBase m_CachedLookAtTargetVcam;
private ICinemachineTargetGroup m_CachedLookAtTargetGroup;
private void InvalidateCachedTargets()
{
m_CachedFollowTarget = null;
m_CachedFollowTargetVcam = null;
m_CachedFollowTargetGroup = null;
m_CachedLookAtTarget = null;
m_CachedLookAtTargetVcam = null;
m_CachedLookAtTargetGroup = null;
}
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoad]
class OnDomainReload
{
static OnDomainReload()
{
#if UNITY_2023_1_OR_NEWER
var vcams = FindObjectsByType
(FindObjectsInactive.Include, FindObjectsSortMode.None);
#elif UNITY_2020_1_OR_NEWER
var vcams = FindObjectsOfType(true);
#else
var vcams = FindObjectsOfType();
#endif
foreach (var vcam in vcams)
vcam.InvalidateCachedTargets();
}
}
#endif
///
/// This property is true if the Follow target was changed this frame.
///
public bool FollowTargetChanged { get; private set; }
///
/// This property is true if the LookAttarget was changed this frame.
///
public bool LookAtTargetChanged { get; private set; }
///
/// Call this from InternalUpdateCameraState() to check for changed
/// targets and update the target cache. This is needed for tracking
/// when a target object changes.
///
protected void UpdateTargetCache()
{
var target = ResolveFollow(Follow);
FollowTargetChanged = target != m_CachedFollowTarget;
if (FollowTargetChanged)
{
m_CachedFollowTarget = target;
m_CachedFollowTargetVcam = null;
m_CachedFollowTargetGroup = null;
if (m_CachedFollowTarget != null)
{
target.TryGetComponent(out m_CachedFollowTargetVcam);
target.TryGetComponent(out m_CachedFollowTargetGroup);
}
}
target = ResolveLookAt(LookAt);
LookAtTargetChanged = target != m_CachedLookAtTarget;
if (LookAtTargetChanged)
{
m_CachedLookAtTarget = target;
m_CachedLookAtTargetVcam = null;
m_CachedLookAtTargetGroup = null;
if (target != null)
{
target.TryGetComponent(out m_CachedLookAtTargetVcam);
target.TryGetComponent(out m_CachedLookAtTargetGroup);
}
}
}
/// Get Follow target as ICinemachineTargetGroup,
/// or null if target is not a ICinemachineTargetGroup
public ICinemachineTargetGroup AbstractFollowTargetGroup => m_CachedFollowTargetGroup;
/// Get Follow target as CinemachineVirtualCameraBase,
/// or null if target is not a CinemachineVirtualCameraBase
public CinemachineVirtualCameraBase FollowTargetAsVcam => m_CachedFollowTargetVcam;
/// Get LookAt target as ICinemachineTargetGroup,
/// or null if target is not a ICinemachineTargetGroup
public ICinemachineTargetGroup AbstractLookAtTargetGroup => m_CachedLookAtTargetGroup;
/// Get LookAt target as CinemachineVirtualCameraBase,
/// or null if target is not a CinemachineVirtualCameraBase
public CinemachineVirtualCameraBase LookAtTargetAsVcam => m_CachedLookAtTargetVcam;
/// Pre-Serialization handler - delegates to derived classes
void ISerializationCallbackReceiver.OnBeforeSerialize() => OnBeforeSerialize();
/// Post-Serialization handler - performs legacy upgrade
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (m_StreamingVersion < CinemachineCore.kStreamingVersion)
LegacyUpgrade(m_StreamingVersion);
m_StreamingVersion = CinemachineCore.kStreamingVersion;
}
///
/// Override this to handle any upgrades necessitated by a streaming version change
///
/// The version that was streamed
internal protected virtual void LegacyUpgrade(int streamedVersion) {}
internal virtual void OnBeforeSerialize() {}
}
}