parent
2a5e4e29d2
commit
3129ba0210
@ -1,28 +1,130 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using LogicLog;
|
using LogicLog;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
namespace CameraRoll.Client.Tool {
|
namespace CameraRoll.Client.Tool {
|
||||||
|
|
||||||
public class CameraRollTool {
|
public class CameraRollTool {
|
||||||
private static ILogicLogger _logger = null!;
|
private static ILogicLogger _logger = null!;
|
||||||
|
private static Camera _cam;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// How fast does it interpolate between points
|
||||||
|
/// TODO: Make this editable per point
|
||||||
|
/// </summary>
|
||||||
|
private const float InterpolationSpeed = 0.005f;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If the track is playing/animating/moving the camera
|
||||||
|
/// </summary>
|
||||||
|
private static bool _playing;
|
||||||
|
|
||||||
|
// Backup of the original (local) position and rotation of the camera
|
||||||
|
private static Vector3 _originalPosition;
|
||||||
|
private static Quaternion _originalRotation;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Which point the camera is currently heading towards
|
||||||
|
/// </summary>
|
||||||
|
private static int _pointIndex;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Progress of the linear interpolation (0.0f - 1.0f)
|
||||||
|
/// </summary>
|
||||||
|
private static float _interpolationState;
|
||||||
|
|
||||||
|
private static readonly List<GameObject> PathPoints = new List<GameObject>();
|
||||||
|
|
||||||
public static void Init(ILogicLogger logger) {
|
public static void Init(ILogicLogger logger) {
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
PathPoints.Clear();
|
||||||
|
_playing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void OnEnter() {
|
||||||
|
_cam = Camera.main;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void PlacePoint() {
|
public static void PlacePoint() {
|
||||||
_logger.Info("PlacePoint");
|
_logger.Info("PlacePoint");
|
||||||
|
GameObject point = new GameObject($"PathPoint #{PathPoints.Count}") {
|
||||||
|
transform = {
|
||||||
|
position = _cam.transform.position,
|
||||||
|
rotation = _cam.transform.rotation
|
||||||
|
}
|
||||||
|
};
|
||||||
|
point.AddComponent<PathPoint>();
|
||||||
|
PathPoints.Add(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Play() {
|
public static void Play() {
|
||||||
_logger.Info("Play");
|
if (_playing) return;
|
||||||
|
|
||||||
|
_originalPosition = _cam.transform.localPosition;
|
||||||
|
_originalRotation = _cam.transform.localRotation;
|
||||||
|
|
||||||
|
_pointIndex = 0;
|
||||||
|
_interpolationState = 0.0f;
|
||||||
|
|
||||||
|
_playing = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Stop() {
|
public static void Stop() {
|
||||||
_logger.Info("Stop");
|
if (!_playing) return;
|
||||||
|
_playing = false;
|
||||||
|
|
||||||
|
_cam.transform.localPosition = _originalPosition;
|
||||||
|
_cam.transform.localRotation = _originalRotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Clear() {
|
public static void Clear() {
|
||||||
_logger.Info("Clear");
|
foreach (GameObject point in PathPoints) {
|
||||||
|
Object.Destroy(point);
|
||||||
|
}
|
||||||
|
|
||||||
|
PathPoints.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Update() {
|
||||||
|
if (_playing) {
|
||||||
|
if (_pointIndex >= PathPoints.Count) {
|
||||||
|
// Reached the end
|
||||||
|
_logger.Info("Reached the end of track");
|
||||||
|
Stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_pointIndex == 0) {
|
||||||
|
// Snap to the first defined point
|
||||||
|
_cam.transform.position = PathPoints[0].transform.position;
|
||||||
|
_pointIndex++;
|
||||||
|
_interpolationState = 0.0f;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_interpolationState += InterpolationSpeed;
|
||||||
|
|
||||||
|
Vector3 prevPos = GetPos(_pointIndex - 1);
|
||||||
|
Vector3 posDiff = GetPos(_pointIndex) - prevPos;
|
||||||
|
_cam.transform.position = prevPos + posDiff * _interpolationState;
|
||||||
|
|
||||||
|
Vector3 prevRot = GetRot(_pointIndex - 1);
|
||||||
|
Vector3 rotDiff = GetRot(_pointIndex) - prevRot;
|
||||||
|
_cam.transform.eulerAngles = prevRot + rotDiff * _interpolationState;
|
||||||
|
// TODO: interpolate across the shorter direction of rotation
|
||||||
|
|
||||||
|
if (_interpolationState >= 1.0f) {
|
||||||
|
_pointIndex++;
|
||||||
|
_interpolationState = 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Vector3 GetPos(int index) => PathPoints[index].transform.position;
|
||||||
|
private static Vector3 GetRot(int index) => PathPoints[index].transform.eulerAngles;
|
||||||
|
|
||||||
|
public static void OnExit() {
|
||||||
|
Clear();
|
||||||
|
_playing = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace CameraRoll.Client.Tool {
|
||||||
|
public class PathPoint : MonoBehaviour {
|
||||||
|
private const float AxisLength = 0.1f;
|
||||||
|
|
||||||
|
void Start() {
|
||||||
|
CreateAxis(Vector3.right, Color.red);
|
||||||
|
CreateAxis(Vector3.up, Color.green);
|
||||||
|
CreateAxis(Vector3.forward, Color.blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CreateAxis(Vector3 dir, Color color) {
|
||||||
|
GameObject line = new GameObject("Axis");
|
||||||
|
line.transform.SetParent(transform, false);
|
||||||
|
LineRenderer lr = line.AddComponent<LineRenderer>();
|
||||||
|
lr.useWorldSpace = false;
|
||||||
|
lr.positionCount = 2;
|
||||||
|
lr.SetPosition(0, dir.normalized * -AxisLength);
|
||||||
|
lr.SetPosition(1, dir.normalized * AxisLength);
|
||||||
|
lr.material = new Material(Shader.Find("Unlit/Color"));
|
||||||
|
lr.material.color = color;
|
||||||
|
lr.startWidth = 0.01f;
|
||||||
|
lr.endWidth = 0.01f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue