using UnityEngine;
using System;
namespace Cinemachine
{
///
/// Describes the FOV and clip planes for a camera. This generally mirrors the Unity Camera's
/// lens settings, and will be used to drive the Unity camera when the vcam is active.
///
[Serializable]
[DocumentationSorting(2, DocumentationSortingAttribute.Level.UserRef)]
public struct LensSettings
{
/// Default Lens Settings
public static LensSettings Default = new LensSettings(40f, 10, 0.1f, 5000f, 0, false, 1);
///
/// This is the camera view in vertical degrees. For cinematic people, a 50mm lens
/// on a super-35mm sensor would equal a 19.6 degree FOV
///
[Range(1f, 179f)]
[Tooltip("This is the camera view in vertical degrees. For cinematic people, a 50mm lens on a super-35mm sensor would equal a 19.6 degree FOV")]
public float FieldOfView;
///
/// When using an orthographic camera, this defines the height, in world
/// co-ordinates, of the camera view.
///
[Tooltip("When using an orthographic camera, this defines the half-height, in world coordinates, of the camera view.")]
public float OrthographicSize;
///
/// The near clip plane for this LensSettings
///
[Tooltip("This defines the near region in the renderable range of the camera frustum. Raising this value will stop the game from drawing things near the camera, which can sometimes come in handy. Larger values will also increase your shadow resolution.")]
public float NearClipPlane;
///
/// The far clip plane for this LensSettings
///
[Tooltip("This defines the far region of the renderable range of the camera frustum. Typically you want to set this value as low as possible without cutting off desired distant objects")]
public float FarClipPlane;
///
/// The dutch (tilt) to be applied to the camera. In degrees
///
[Range(-180f, 180f)]
[Tooltip("Camera Z roll, or tilt, in degrees.")]
public float Dutch;
///
/// This is set every frame by the virtual camera, based on the value found in the
/// currently associated Unity camera
///
internal bool Orthographic { get; set; }
///
/// This is set every frame by the virtual camera, based on the value
/// found in the currently associated Unity camera
///
internal float Aspect { get; set; }
///
/// Creates a new LensSettings, copying the values from the
/// supplied Camera
///
/// The Camera from which the FoV, near
/// and far clip planes will be copied.
public static LensSettings FromCamera(Camera fromCamera)
{
LensSettings lens = Default;
if (fromCamera != null)
{
lens.FieldOfView = fromCamera.fieldOfView;
lens.OrthographicSize = fromCamera.orthographicSize;
lens.NearClipPlane = fromCamera.nearClipPlane;
lens.FarClipPlane = fromCamera.farClipPlane;
lens.Orthographic = fromCamera.orthographic;
lens.Aspect = fromCamera.aspect;
}
return lens;
}
///
/// Explicit constructor for this LensSettings
///
/// The Vertical field of view
/// If orthographic, this is the half-height of the screen
/// The near clip plane
/// The far clip plane
/// Camera roll, in degrees. This is applied at the end
/// Whether the lens is orthographic
/// The aspect ratio of the lens Width/height
/// after shot composition.
public LensSettings(
float fov, float orthographicSize,
float nearClip, float farClip, float dutch,
bool ortho, float aspect) : this()
{
FieldOfView = fov;
OrthographicSize = orthographicSize;
NearClipPlane = nearClip;
FarClipPlane = farClip;
Dutch = dutch;
Orthographic = ortho;
Aspect = aspect;
}
///
/// Linearly blends the fields of two LensSettings and returns the result
///
/// The LensSettings to blend from
/// The LensSettings to blend to
/// The interpolation value. Internally clamped to the range [0,1]
/// Interpolated settings
public static LensSettings Lerp(LensSettings lensA, LensSettings lensB, float t)
{
t = Mathf.Clamp01(t);
LensSettings blendedLens = new LensSettings();
blendedLens.FarClipPlane = Mathf.Lerp(lensA.FarClipPlane, lensB.FarClipPlane, t);
blendedLens.NearClipPlane = Mathf.Lerp(lensA.NearClipPlane, lensB.NearClipPlane, t);
blendedLens.FieldOfView = Mathf.Lerp(lensA.FieldOfView, lensB.FieldOfView, t);
blendedLens.OrthographicSize = Mathf.Lerp(lensA.OrthographicSize, lensB.OrthographicSize, t);
blendedLens.Dutch = Mathf.Lerp(lensA.Dutch, lensB.Dutch, t);
blendedLens.Aspect = Mathf.Lerp(lensA.Aspect, lensB.Aspect, t);
blendedLens.Orthographic = lensA.Orthographic && lensB.Orthographic;
return blendedLens;
}
/// Make sure lens settings are sane. Call this from OnValidate().
public void Validate()
{
NearClipPlane = Mathf.Max(NearClipPlane, 0.01f);
FarClipPlane = Mathf.Max(FarClipPlane, NearClipPlane + 0.01f);
}
}
}