-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
RadialView.cs
299 lines (254 loc) · 10.8 KB
/
RadialView.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.Utilities.Solvers
{
/// <summary>
/// RadialViewPoser solver locks a tag-along type object within a view cone
/// </summary>
[AddComponentMenu("Scripts/MRTK/SDK/RadialView")]
public class RadialView : Solver
{
[SerializeField]
[Tooltip("Which direction to position the element relative to: HeadOriented rolls with the head, HeadFacingWorldUp view direction but ignores head roll, and HeadMoveDirection uses the direction the head last moved without roll")]
private RadialViewReferenceDirection referenceDirection = RadialViewReferenceDirection.FacingWorldUp;
/// <summary>
/// Which direction to position the element relative to:
/// HeadOriented rolls with the head,
/// HeadFacingWorldUp view direction but ignores head roll,
/// and HeadMoveDirection uses the direction the head last moved without roll.
/// </summary>
public RadialViewReferenceDirection ReferenceDirection
{
get => referenceDirection;
set => referenceDirection = value;
}
[SerializeField]
[Tooltip("Min distance from eye to position element around, i.e. the sphere radius")]
private float minDistance = 1f;
/// <summary>
/// Min distance from eye to position element around, i.e. the sphere radius.
/// </summary>
public float MinDistance
{
get => minDistance;
set => minDistance = value;
}
[SerializeField]
[Tooltip("Max distance from eye to element")]
private float maxDistance = 2f;
/// <summary>
/// Max distance from eye to element.
/// </summary>
public float MaxDistance
{
get => maxDistance;
set => maxDistance = value;
}
[SerializeField]
[Tooltip("The element will stay at least this far away from the center of view")]
private float minViewDegrees = 0f;
/// <summary>
/// The element will stay at least this far away from the center of view.
/// </summary>
public float MinViewDegrees
{
get => minViewDegrees;
set => minViewDegrees = value;
}
[SerializeField]
[Tooltip("The element will stay at least this close to the center of view")]
private float maxViewDegrees = 30f;
/// <summary>
/// The element will stay at least this close to the center of view.
/// </summary>
public float MaxViewDegrees
{
get => maxViewDegrees;
set => maxViewDegrees = value;
}
[SerializeField]
[Tooltip("Apply a different clamp to vertical FOV than horizontal. Vertical = Horizontal * aspectV")]
private float aspectV = 1f;
/// <summary>
/// Apply a different clamp to vertical FOV than horizontal. Vertical = Horizontal * AspectV.
/// </summary>
public float AspectV
{
get => aspectV;
set => aspectV = value;
}
[SerializeField]
[Tooltip("Option to ignore angle clamping")]
private bool ignoreAngleClamp = false;
/// <summary>
/// Option to ignore angle clamping.
/// </summary>
public bool IgnoreAngleClamp
{
get => ignoreAngleClamp;
set => ignoreAngleClamp = value;
}
[SerializeField]
[Tooltip("Option to ignore distance clamping")]
private bool ignoreDistanceClamp = false;
/// <summary>
/// Option to ignore distance clamping.
/// </summary>
public bool IgnoreDistanceClamp
{
get => ignoreDistanceClamp;
set => ignoreDistanceClamp = value;
}
[SerializeField]
[Tooltip("Ignore vertical movement and lock the Y position of the object")]
private bool useFixedVerticalPosition = false;
/// <summary>
/// Ignore vertical movement and lock the Y position of the object.
/// </summary>
public bool UseFixedVerticalPosition
{
get => useFixedVerticalPosition;
set => useFixedVerticalPosition = value;
}
[SerializeField]
[Tooltip("Offset amount of the vertical position")]
private float fixedVerticalPosition = -0.4f;
/// <summary>
/// Offset amount of the vertical position.
/// </summary>
public float FixedVerticalPosition
{
get => fixedVerticalPosition;
set => fixedVerticalPosition = value;
}
[SerializeField]
[Tooltip("If true, element will orient to ReferenceDirection, otherwise it will orient to ref position.")]
private bool orientToReferenceDirection = false;
/// <summary>
/// If true, element will orient to ReferenceDirection, otherwise it will orient to ref position.
/// </summary>
public bool OrientToReferenceDirection
{
get => orientToReferenceDirection;
set => orientToReferenceDirection = value;
}
/// <summary>
/// Position to the view direction, or the movement direction, or the direction of the view cone.
/// </summary>
private Vector3 SolverReferenceDirection => SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.forward : Vector3.forward;
/// <summary>
/// The up direction to use for orientation.
/// </summary>
/// <remarks>Cone may roll with head, or not.</remarks>
private Vector3 UpReference
{
get
{
Vector3 upReference = Vector3.up;
if (referenceDirection == RadialViewReferenceDirection.ObjectOriented)
{
upReference = SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.up : Vector3.up;
}
return upReference;
}
}
private Vector3 ReferencePoint => SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.position : Vector3.zero;
/// <inheritdoc />
public override void SolverUpdate()
{
Vector3 goalPosition = WorkingPosition;
if (ignoreAngleClamp)
{
if (ignoreDistanceClamp)
{
goalPosition = transform.position;
}
else
{
GetDesiredOrientation_DistanceOnly(ref goalPosition);
}
}
else
{
GetDesiredOrientation(ref goalPosition);
}
// Element orientation
Vector3 refDirUp = UpReference;
Quaternion goalRotation;
if (orientToReferenceDirection)
{
goalRotation = Quaternion.LookRotation(SolverReferenceDirection, refDirUp);
}
else
{
goalRotation = Quaternion.LookRotation(goalPosition - ReferencePoint, refDirUp);
}
// If gravity aligned then zero out the x and z axes on the rotation
if (referenceDirection == RadialViewReferenceDirection.GravityAligned)
{
goalRotation.x = goalRotation.z = 0f;
}
if (UseFixedVerticalPosition)
{
goalPosition.y = ReferencePoint.y + FixedVerticalPosition;
}
GoalPosition = goalPosition;
GoalRotation = goalRotation;
}
/// <summary>
/// Optimized version of GetDesiredOrientation.
/// </summary>
private void GetDesiredOrientation_DistanceOnly(ref Vector3 desiredPos)
{
// TODO: There should be a different solver for distance constraint.
// Determine reference locations and directions
Vector3 refPoint = ReferencePoint;
Vector3 elementPoint = transform.position;
Vector3 elementDelta = elementPoint - refPoint;
float elementDist = elementDelta.magnitude;
Vector3 elementDir = elementDist > 0 ? elementDelta / elementDist : Vector3.one;
// Clamp distance too
float clampedDistance = Mathf.Clamp(elementDist, minDistance, maxDistance);
if (!clampedDistance.Equals(elementDist))
{
desiredPos = refPoint + clampedDistance * elementDir;
}
}
private void GetDesiredOrientation(ref Vector3 desiredPos)
{
// Determine reference locations and directions
Vector3 direction = SolverReferenceDirection;
Vector3 upDirection = UpReference;
Vector3 referencePoint = ReferencePoint;
Vector3 elementPoint = transform.position;
Vector3 elementDelta = elementPoint - referencePoint;
float elementDist = elementDelta.magnitude;
Vector3 elementDir = elementDist > 0 ? elementDelta / elementDist : Vector3.one;
// Generate basis: First get axis perpendicular to reference direction pointing toward element
Vector3 perpendicularDirection = (elementDir - direction);
perpendicularDirection -= direction * Vector3.Dot(perpendicularDirection, direction);
perpendicularDirection.Normalize();
// Calculate the clamping angles, accounting for aspect (need the angle relative to view plane)
float heightToViewAngle = Vector3.Angle(perpendicularDirection, upDirection);
float verticalAspectScale = Mathf.Lerp(aspectV, 1f, Mathf.Abs(Mathf.Sin(heightToViewAngle * Mathf.Deg2Rad)));
// Calculate the current angle
float currentAngle = Vector3.Angle(elementDir, direction);
float currentAngleClamped = Mathf.Clamp(currentAngle, minViewDegrees * verticalAspectScale, maxViewDegrees * verticalAspectScale);
// Clamp distance too, if desired
float clampedDistance = ignoreDistanceClamp ? elementDist : Mathf.Clamp(elementDist, minDistance, maxDistance);
// If the angle was clamped, do some special update stuff
if (currentAngle != currentAngleClamped)
{
float angRad = currentAngleClamped * Mathf.Deg2Rad;
// Calculate new position
desiredPos = referencePoint + clampedDistance * (direction * Mathf.Cos(angRad) + perpendicularDirection * Mathf.Sin(angRad));
}
else if (!clampedDistance.Equals(elementDist))
{
// Only need to apply distance
desiredPos = referencePoint + clampedDistance * elementDir;
}
}
}
}