Space-Smash-Out/Assets/Plugins/PrimeTween/Demo/Scripts/CameraProjectionMatrixAnimation.cs
Jakob Feldmann 64162cb4a1 feat: whole project restructuring
This can be seen as the initial state of the project after the released demo.

The changes include:
- New ship models
- Singleton manager structure to keep project scaleable in the future
     - Managing players, their settings, character choices, statistics, match setups, controls etc. in a separate decoupled scene
- Main menu with transitions to the arena scene
- Beginnings of a custom audio solution
- Logging with Log4Net

It is really a complete overhaul of the projects structure and management.
2024-04-01 23:06:39 +02:00

40 lines
1.7 KiB
C#

#if PRIME_TWEEN_INSTALLED
using PrimeTween;
using UnityEngine;
namespace PrimeTweenDemo {
public class CameraProjectionMatrixAnimation : MonoBehaviour {
[SerializeField] Camera mainCamera;
float interpolationFactor;
bool isOrthographic;
Tween tween;
public void AnimateCameraProjection() {
isOrthographic = !isOrthographic;
tween.Stop();
tween = Tween.Custom(this, interpolationFactor, isOrthographic ? 1 : 0, 0.6f, ease: Ease.InOutSine, onValueChange: (target, t) => {
target.InterpolateProjectionMatrix(t);
})
.OnComplete(this, target => {
target.mainCamera.orthographic = target.isOrthographic;
target.mainCamera.ResetProjectionMatrix();
});
}
void InterpolateProjectionMatrix(float _interpolationFactor) {
interpolationFactor = _interpolationFactor;
float aspect = (float)Screen.width / Screen.height;
var orthographicSize = mainCamera.orthographicSize;
var perspectiveMatrix = Matrix4x4.Perspective(mainCamera.fieldOfView, aspect, mainCamera.nearClipPlane, mainCamera.farClipPlane);
var orthoMatrix = Matrix4x4.Ortho(-orthographicSize * aspect, orthographicSize * aspect, -orthographicSize, orthographicSize, mainCamera.nearClipPlane, mainCamera.farClipPlane);
Matrix4x4 projectionMatrix = default;
for (int i = 0; i < 16; i++) {
projectionMatrix[i] = Mathf.Lerp(perspectiveMatrix[i], orthoMatrix[i], _interpolationFactor);
}
mainCamera.projectionMatrix = projectionMatrix;
}
public bool IsAnimating => tween.isAlive;
}
}
#endif