Skip to content

Commit 5ef4c19

Browse files
committed
WebXRControllerModel optimizations
1 parent 809c0dd commit 5ef4c19

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

Packages/webxr-interactions/Runtime/InputSystem/WebXRControllerModel.cs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ public UpdateType updateType
169169
bool m_RotationBound;
170170
bool m_PositionBound;
171171
bool m_TrackingStateBound;
172-
private bool visualActionsInit;
172+
private bool visualActionsBound;
173+
private bool visualActionsUpdated;
173174
bool m_IsFirstUpdate = true;
174175

175176
private Transform xrControllerOldModel;
@@ -180,14 +181,15 @@ void BindActions()
180181
BindPosition();
181182
BindRotation();
182183
BindTrackingState();
183-
InitVisualActions();
184+
BindVisualActions();
184185
}
185186

186187
void UnbindActions()
187188
{
188189
UnbindPosition();
189190
UnbindRotation();
190191
UnbindTrackingState();
192+
UnbindVisualActions();
191193
}
192194

193195
void BindPosition()
@@ -232,9 +234,9 @@ void BindTrackingState()
232234
m_TrackingStateBound = true;
233235
}
234236

235-
void InitVisualActions()
237+
void BindVisualActions()
236238
{
237-
if (visualActionsInit)
239+
if (visualActionsBound)
238240
return;
239241

240242
buttonActions = new InputAction[6];
@@ -248,7 +250,18 @@ void InitVisualActions()
248250
axisActions[0] = touchpadActionProperty.action;
249251
axisActions[1] = thumbstickActionProperty.action;
250252

251-
visualActionsInit = true;
253+
for (int i = 0; i < buttonActions.Length; i++)
254+
{
255+
buttonActions[i].performed += OnVisualActionsPerformed;
256+
buttonActions[i].canceled += OnVisualActionsCanceled;
257+
}
258+
for (int i = 0; i < axisActions.Length; i++)
259+
{
260+
axisActions[i].performed += OnVisualActionsPerformed;
261+
axisActions[i].canceled += OnVisualActionsCanceled;
262+
}
263+
264+
visualActionsBound = true;
252265
}
253266

254267
void UnbindPosition()
@@ -290,6 +303,25 @@ void UnbindTrackingState()
290303
m_TrackingStateBound = false;
291304
}
292305

306+
void UnbindVisualActions()
307+
{
308+
if (!visualActionsBound)
309+
return;
310+
311+
for (int i = 0; i < buttonActions.Length; i++)
312+
{
313+
buttonActions[i].performed -= OnVisualActionsPerformed;
314+
buttonActions[i].canceled -= OnVisualActionsCanceled;
315+
}
316+
for (int i = 0; i < axisActions.Length; i++)
317+
{
318+
axisActions[i].performed -= OnVisualActionsPerformed;
319+
axisActions[i].canceled -= OnVisualActionsCanceled;
320+
}
321+
322+
visualActionsBound = false;
323+
}
324+
293325
void OnPositionPerformed(InputAction.CallbackContext context)
294326
{
295327
m_CurrentPosition = context.ReadValue<Vector3>();
@@ -328,6 +360,16 @@ void OnTrackingStateCanceled(InputAction.CallbackContext context)
328360
}
329361
}
330362

363+
void OnVisualActionsPerformed(InputAction.CallbackContext context)
364+
{
365+
visualActionsUpdated = true;
366+
}
367+
368+
void OnVisualActionsCanceled(InputAction.CallbackContext context)
369+
{
370+
visualActionsUpdated = true;
371+
}
372+
331373
private void Awake()
332374
{
333375
inputProfileLoader = rigOrigin.GetComponent<InputProfileLoader>();
@@ -472,7 +514,7 @@ protected virtual void OnBeforeRender()
472514
protected virtual void PerformUpdate()
473515
{
474516
SetLocalTransform(m_CurrentPosition, m_CurrentRotation);
475-
if (loadedModel && m_CurrentTrackingState != TrackingStates.None)
517+
if (loadedModel && visualActionsUpdated)
476518
{
477519
UpdateModelInput();
478520
}
@@ -513,6 +555,11 @@ protected virtual void SetLocalTransform(Vector3 newPosition, Quaternion newRota
513555

514556
private void UpdateModelInput()
515557
{
558+
if (m_CurrentTrackingState == TrackingStates.None)
559+
{
560+
return;
561+
}
562+
visualActionsUpdated = false;
516563
for (int i = 0; i < 6; i++)
517564
{
518565
inputProfileModel.SetButtonValue(i, buttonActions[i].ReadValue<float>());

0 commit comments

Comments
 (0)