///
/// MIT License - Copyright(c) 2019 Ugo Belfiore
///
using UnityEngine;
using System;
namespace PerspectiveSwitcher
{
///
/// this class containt the datas of the projection matrix
///
[Serializable]
public class MatrixBlendData
{
public enum MatrixType
{
PERSPECTIVE = 10,
ORTHOGRAPHIC = 20
}
public MatrixType MatrixCurrentType = MatrixType.PERSPECTIVE;
public float NearClippingPlane = .3f;
public float FarClippingPlane = 1000f;
public float FieldOfView = 60f;
public float OrthographicSize = 5f;
private Matrix4x4 _currentCalculatedMatrix;
public MatrixBlendData(MatrixBlendData.MatrixType matrixType)
{
MatrixCurrentType = matrixType;
Init();
}
///
/// construct the current matrix based on the class settings
///
public void Init()
{
float aspectRatio = 16f / 9f;
switch (MatrixCurrentType)
{
case MatrixType.ORTHOGRAPHIC:
_currentCalculatedMatrix = ExtMatrix.GetMatrixOrthographic(aspectRatio, OrthographicSize, NearClippingPlane, FarClippingPlane);
break;
case MatrixType.PERSPECTIVE:
_currentCalculatedMatrix = ExtMatrix.GetMatrixPerspective(aspectRatio, FieldOfView, NearClippingPlane, FarClippingPlane);
break;
}
}
///
/// reset the projection matrix of the camera
///
/// main camera reference
public void ResetCameraToMatrix(Camera camRef)
{
switch (MatrixCurrentType)
{
case MatrixType.ORTHOGRAPHIC:
camRef.orthographic = true;
camRef.orthographicSize = OrthographicSize;
camRef.ResetProjectionMatrix();
break;
case MatrixType.PERSPECTIVE:
camRef.orthographic = false;
camRef.fieldOfView = FieldOfView;
camRef.ResetProjectionMatrix();
break;
}
}
///
/// get the final matrix corresponding to all the setting.
/// You NEED to call Init() BEFORE calling this function
///
///
public Matrix4x4 GetSetuppedMatrix()
{
return (_currentCalculatedMatrix);
}
///
/// From a previous and a next matrice, get a lerped one by an evaluated time
///
/// time of the lerp
/// start lerp matrix
/// end lerp matrix
/// curve if we want to add an ease to the lerp
/// return the current lerped matrix
public static Matrix4x4 DoMatrixTransition(float timeEvaluated, Matrix4x4 prevMatrix, Matrix4x4 nextMatrix)
{
return (ExtMatrix.MatrixLerp(prevMatrix, nextMatrix, timeEvaluated));
}
}
}