70 lines
2.3 KiB
C#
70 lines
2.3 KiB
C#
using UnityEngine;
|
|
|
|
namespace EnglewoodLAB
|
|
{
|
|
/// <summary>
|
|
/// 카메라 이동 경로. 자식 Transform 순서대로 카메라가 보간 이동.
|
|
/// 각 자식에 CameraRoutePoint를 추가하면 궤도 파라미터(elevation, azimuth, distance)도 제어 가능.
|
|
/// </summary>
|
|
public class CameraRoute : MonoBehaviour
|
|
{
|
|
[SerializeField] private int gizmoResolution = 20;
|
|
[SerializeField] private Color gizmoColor = Color.cyan;
|
|
|
|
public MovePoint[] movePoints;
|
|
|
|
void Awake()
|
|
{
|
|
movePoints = GetComponentsInChildren<MovePoint>();
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnDrawGizmos()
|
|
{
|
|
var points = Application.isPlaying
|
|
? movePoints
|
|
: GetComponentsInChildren<MovePoint>();
|
|
|
|
if (points == null || points.Length < 2) return;
|
|
|
|
Gizmos.color = gizmoColor;
|
|
|
|
for (int i = 1; i < points.Length; i++)
|
|
{
|
|
Vector3 p0 = (i >= 2) ? points[i - 2].transform.position : points[i - 1].transform.position;
|
|
Vector3 p1 = points[i - 1].transform.position;
|
|
Vector3 p2 = points[i].transform.position;
|
|
Vector3 p3 = (i + 1 < points.Length) ? points[i + 1].transform.position : points[i].transform.position;
|
|
|
|
Vector3 prev = p1;
|
|
for (int s = 1; s <= gizmoResolution; s++)
|
|
{
|
|
float t = s / (float)gizmoResolution;
|
|
Vector3 curr = CatmullRom(p0, p1, p2, p3, t);
|
|
Gizmos.DrawLine(prev, curr);
|
|
prev = curr;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < points.Length; i++)
|
|
{
|
|
Gizmos.color = (i == 0) ? Color.green : gizmoColor;
|
|
Gizmos.DrawWireSphere(points[i].transform.position, 0.3f);
|
|
}
|
|
}
|
|
|
|
private static Vector3 CatmullRom(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
|
|
{
|
|
float t2 = t * t;
|
|
float t3 = t2 * t;
|
|
return 0.5f * (
|
|
2f * p1 +
|
|
(-p0 + p2) * t +
|
|
(2f * p0 - 5f * p1 + 4f * p2 - p3) * t2 +
|
|
(-p0 + 3f * p1 - 3f * p2 + p3) * t3
|
|
);
|
|
}
|
|
#endif
|
|
}
|
|
}
|