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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
using Cinemachine.Utility;
using UnityEngine;
namespace Cinemachine
{
/// <summary>
/// This is a CinemachineComponent in the Body section of the component pipeline.
/// Its job is to position the camera in a fixed relationship to the vcam's Follow
/// target object, with offsets and damping.
///
/// The Tansposer will only change the camera's position in space. It will not
/// re-orient or otherwise aim the camera. To to that, you need to instruct
/// the vcam in the Aim section of its pipeline.
/// </summary>
[DocumentationSorting(5, DocumentationSortingAttribute.Level.UserRef)]
[AddComponentMenu("")] // Don't display in add component menu
[RequireComponent(typeof(CinemachinePipeline))]
[SaveDuringPlay]
public class CinemachineTransposer : CinemachineComponentBase
{
/// <summary>
/// The coordinate space to use when interpreting the offset from the target
/// </summary>
[DocumentationSorting(5.01f, DocumentationSortingAttribute.Level.UserRef)]
public enum BindingMode
{
/// <summary>
/// Camera will be bound to the Follow target using a frame of reference consisting
/// of the target's local frame at the moment when the virtual camera was enabled,
/// or when the target was assigned.
/// </summary>
LockToTargetOnAssign = 0,
/// <summary>
/// Camera will be bound to the Follow target using a frame of reference consisting
/// of the target's local frame, with the tilt and roll zeroed out.
/// </summary>
LockToTargetWithWorldUp = 1,
/// <summary>
/// Camera will be bound to the Follow target using a frame of reference consisting
/// of the target's local frame, with the roll zeroed out.
/// </summary>
LockToTargetNoRoll = 2,
/// <summary>
/// Camera will be bound to the Follow target using the target's local frame.
/// </summary>
LockToTarget = 3,
/// <summary>Camera will be bound to the Follow target using a world space offset.</summary>
WorldSpace = 4,
/// <summary>Offsets will be calculated relative to the target, using Camera-local axes</summary>
SimpleFollowWithWorldUp = 5
}
/// <summary>The coordinate space to use when interpreting the offset from the target</summary>
[Tooltip("The coordinate space to use when interpreting the offset from the target. This is also used to set the camera's Up vector, which will be maintained when aiming the camera.")]
public BindingMode m_BindingMode = BindingMode.LockToTargetWithWorldUp;
/// <summary>The distance which the transposer will attempt to maintain from the transposer subject</summary>
[Tooltip("The distance vector that the transposer will attempt to maintain from the Follow target")]
public Vector3 m_FollowOffset = Vector3.back * 10f;
/// <summary>How aggressively the camera tries to maintain the offset in the X-axis.
/// Small numbers are more responsive, rapidly translating the camera to keep the target's
/// x-axis offset. Larger numbers give a more heavy slowly responding camera.
/// Using different settings per axis can yield a wide range of camera behaviors</summary>
[Range(0f, 20f)]
[Tooltip("How aggressively the camera tries to maintain the offset in the X-axis. Small numbers are more responsive, rapidly translating the camera to keep the target's x-axis offset. Larger numbers give a more heavy slowly responding camera. Using different settings per axis can yield a wide range of camera behaviors.")]
public float m_XDamping = 1f;
/// <summary>How aggressively the camera tries to maintain the offset in the Y-axis.
/// Small numbers are more responsive, rapidly translating the camera to keep the target's
/// y-axis offset. Larger numbers give a more heavy slowly responding camera.
/// Using different settings per axis can yield a wide range of camera behaviors</summary>
[Range(0f, 20f)]
[Tooltip("How aggressively the camera tries to maintain the offset in the Y-axis. Small numbers are more responsive, rapidly translating the camera to keep the target's y-axis offset. Larger numbers give a more heavy slowly responding camera. Using different settings per axis can yield a wide range of camera behaviors.")]
public float m_YDamping = 1f;
/// <summary>How aggressively the camera tries to maintain the offset in the Z-axis.
/// Small numbers are more responsive, rapidly translating the camera to keep the
/// target's z-axis offset. Larger numbers give a more heavy slowly responding camera.
/// Using different settings per axis can yield a wide range of camera behaviors</summary>
[Range(0f, 20f)]
[Tooltip("How aggressively the camera tries to maintain the offset in the Z-axis. Small numbers are more responsive, rapidly translating the camera to keep the target's z-axis offset. Larger numbers give a more heavy slowly responding camera. Using different settings per axis can yield a wide range of camera behaviors.")]
public float m_ZDamping = 1f;
/// <summary>How aggressively the camera tries to track the target rotation's X angle.
/// Small numbers are more responsive. Larger numbers give a more heavy slowly responding camera.</summary>
[Range(0f, 20f)]
[Tooltip("How aggressively the camera tries to track the target rotation's X angle. Small numbers are more responsive. Larger numbers give a more heavy slowly responding camera.")]
public float m_PitchDamping = 0;
/// <summary>How aggressively the camera tries to track the target rotation's Y angle.
/// Small numbers are more responsive. Larger numbers give a more heavy slowly responding camera.</summary>
[Range(0f, 20f)]
[Tooltip("How aggressively the camera tries to track the target rotation's Y angle. Small numbers are more responsive. Larger numbers give a more heavy slowly responding camera.")]
public float m_YawDamping = 0;
/// <summary>How aggressively the camera tries to track the target rotation's Z angle.
/// Small numbers are more responsive. Larger numbers give a more heavy slowly responding camera.</summary>
[Range(0f, 20f)]
[Tooltip("How aggressively the camera tries to track the target rotation's Z angle. Small numbers are more responsive. Larger numbers give a more heavy slowly responding camera.")]
public float m_RollDamping = 0f;
protected virtual void OnValidate()
{
m_FollowOffset = EffectiveOffset;
}
/// <summary>Get the target offset, with sanitization</summary>
protected Vector3 EffectiveOffset
{
get
{
Vector3 offset = m_FollowOffset;
if (m_BindingMode == BindingMode.SimpleFollowWithWorldUp)
{
offset.x = 0;
offset.z = -Mathf.Abs(offset.z);
}
return offset;
}
}
/// <summary>True if component is enabled and has a valid Follow target</summary>
public override bool IsValid { get { return enabled && FollowTarget != null; } }
/// <summary>Get the Cinemachine Pipeline stage that this component implements.
/// Always returns the Body stage</summary>
public override CinemachineCore.Stage Stage { get { return CinemachineCore.Stage.Body; } }
/// <summary>Positions the virtual camera according to the transposer rules.</summary>
/// <param name="curState">The current camera state</param>
/// <param name="deltaTime">Used for damping. If less than 0, no damping is done.</param>
public override void MutateCameraState(ref CameraState curState, float deltaTime)
{
//UnityEngine.Profiling.Profiler.BeginSample("CinemachineTransposer.MutateCameraState");
InitPrevFrameStateInfo(ref curState, deltaTime);
if (IsValid)
{
Vector3 pos;
Quaternion orient;
Vector3 offset = EffectiveOffset;
TrackTarget(deltaTime, curState.ReferenceUp, offset, out pos, out orient);
curState.RawPosition = pos + orient * offset;
curState.ReferenceUp = orient * Vector3.up;
}
//UnityEngine.Profiling.Profiler.EndSample();
}
/// <summary>API for the editor, to process a position drag from the user.
/// This implementation adds the delta to the follow offset.</summary>
/// <param name="delta">The amount dragged this frame</param>
public override void OnPositionDragged(Vector3 delta)
{
Quaternion targetOrientation = GetReferenceOrientation(VcamState.ReferenceUp);
Vector3 localOffset = Quaternion.Inverse(targetOrientation) * delta;
m_FollowOffset += localOffset;
m_FollowOffset = EffectiveOffset;
}
/// <summary>Initializes the state for previous frame if appropriate.</summary>
protected void InitPrevFrameStateInfo(
ref CameraState curState, float deltaTime)
{
if (m_previousTarget != FollowTarget || deltaTime < 0)
{
m_previousTarget = FollowTarget;
m_targetOrientationOnAssign
= (m_previousTarget == null) ? Quaternion.identity : FollowTarget.rotation;
}
if (deltaTime < 0)
{
m_PreviousTargetPosition = curState.RawPosition;
m_PreviousReferenceOrientation = GetReferenceOrientation(curState.ReferenceUp);
}
}
/// <summary>Positions the virtual camera according to the transposer rules.</summary>
/// <param name="deltaTime">Used for damping. If less than 0, no damping is done.</param>
/// <param name="up">Current camera up</param>
/// <param name="desiredCameraOffset">Where we want to put the camera relative to the follow target</param>
/// <param name="outTargetPosition">Resulting camera position</param>
/// <param name="outTargetOrient">Damped target orientation</param>
protected void TrackTarget(
float deltaTime, Vector3 up, Vector3 desiredCameraOffset,
out Vector3 outTargetPosition, out Quaternion outTargetOrient)
{
Quaternion targetOrientation = GetReferenceOrientation(up);
Quaternion dampedOrientation = targetOrientation;
if (deltaTime >= 0)
{
Vector3 relative = (Quaternion.Inverse(m_PreviousReferenceOrientation)
* targetOrientation).eulerAngles;
for (int i = 0; i < 3; ++i)
if (relative[i] > 180)
relative[i] -= 360;
relative = Damper.Damp(relative, AngularDamping, deltaTime);
dampedOrientation = m_PreviousReferenceOrientation * Quaternion.Euler(relative);
}
m_PreviousReferenceOrientation = dampedOrientation;
Vector3 targetPosition = FollowTarget.position;
Vector3 currentPosition = m_PreviousTargetPosition;
Vector3 worldOffset = targetPosition - currentPosition;
// Adjust for damping, which is done in camera-offset-local coords
if (deltaTime >= 0)
{
Quaternion dampingSpace;
if (desiredCameraOffset.AlmostZero())
dampingSpace = VcamState.RawOrientation;
else
dampingSpace = Quaternion.LookRotation(dampedOrientation * desiredCameraOffset.normalized, up);
Vector3 localOffset = Quaternion.Inverse(dampingSpace) * worldOffset;
localOffset = Damper.Damp(localOffset, Damping, deltaTime);
worldOffset = dampingSpace * localOffset;
}
outTargetPosition = m_PreviousTargetPosition = currentPosition + worldOffset;
outTargetOrient = dampedOrientation;
}
/// <summary>
/// Damping speeds for each of the 3 axes of the offset from target
/// </summary>
protected Vector3 Damping
{
get
{
switch (m_BindingMode)
{
case BindingMode.SimpleFollowWithWorldUp:
return new Vector3(0, m_YDamping, m_ZDamping);
default:
return new Vector3(m_XDamping, m_YDamping, m_ZDamping);
}
}
}
/// <summary>
/// Damping speeds for each of the 3 axes of the target's rotation
/// </summary>
protected Vector3 AngularDamping
{
get
{
switch (m_BindingMode)
{
case BindingMode.LockToTargetNoRoll:
return new Vector3(m_PitchDamping, m_YawDamping, 0);
case BindingMode.LockToTargetWithWorldUp:
return new Vector3(0, m_YawDamping, 0);
case BindingMode.LockToTargetOnAssign:
case BindingMode.WorldSpace:
case BindingMode.SimpleFollowWithWorldUp:
return Vector3.zero;
default:
return new Vector3(m_PitchDamping, m_YawDamping, m_RollDamping);
}
}
}
/// <summary>Internal API for the Inspector Editor, so it can draw a marker at the target</summary>
public Vector3 GeTargetCameraPosition(Vector3 worldUp)
{
if (!IsValid)
return Vector3.zero;
return FollowTarget.position + GetReferenceOrientation(worldUp) * EffectiveOffset;
}
/// <summary>State information for damping</summary>
Vector3 m_PreviousTargetPosition = Vector3.zero;
Quaternion m_PreviousReferenceOrientation = Quaternion.identity;
Quaternion m_targetOrientationOnAssign = Quaternion.identity;
Transform m_previousTarget = null;
/// <summary>Internal API for the Inspector Editor, so it can draw a marker at the target</summary>
public Quaternion GetReferenceOrientation(Vector3 worldUp)
{
if (FollowTarget != null)
{
Quaternion targetOrientation = FollowTarget.rotation;
switch (m_BindingMode)
{
case BindingMode.LockToTargetOnAssign:
return m_targetOrientationOnAssign;
case BindingMode.LockToTargetWithWorldUp:
return Uppify(targetOrientation, worldUp);
case BindingMode.LockToTargetNoRoll:
return Quaternion.LookRotation(targetOrientation * Vector3.forward, worldUp);
case BindingMode.LockToTarget:
return targetOrientation;
case BindingMode.SimpleFollowWithWorldUp:
{
Vector3 dir = FollowTarget.position - VcamState.RawPosition;
if (dir.AlmostZero())
break;
return Uppify(Quaternion.LookRotation(dir, worldUp), worldUp);
}
default:
break;
}
}
return Quaternion.identity;
}
static Quaternion Uppify(Quaternion q, Vector3 up)
{
Quaternion r = Quaternion.FromToRotation(q * Vector3.up, up);
return r * q;
}
}
}
|