1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
using UnityEngine;
using System;
using Cinemachine.Utility;
namespace Cinemachine
{
/// <summary>Defines a world-space path, consisting of an array of waypoints,
/// each of which has position, tangent, and roll settings. Bezier interpolation
/// is performed between the waypoints, to get a smooth and continuous path.</summary>
[DocumentationSorting(18, DocumentationSortingAttribute.Level.UserRef)]
[AddComponentMenu("Cinemachine/CinemachinePath")]
[SaveDuringPlay]
public class CinemachinePath : CinemachinePathBase
{
/// <summary>A waypoint along the path</summary>
[DocumentationSorting(18.2f, DocumentationSortingAttribute.Level.UserRef)]
[Serializable] public struct Waypoint
{
/// <summary>Position in path-local space</summary>
[Tooltip("Position in path-local space")]
public Vector3 position;
/// <summary>Offset from the position, which defines the tangent of the curve at the waypoint.
/// The length of the tangent encodes the strength of the bezier handle.
/// The same handle is used symmetrically on both sides of the waypoint, to ensure smoothness.</summary>
[Tooltip("Offset from the position, which defines the tangent of the curve at the waypoint. The length of the tangent encodes the strength of the bezier handle. The same handle is used symmetrically on both sides of the waypoint, to ensure smoothness.")]
public Vector3 tangent;
/// <summary>Defines the roll of the path at this waypoint.
/// The other orientation axes are inferred from the tangent and world up.</summary>
[Tooltip("Defines the roll of the path at this waypoint. The other orientation axes are inferred from the tangent and world up.")]
public float roll;
}
/// <summary>If checked, then the path ends are joined to form a continuous loop</summary>
[Tooltip("If checked, then the path ends are joined to form a continuous loop.")]
public bool m_Looped;
/// <summary>The waypoints that define the path.
/// They will be interpolated using a bezier curve</summary>
[Tooltip("The waypoints that define the path. They will be interpolated using a bezier curve.")]
public Waypoint[] m_Waypoints = new Waypoint[0];
/// <summary>The minimum value for the path position</summary>
public override float MinPos { get { return 0; } }
/// <summary>The maximum value for the path position</summary>
public override float MaxPos
{
get
{
int count = m_Waypoints.Length - 1;
if (count < 1)
return 0;
return m_Looped ? count + 1 : count;
}
}
/// <summary>True if the path ends are joined to form a continuous loop</summary>
public override bool Looped { get { return m_Looped; } }
/// <summary>When calculating the distance cache, sample the path this many
/// times between points</summary>
public override int DistanceCacheSampleStepsPerSegment { get { return m_Resolution; } }
/// <summary>Returns normalized position</summary>
float GetBoundingIndices(float pos, out int indexA, out int indexB)
{
pos = NormalizePos(pos);
int rounded = Mathf.RoundToInt(pos);
if (Mathf.Abs(pos - rounded) < UnityVectorExtensions.Epsilon)
indexA = indexB = (rounded == m_Waypoints.Length) ? 0 : rounded;
else
{
indexA = Mathf.FloorToInt(pos);
if (indexA >= m_Waypoints.Length)
{
pos -= MaxPos;
indexA = 0;
}
indexB = Mathf.CeilToInt(pos);
if (indexB >= m_Waypoints.Length)
indexB = 0;
}
return pos;
}
/// <summary>Get a worldspace position of a point along the path</summary>
/// <param name="pos">Postion along the path. Need not be normalized.</param>
/// <returns>World-space position of the point along at path at pos</returns>
public override Vector3 EvaluatePosition(float pos)
{
Vector3 result = new Vector3();
if (m_Waypoints.Length == 0)
result = transform.position;
else
{
int indexA, indexB;
pos = GetBoundingIndices(pos, out indexA, out indexB);
if (indexA == indexB)
result = m_Waypoints[indexA].position;
else
{
// interpolate
Waypoint wpA = m_Waypoints[indexA];
Waypoint wpB = m_Waypoints[indexB];
result = SplineHelpers.Bezier3(pos - indexA,
m_Waypoints[indexA].position, wpA.position + wpA.tangent,
wpB.position - wpB.tangent, wpB.position);
}
}
return transform.TransformPoint(result);
}
/// <summary>Get the tangent of the curve at a point along the path.</summary>
/// <param name="pos">Postion along the path. Need not be normalized.</param>
/// <returns>World-space direction of the path tangent.
/// Length of the vector represents the tangent strength</returns>
public override Vector3 EvaluateTangent(float pos)
{
Vector3 result = new Vector3();
if (m_Waypoints.Length == 0)
result = transform.rotation * Vector3.forward;
else
{
int indexA, indexB;
pos = GetBoundingIndices(pos, out indexA, out indexB);
if (indexA == indexB)
result = m_Waypoints[indexA].tangent;
else
{
Waypoint wpA = m_Waypoints[indexA];
Waypoint wpB = m_Waypoints[indexB];
result = SplineHelpers.BezierTangent3(pos - indexA,
m_Waypoints[indexA].position, wpA.position + wpA.tangent,
wpB.position - wpB.tangent, wpB.position);
}
}
return transform.TransformDirection(result);
}
/// <summary>Get the orientation the curve at a point along the path.</summary>
/// <param name="pos">Postion along the path. Need not be normalized.</param>
/// <returns>World-space orientation of the path, as defined by tangent, up, and roll.</returns>
public override Quaternion EvaluateOrientation(float pos)
{
Quaternion result = transform.rotation;
if (m_Waypoints.Length > 0)
{
float roll = 0;
int indexA, indexB;
pos = GetBoundingIndices(pos, out indexA, out indexB);
if (indexA == indexB)
roll = m_Waypoints[indexA].roll;
else
{
float rollA = m_Waypoints[indexA].roll;
float rollB = m_Waypoints[indexB].roll;
if (indexB == 0)
{
// Special handling at the wraparound - cancel the spins
rollA = rollA % 360;
rollB = rollB % 360;
}
roll = Mathf.Lerp(rollA, rollB, pos - indexA);
}
Vector3 fwd = EvaluateTangent(pos);
if (!fwd.AlmostZero())
{
Vector3 up = transform.rotation * Vector3.up;
Quaternion q = Quaternion.LookRotation(fwd, up);
result = q * Quaternion.AngleAxis(roll, Vector3.forward);
}
}
return result;
}
private void OnValidate() { InvalidateDistanceCache(); }
}
}
|