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
|
using System;
using Unity.Mathematics;
using Unity.XR.CoreUtils.Bindings;
using UnityEngine.XR.Interaction.Toolkit.AffordanceSystem.State;
using UnityEngine.XR.Interaction.Toolkit.Filtering;
using UnityEngine.XR.Interaction.Toolkit.Utilities.Tweenables.Primitives;
namespace UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets
{
/// <summary>
/// Follow animation affordance for <see cref="IPokeStateDataProvider"/>, such as <see cref="XRPokeFilter"/>.
/// Used to animate a pressed transform, such as a button to follow the poke position.
/// </summary>
/// <remarks>
/// The Affordance System namespace and all associated classes have been deprecated.
/// The existing affordance system will be moved, replaced and updated with a new interaction
/// feedback system in a future version of XRI, including this sample script.
/// </remarks>
[AddComponentMenu("XR/XR Poke Follow Affordance", 22)]
public class XRPokeFollowAffordance : MonoBehaviour
{
[SerializeField]
[Tooltip("Transform that will move in the poke direction when this or a parent GameObject is poked." +
"\nNote: Should be a direct child GameObject.")]
Transform m_PokeFollowTransform;
/// <summary>
/// Transform that will animate along the axis of interaction when this interactable is poked.
/// Note: Must be a direct child GameObject as it moves in local space relative to the poke target's transform.
/// </summary>
public Transform pokeFollowTransform
{
get => m_PokeFollowTransform;
set => m_PokeFollowTransform = value;
}
[SerializeField]
[Range(0f, 20f)]
[Tooltip("Multiplies transform position interpolation as a factor of Time.deltaTime. If 0, no smoothing will be applied.")]
float m_SmoothingSpeed = 16f;
/// <summary>
/// Multiplies transform position interpolation as a factor of <see cref="Time.deltaTime"/>. If <c>0</c>, no smoothing will be applied.
/// </summary>
public float smoothingSpeed
{
get => m_SmoothingSpeed;
set => m_SmoothingSpeed = value;
}
[SerializeField]
[Tooltip("When this component is no longer the target of the poke, the Poke Follow Transform returns to the original position.")]
bool m_ReturnToInitialPosition = true;
/// <summary>
/// When this component is no longer the target of the poke, the <see cref="pokeFollowTransform"/> returns to the original position.
/// </summary>
public bool returnToInitialPosition
{
get => m_ReturnToInitialPosition;
set => m_ReturnToInitialPosition = value;
}
[SerializeField]
[Tooltip("Whether to apply the follow animation if the target of the poke is a child of this transform. " +
"This is useful for UI objects that may have child graphics.")]
bool m_ApplyIfChildIsTarget = true;
/// <summary>
/// Whether to apply the follow animation if the target of the poke is a child of this transform.
/// This is useful for UI objects that may have child graphics.
/// </summary>
public bool applyIfChildIsTarget
{
get => m_ApplyIfChildIsTarget;
set => m_ApplyIfChildIsTarget = value;
}
[SerializeField]
[Tooltip("Whether to keep the Poke Follow Transform from moving past a maximum distance from the poke target.")]
bool m_ClampToMaxDistance;
/// <summary>
/// Whether to keep the <see cref="pokeFollowTransform"/> from moving past <see cref="maxDistance"/> from the poke target.
/// </summary>
public bool clampToMaxDistance
{
get => m_ClampToMaxDistance;
set => m_ClampToMaxDistance = value;
}
[SerializeField]
[Tooltip("The maximum distance from this transform that the Poke Follow Transform can move.")]
float m_MaxDistance;
/// <summary>
/// The maximum distance from this transform that the <see cref="pokeFollowTransform"/> can move when
/// <see cref="clampToMaxDistance"/> is <see langword="true"/>.
/// </summary>
public float maxDistance
{
get => m_MaxDistance;
set => m_MaxDistance = value;
}
/// <summary>
/// The original position of this interactable before any pushes have been applied.
/// </summary>
public Vector3 initialPosition
{
get => m_InitialPosition;
set => m_InitialPosition = value;
}
IPokeStateDataProvider m_PokeDataProvider;
IMultiPokeStateDataProvider m_MultiPokeStateDataProvider;
#pragma warning disable CS0618 // Type or member is obsolete
readonly Vector3TweenableVariable m_TransformTweenableVariable = new Vector3TweenableVariable();
#pragma warning restore CS0618 // Type or member is obsolete
readonly BindingsGroup m_BindingsGroup = new BindingsGroup();
Vector3 m_InitialPosition;
bool m_IsFirstFrame;
[HideInInspector]
[SerializeField]
XRPokeFilter m_PokeFilter = null;
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void Awake()
{
m_MultiPokeStateDataProvider = GetComponentInParent<IMultiPokeStateDataProvider>();
if (m_MultiPokeStateDataProvider == null)
m_PokeDataProvider = GetComponentInParent<IPokeStateDataProvider>();
}
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void Start()
{
if (m_PokeFollowTransform != null)
{
m_InitialPosition = m_PokeFollowTransform.localPosition;
m_BindingsGroup.AddBinding(m_TransformTweenableVariable.Subscribe(OnTransformTweenableVariableUpdated));
if (m_MultiPokeStateDataProvider != null)
m_BindingsGroup.AddBinding(m_MultiPokeStateDataProvider.GetPokeStateDataForTarget(transform).Subscribe(OnPokeStateDataUpdated));
else if (m_PokeDataProvider != null)
m_BindingsGroup.AddBinding(m_PokeDataProvider.pokeStateData.SubscribeAndUpdate(OnPokeStateDataUpdated));
}
else
{
enabled = false;
Debug.LogWarning($"Missing Poke Follow Transform assignment on {this}. Disabling component.", this);
}
}
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void OnDestroy()
{
m_BindingsGroup.Clear();
m_TransformTweenableVariable?.Dispose();
}
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void LateUpdate()
{
if (m_IsFirstFrame)
{
m_TransformTweenableVariable.HandleTween(1f);
m_IsFirstFrame = false;
return;
}
m_TransformTweenableVariable.HandleTween(m_SmoothingSpeed > 0f ? Time.deltaTime * m_SmoothingSpeed : 1f);
}
protected virtual void OnTransformTweenableVariableUpdated(float3 position)
{
// UI Anchors can cause this to not work correctly, so we check if it's a RectTransform and set the localPosition Z only
if (m_PokeFollowTransform is RectTransform)
{
var targetPosition = m_PokeFollowTransform.localPosition;
targetPosition.z = position.z;
m_PokeFollowTransform.localPosition = targetPosition;
}
else
{
m_PokeFollowTransform.localPosition = position;
}
}
void OnPokeStateDataUpdated(PokeStateData data)
{
var pokeTarget = data.target;
var applyFollow = m_ApplyIfChildIsTarget
? pokeTarget != null && pokeTarget.IsChildOf(transform)
: pokeTarget == transform;
if (applyFollow)
{
var targetPosition = pokeTarget.InverseTransformPoint(data.axisAlignedPokeInteractionPoint);
if (m_ClampToMaxDistance && targetPosition.sqrMagnitude > m_MaxDistance * m_MaxDistance)
targetPosition = Vector3.ClampMagnitude(targetPosition, m_MaxDistance);
m_TransformTweenableVariable.target = targetPosition;
}
else if (m_ReturnToInitialPosition)
{
m_TransformTweenableVariable.target = m_InitialPosition;
}
}
public void ResetFollowTransform()
{
if (!m_ClampToMaxDistance || m_PokeFollowTransform == null)
return;
m_PokeFollowTransform.localPosition = m_InitialPosition;
}
void OnDrawGizmos()
{
if (!TryGetTargetEndPoint(out var endPoint))
return;
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, endPoint);
}
bool TryGetTargetEndPoint(out Vector3 endPoint)
{
if (!m_ClampToMaxDistance || m_PokeFilter == null)
{
endPoint = Vector3.zero;
return false;
}
Vector3 origin = transform.position;
Vector3 direction = ComputeRotatedDepthEvaluationAxis(m_PokeFilter.pokeConfiguration);
endPoint = origin + direction.normalized * m_MaxDistance;
return true;
}
Vector3 ComputeRotatedDepthEvaluationAxis(PokeThresholdData pokeThresholdData)
{
if (pokeThresholdData == null)
return Vector3.zero;
Vector3 rotatedDepthEvaluationAxis = Vector3.zero;
switch (pokeThresholdData.pokeDirection)
{
case PokeAxis.X:
case PokeAxis.NegativeX:
rotatedDepthEvaluationAxis = transform.right;
break;
case PokeAxis.Y:
case PokeAxis.NegativeY:
rotatedDepthEvaluationAxis = transform.up;
break;
case PokeAxis.Z:
case PokeAxis.NegativeZ:
rotatedDepthEvaluationAxis = transform.forward;
break;
}
switch (pokeThresholdData.pokeDirection)
{
case PokeAxis.X:
case PokeAxis.Y:
case PokeAxis.Z:
rotatedDepthEvaluationAxis = -rotatedDepthEvaluationAxis;
break;
}
return rotatedDepthEvaluationAxis;
}
void OnValidate()
{
if (m_PokeFilter == null)
{
m_PokeFilter = GetComponentInParent<XRPokeFilter>();
}
// Visually update the end point to match the target clamped position
if (m_PokeFollowTransform != null && TryGetTargetEndPoint(out var endPoint))
m_PokeFollowTransform.position = endPoint;
}
}
}
|