-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GrabInteractableFollowAction.cs
294 lines (278 loc) · 12.5 KB
/
GrabInteractableFollowAction.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
namespace VRTK.Prefabs.Interactions.Interactables.Grab.Action
{
using UnityEngine;
using Malimbe.MemberChangeMethod;
using Malimbe.XmlDocumentationAttribute;
using Malimbe.PropertySerializationAttribute;
using Zinnia.Extension;
using Zinnia.Data.Attribute;
using Zinnia.Tracking.Velocity;
using Zinnia.Tracking.Follow;
using Zinnia.Tracking.Follow.Modifier;
/// <summary>
/// Describes an action that allows the Interactable to follow an Interactor's position, rotation and scale.
/// </summary>
public class GrabInteractableFollowAction : GrabInteractableAction
{
/// <summary>
/// The way in which the object is moved.
/// </summary>
public enum TrackingType
{
/// <summary>
/// Updates the transform data directly, outside of the physics system.
/// </summary>
FollowTransform,
/// <summary>
/// Updates the rigidbody using velocity to stay within the bounds of the physics system.
/// </summary>
FollowRigidbody,
/// <summary>
/// Updates the rigidbody rotation by using a force at position.
/// </summary>
FollowRigidbodyForceRotate,
/// <summary>
/// Updates the transform rotation based on the position difference of the source.
/// </summary>
FollowTransformPositionDifferenceRotate
}
/// <summary>
/// The offset to apply on grab.
/// </summary>
public enum OffsetType
{
/// <summary>
/// No offset is applied.
/// </summary>
None,
/// <summary>
/// An offset of a specified <see cref="GameObject"/> is applied to orientate the interactable on grab.
/// </summary>
OrientationHandle,
/// <summary>
/// An offset of where the collision between the Interactor and Interactable is applied for precision grabbing.
/// </summary>
PrecisionPoint,
/// <summary>
/// The precision point offset will always be re-created based on the latest Interactor grabbing the Interactable.
/// </summary>
ForcePrecisionPoint
}
#region Interactable Settings
/// <summary>
/// Determines how to track the movement of interactable to the interactor.
/// </summary>
[Serialized]
[field: Header("Interactable Settings"), DocumentedByXml]
public TrackingType FollowTracking { get; set; }
/// <summary>
/// The offset to apply when grabbing the Interactable.
/// </summary>
[Serialized]
[field: DocumentedByXml]
public OffsetType GrabOffset { get; set; }
#endregion
#region Tracking Settings
/// <summary>
/// The <see cref="Zinnia.Tracking.Follow.ObjectFollower"/> for tracking movement.
/// </summary>
[Serialized]
[field: Header("Interactable Settings"), DocumentedByXml, Restricted]
public ObjectFollower ObjectFollower { get; protected set; }
/// <summary>
/// The <see cref="Zinnia.Tracking.Follow.Modifier.FollowModifier"/> to move by following the transform.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public FollowModifier FollowTransformModifier { get; protected set; }
/// <summary>
/// The <see cref="Zinnia.Tracking.Follow.Modifier.FollowModifier"/> to move by applying velocities to the rigidbody.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public FollowModifier FollowRigidbodyModifier { get; protected set; }
/// <summary>
/// The <see cref="Zinnia.Tracking.Follow.Modifier.FollowModifier"/> to rotate by applying a force to the rigidbody.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public FollowModifier FollowRigidbodyForceRotateModifier { get; protected set; }
/// <summary>
/// The <see cref="Zinnia.Tracking.Follow.Modifier.FollowModifier"/> to rotate by the angle difference between the source positions.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public FollowModifier FollowTransformRotateOnPositionDifferenceModifier { get; protected set; }
/// <summary>
/// The <see cref="Zinnia.Tracking.Velocity.VelocityApplier"/> to apply velocity on ungrab.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public VelocityApplier VelocityApplier { get; protected set; }
#endregion
#region Grab Offset Settings
/// <summary>
/// The container for the precision point logic.
/// </summary>
[Serialized]
[field: Header("Grab Offset Settings"), DocumentedByXml, Restricted]
public GameObject PrecisionLogicContainer { get; protected set; }
/// <summary>
/// The container for the precision point creation logic.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public GameObject PrecisionCreateContainer { get; protected set; }
/// <summary>
/// The container for the precision point force creation logic.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public GameObject PrecisionForceCreateContainer { get; protected set; }
/// <summary>
/// The container for the orientation handle logic.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public GameObject OrientationLogicContainer { get; protected set; }
#endregion
#region Kinematic Settings
/// <summary>
/// The container for the enable kinematic logic.
/// </summary>
[Serialized]
[field: Header("Kinematic Settings"), DocumentedByXml, Restricted]
public GameObject EnableKinematicContainer { get; protected set; }
/// <summary>
/// The container for the disable kinematic logic.
/// </summary>
[Serialized]
[field: DocumentedByXml, Restricted]
public GameObject DisableKinematicContainer { get; protected set; }
#endregion
/// <summary>
/// The kinematic state of the consumer rigidbody.
/// </summary>
public bool IsConsumerRigidbodyKinematic { get; set; }
protected virtual void OnEnable()
{
ConfigureFollowTracking();
ConfigureGrabOffset();
}
/// <summary>
/// Configures the appropriate processes to be used for follow tracking based on the <see cref="FollowTracking"/> setting.
/// </summary>
protected virtual void ConfigureFollowTracking()
{
switch (FollowTracking)
{
case TrackingType.FollowTransform:
FollowTransformModifier.gameObject.SetActive(true);
FollowRigidbodyModifier.gameObject.SetActive(false);
FollowRigidbodyForceRotateModifier.gameObject.SetActive(false);
FollowTransformRotateOnPositionDifferenceModifier.gameObject.SetActive(false);
ObjectFollower.FollowModifier = FollowTransformModifier;
ToggleKinematicLogic(true);
break;
case TrackingType.FollowRigidbody:
FollowTransformModifier.gameObject.SetActive(false);
FollowRigidbodyModifier.gameObject.SetActive(true);
FollowRigidbodyForceRotateModifier.gameObject.SetActive(false);
FollowTransformRotateOnPositionDifferenceModifier.gameObject.SetActive(false);
ObjectFollower.FollowModifier = FollowRigidbodyModifier;
ToggleKinematicLogic(false);
break;
case TrackingType.FollowRigidbodyForceRotate:
FollowTransformModifier.gameObject.SetActive(false);
FollowRigidbodyModifier.gameObject.SetActive(false);
FollowRigidbodyForceRotateModifier.gameObject.SetActive(true);
FollowTransformRotateOnPositionDifferenceModifier.gameObject.SetActive(false);
ObjectFollower.FollowModifier = FollowRigidbodyForceRotateModifier;
ToggleKinematicLogic(false);
break;
case TrackingType.FollowTransformPositionDifferenceRotate:
FollowTransformModifier.gameObject.SetActive(false);
FollowRigidbodyModifier.gameObject.SetActive(false);
FollowRigidbodyForceRotateModifier.gameObject.SetActive(false);
FollowTransformRotateOnPositionDifferenceModifier.gameObject.SetActive(true);
ObjectFollower.FollowModifier = FollowTransformRotateOnPositionDifferenceModifier;
ToggleKinematicLogic(false);
break;
}
}
/// <summary>
/// Configures the appropriate processes to be used for grab offset based on the <see cref="GrabOffset"/> setting.
/// </summary>
protected virtual void ConfigureGrabOffset()
{
switch (GrabOffset)
{
case OffsetType.None:
PrecisionLogicContainer.TrySetActive(false);
OrientationLogicContainer.TrySetActive(false);
break;
case OffsetType.PrecisionPoint:
PrecisionLogicContainer.TrySetActive(true);
PrecisionCreateContainer.TrySetActive(true);
PrecisionForceCreateContainer.TrySetActive(false);
OrientationLogicContainer.TrySetActive(false);
break;
case OffsetType.ForcePrecisionPoint:
PrecisionLogicContainer.TrySetActive(true);
PrecisionForceCreateContainer.TrySetActive(true);
PrecisionCreateContainer.TrySetActive(false);
OrientationLogicContainer.TrySetActive(false);
break;
case OffsetType.OrientationHandle:
PrecisionLogicContainer.TrySetActive(false);
OrientationLogicContainer.TrySetActive(true);
break;
}
}
/// <inheritdoc />
protected override void OnAfterGrabSetupChange()
{
IsConsumerRigidbodyKinematic = GrabSetup.Facade.ConsumerRigidbody != null ? GrabSetup.Facade.ConsumerRigidbody.isKinematic : false;
ObjectFollower.Targets.RunWhenActiveAndEnabled(() => ObjectFollower.Targets.Clear());
ObjectFollower.Targets.RunWhenActiveAndEnabled(() => ObjectFollower.Targets.Add(GrabSetup.Facade.ConsumerContainer));
VelocityApplier.Target = GrabSetup.Facade.ConsumerRigidbody != null ? GrabSetup.Facade.ConsumerRigidbody : null;
}
/// <summary>
/// Toggles the kinematic logic state.
/// </summary>
/// <param name="state">Whether the logic should run or not.</param>
protected virtual void ToggleKinematicLogic(bool state)
{
EnableKinematicContainer.SetActive(state);
DisableKinematicContainer.SetActive(state);
}
/// <summary>
/// Called after <see cref="FollowTracking"/> has been changed.
/// </summary>
[CalledAfterChangeOf(nameof(FollowTracking))]
protected virtual void OnAfterFollowTrackingChange()
{
ConfigureFollowTracking();
}
/// <summary>
/// Called after <see cref="GrabOffset"/> has been changed.
/// </summary>
[CalledAfterChangeOf(nameof(GrabOffset))]
protected virtual void OnAfterGrabOffsetChange()
{
ConfigureGrabOffset();
}
/// <summary>
/// Called after <see cref="IsConsumerRigidbodyKinematic"/> has been changed.
/// </summary>
[CalledAfterChangeOf(nameof(IsConsumerRigidbodyKinematic))]
protected virtual void OnAfterConsumerRigidbodyIsKinematicChange()
{
if (GrabSetup.Facade.ConsumerRigidbody == null)
{
return;
}
GrabSetup.Facade.ConsumerRigidbody.isKinematic = IsConsumerRigidbodyKinematic;
}
}
}