Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- namespace Victis.CameraSystem
- {
- [RequireComponent (typeof(Camera))]
- public class PerspectiveSwitcher : MonoBehaviour
- {
- static PerspectiveSwitcher _singleton;
- public static PerspectiveSwitcher Singleton { get{ return _singleton; } }
- Matrix4x4 _perspective;
- Matrix4x4 _targetProjection;
- [SerializeField]
- float _fov = 60f;
- [SerializeField]
- float _near = 0.01f;
- [SerializeField]
- float _far = 1000f;
- [SerializeField]
- float _orthographicSize = 4f;
- public float OrthographicSize { get{ return _orthographicSize; } set{ _orthographicSize = value; } }
- [SerializeField]
- float _orthoNear = -110;
- float _aspect;
- bool _orthoOn;
- public bool OrthoOn { get{ return _orthoOn; } }
- bool _orthoSwitchFinished;
- public bool OrthoSwitchFinished { get{ return _orthoSwitchFinished; } }
- [SerializeField]
- float _transitionToPerspectiveSpeed = 1.0f;
- [SerializeField]
- float _transitionToOrthographicSpeed = 1.0f;
- float _transitionSpeed = 1.0f;
- public float TransitionSpeed { get{return _transitionSpeed;} }
- [SerializeField]
- Camera _orthoCam;
- [SerializeField]
- bool _startInOrtho;
- [SerializeField]
- float _cameraTiltWhenInIso = 45f;
- CamRotLeftRight _camX;
- CamRotUpDown _camY;
- CameraCollider _camCollider;
- CamMovement _camMove;
- float _camYLastY = 0.0f;
- public Camera CameraUsed { get{ return _orthoOn ? _orthoCam : camera; } }
- const int CAM_DEPTH_ABOVE = -10;
- const int CAM_DEPTH_BELOW = -20;
- void ShowOrthoCam()
- {
- //_orthoCam.depth = CAM_DEPTH_ABOVE;
- //camera.depth = CAM_DEPTH_BELOW;
- camera.orthographic = true;
- camera.nearClipPlane = _orthoNear;
- }
- void ShowPerspectiveCam()
- {
- //camera.depth = CAM_DEPTH_ABOVE;
- //_orthoCam.depth = CAM_DEPTH_BELOW;
- camera.orthographic = false;
- camera.nearClipPlane = _near;
- }
- //public bool _isZoomingInOrtho;
- void Awake()
- {
- if (_singleton != null)
- {
- Debug.LogError("Singleton instance already found for PerspectiveSwitcher");
- }
- _singleton = this;
- }
- void Start()
- {
- _aspect = (float) Screen.width / (float) Screen.height;
- _perspective = Matrix4x4.Perspective(_fov, _aspect, _near, _far);
- _targetProjection = _perspective;
- camera.projectionMatrix = _perspective;
- _orthoOn = false;
- _camX = transform.root.GetComponentInChildren<CamRotLeftRight>();
- _camY = transform.root.GetComponentInChildren<CamRotUpDown>();
- _camCollider = transform.root.GetComponentInChildren<CameraCollider>();
- _camMove = transform.root.GetComponentInChildren<CamMovement>();
- if (_startInOrtho)
- {
- _orthoOn = true;
- _transitionSpeed = _transitionToOrthographicSpeed;
- _camY.SkipTo(_cameraTiltWhenInIso);
- _camYLastY = _camY.CurrentY;
- _camY.Locked = true;
- _camCollider.Enabled = false;
- _camX.SetToOrthoMode(true);
- _camMove.SetToOrthoMode(true);
- _targetProjection = Matrix4x4.Ortho(-_orthographicSize * _aspect, _orthographicSize * _aspect, -_orthographicSize, _orthographicSize, _orthoNear, _far);
- _orthoCam.orthographicSize = _orthographicSize;
- camera.projectionMatrix = _targetProjection;
- ShowOrthoCam();
- }
- }
- public void SwitchToOrthographic()
- {
- if (!_orthoOn)
- {
- TogglePerspective();
- }
- }
- public void SwitchToPerspective()
- {
- if (_orthoOn)
- {
- TogglePerspective();
- }
- }
- public void TogglePerspective()
- {
- _orthoOn = !_orthoOn;
- if (_orthoOn)
- {
- // to ortho
- _transitionSpeed = _transitionToOrthographicSpeed;
- _camY.MoveTo(_cameraTiltWhenInIso, _transitionSpeed); // put this in Update if you want real-time changing of tilt
- _camYLastY = _camY.CurrentY;
- _camY.Locked = true;
- _camCollider.Enabled = false;
- _camX.SetToOrthoMode(true);
- _camMove.SetToOrthoMode(true);
- }
- else
- {
- // to perspective
- _transitionSpeed = _transitionToPerspectiveSpeed;
- _targetProjection = _perspective;
- _camY.MoveTo(_camYLastY, _transitionSpeed);
- _camY.Locked = false;
- _camCollider.Enabled = true;
- _camX.SetToOrthoMode(false);
- _camMove.SetToOrthoMode(false);
- ShowPerspectiveCam();
- }
- }
- void Update()
- {
- if (Input.GetButtonDown("EagleEye"))
- {
- TogglePerspective();
- }
- if (_orthoOn)
- {
- _targetProjection = Matrix4x4.Ortho(-_orthographicSize * _aspect, _orthographicSize * _aspect, -_orthographicSize, _orthographicSize, _orthoNear, _far);
- _orthoCam.orthographicSize = _orthographicSize;
- _orthoSwitchFinished = MatrixApproximately(camera.projectionMatrix, _targetProjection, 0.001f);
- if (_orthoSwitchFinished)
- {
- ShowOrthoCam();
- }
- }
- else
- {
- _orthoSwitchFinished = false;
- // only do this in editor or web build since window can be resized there, thus, a changing aspect ratio
- _perspective = Matrix4x4.Perspective(_fov, _aspect, _near, _far);
- _targetProjection = _perspective;
- }
- _aspect = (float) Screen.width / (float) Screen.height;
- camera.projectionMatrix = MatrixLerp(camera.projectionMatrix, _targetProjection, Time.deltaTime * _transitionSpeed);
- }
- public static Matrix4x4 MatrixLerp(Matrix4x4 from, Matrix4x4 to, float time)
- {
- Matrix4x4 ret = new Matrix4x4();
- for (int i = 0; i < 16; i++)
- ret[i] = Mathf.Lerp(from[i], to[i], time);
- return ret;
- }
- public static bool MatrixApproximately(Matrix4x4 a, Matrix4x4 b, float range)
- {
- return
- Mathf.Abs(a[0] - b[0]) <= range &&
- Mathf.Abs(a[1] - b[1]) <= range &&
- Mathf.Abs(a[2] - b[2]) <= range &&
- Mathf.Abs(a[3] - b[3]) <= range &&
- Mathf.Abs(a[4] - b[4]) <= range &&
- Mathf.Abs(a[5] - b[5]) <= range &&
- Mathf.Abs(a[6] - b[6]) <= range &&
- Mathf.Abs(a[7] - b[7]) <= range &&
- Mathf.Abs(a[8] - b[8]) <= range &&
- Mathf.Abs(a[9] - b[9]) <= range &&
- Mathf.Abs(a[10] - b[10]) <= range &&
- Mathf.Abs(a[11] - b[11]) <= range &&
- Mathf.Abs(a[12] - b[12]) <= range &&
- Mathf.Abs(a[13] - b[13]) <= range &&
- Mathf.Abs(a[14] - b[14]) <= range &&
- Mathf.Abs(a[15] - b[15]) <= range;
- }
- }
- } // namespace Victis.CameraSystem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement