Skip to content

Commit ef179df

Browse files
committed
Optimizations
1 parent 5ff69c1 commit ef179df

File tree

4 files changed

+52
-51
lines changed

4 files changed

+52
-51
lines changed

Packages/webxr-interactions/Runtime/Scripts/ControllerInteraction.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,10 @@ private void OnHandUpdate(WebXRHandData handData)
256256
{
257257
for (int i = 0; i <= (int)WebXRHandJoint.pinky_finger_tip; i++)
258258
{
259-
if (handData.joints[i].enabled)
259+
if (handModelJoints.ContainsKey(i))
260260
{
261-
if (handModelJoints.ContainsKey(i))
262-
{
263-
handModelJoints[i].localPosition = rotationOffset * (handData.joints[i].position - handData.joints[0].position);
264-
handModelJoints[i].localRotation = rotationOffset * handData.joints[i].rotation;
265-
}
261+
handModelJoints[i].localPosition = rotationOffset * (handData.joints[i].position - handData.joints[0].position);
262+
handModelJoints[i].localRotation = rotationOffset * handData.joints[i].rotation;
266263
}
267264
}
268265
return;
@@ -271,34 +268,31 @@ private void OnHandUpdate(WebXRHandData handData)
271268

272269
for (int i = 0; i <= (int)WebXRHandJoint.pinky_finger_tip; i++)
273270
{
274-
if (handData.joints[i].enabled)
271+
if (handJoints.ContainsKey(i))
275272
{
276-
if (handJoints.ContainsKey(i))
273+
handJoints[i].localPosition = rotationOffset * (handData.joints[i].position - handData.joints[0].position);
274+
handJoints[i].localRotation = rotationOffset * handData.joints[i].rotation;
275+
if (handData.joints[i].radius != handJoints[i].localScale.x && handData.joints[i].radius > 0)
276+
{
277+
handJoints[i].localScale = new Vector3(handData.joints[i].radius, handData.joints[i].radius, handData.joints[i].radius);
278+
}
279+
}
280+
else
281+
{
282+
var clone = Instantiate(handJointPrefab,
283+
rotationOffset * (handData.joints[i].position - handData.joints[0].position),
284+
rotationOffset * handData.joints[i].rotation,
285+
transform);
286+
if (handData.joints[i].radius > 0f)
277287
{
278-
handJoints[i].localPosition = rotationOffset * (handData.joints[i].position - handData.joints[0].position);
279-
handJoints[i].localRotation = rotationOffset * handData.joints[i].rotation;
280-
if (handData.joints[i].radius != handJoints[i].localScale.x && handData.joints[i].radius > 0)
281-
{
282-
handJoints[i].localScale = new Vector3(handData.joints[i].radius, handData.joints[i].radius, handData.joints[i].radius);
283-
}
288+
clone.localScale = new Vector3(handData.joints[i].radius, handData.joints[i].radius, handData.joints[i].radius);
284289
}
285290
else
286291
{
287-
var clone = Instantiate(handJointPrefab,
288-
rotationOffset * (handData.joints[i].position - handData.joints[0].position),
289-
rotationOffset * handData.joints[i].rotation,
290-
transform);
291-
if (handData.joints[i].radius > 0f)
292-
{
293-
clone.localScale = new Vector3(handData.joints[i].radius, handData.joints[i].radius, handData.joints[i].radius);
294-
}
295-
else
296-
{
297-
clone.localScale = new Vector3(0.005f, 0.005f, 0.005f);
298-
}
299-
handJoints.Add(i, clone);
300-
handJointsVisuals[i] = clone.gameObject;
292+
clone.localScale = new Vector3(0.005f, 0.005f, 0.005f);
301293
}
294+
handJoints.Add(i, clone);
295+
handJointsVisuals[i] = clone.gameObject;
302296
}
303297
}
304298
}

Packages/webxr/Runtime/Plugins/WebGL/webxr.jspre

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ setTimeout(function () {
122122
this.radii = new Float32Array(25);
123123
this.jointQuaternion = new Float32Array(4);
124124
this.jointIndex = 0;
125+
this.handValues = null;
126+
this.handValuesType = 0;
125127
}
126128

127129
function XRJointData() {
128-
this.enabled = 0;
129130
this.position = [0, 0, 0];
130131
this.rotation = [0, 0, 0, 1];
131132
this.radius = 0;
@@ -618,23 +619,32 @@ setTimeout(function () {
618619
xrHand.hand = 2;
619620
}
620621
xrHand.enabled = 1;
621-
if (inputSource.hand.values) {
622-
if (!frame.fillPoses(inputSource.hand.values(), refSpace, xrHand.poses)) {
623-
xrHand.enabled = 0;
624-
continue;
625-
}
626-
frame.fillJointRadii(inputSource.hand.values(), xrHand.radii);
627-
} else {
628-
if (!frame.fillPoses(inputSource.hand, refSpace, xrHand.poses)) {
629-
xrHand.enabled = 0;
630-
continue;
631-
}
632-
frame.fillJointRadii(inputSource.hand, xrHand.radii);
622+
623+
switch (xrHand.handValuesType) {
624+
case 0:
625+
if (inputSource.hand.values) {
626+
xrHand.handValues = inputSource.hand.values();
627+
xrHand.handValuesType = 1
628+
} else {
629+
xrHand.handValues = inputSource.hand;
630+
xrHand.handValuesType = 2
631+
}
632+
break;
633+
case 1:
634+
xrHand.handValues = inputSource.hand.values();
635+
break;
636+
case 2:
637+
xrHand.handValues = inputSource.hand;
638+
break;
639+
}
640+
if (!frame.fillPoses(xrHand.handValues, refSpace, xrHand.poses)) {
641+
xrHand.enabled = 0;
642+
continue;
633643
}
644+
frame.fillJointRadii(xrHand.handValues, xrHand.radii);
634645
for (var j = 0; j < 25; j++) {
635646
xrHand.jointIndex = j*16;
636647
if (!isNaN(xrHand.poses[xrHand.jointIndex])) {
637-
xrHand.joints[j].enabled = 1;
638648
xrHand.joints[j].position[0] = xrHand.poses[xrHand.jointIndex+12];
639649
xrHand.joints[j].position[1] = xrHand.poses[xrHand.jointIndex+13];
640650
xrHand.joints[j].position[2] = -xrHand.poses[xrHand.jointIndex+14];
@@ -1065,7 +1075,6 @@ setTimeout(function () {
10651075
Module.HandsArray[index++] = data[key].trigger;
10661076
Module.HandsArray[index++] = data[key].squeeze;
10671077
for (var j = 0; j < 25; j++) {
1068-
Module.HandsArray[index++] = data[key].joints[j].enabled;
10691078
Module.HandsArray[index++] = data[key].joints[j].position[0];
10701079
Module.HandsArray[index++] = data[key].joints[j].position[1];
10711080
Module.HandsArray[index++] = data[key].joints[j].position[2];

Packages/webxr/Runtime/Scripts/WebXRControllerData.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public class WebXRHandData
7575
[System.Serializable]
7676
public struct WebXRJointData
7777
{
78-
public bool enabled;
7978
public Vector3 position;
8079
public Quaternion rotation;
8180
public float radius;

Packages/webxr/Runtime/XRPlugin/WebXRSubsystem.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ internal void OnUpdate()
8787
reportedXRStateSwitch = true;
8888
OnXRChange?.Invoke(xrState, viewsCount, leftRect, rightRect);
8989
}
90-
if (switchToEnd)
90+
if (!updatedControllersOnEnd)
9191
{
92-
switchToEnd = false;
92+
updatedControllersOnEnd = true;
9393
UpdateControllersOnEnd();
9494
}
9595
if (this.xrState == WebXRState.NORMAL)
@@ -260,14 +260,14 @@ public delegate void HeadsetUpdate(
260260
float[] controllersArray = new float[2 * 28];
261261

262262
// Shared array for hands data
263-
float[] handsArray = new float[2 * (25 * 9 + 5)];
263+
float[] handsArray = new float[2 * (25 * 8 + 5)];
264264

265265
// Shared array for hit-test pose data
266266
float[] viewerHitTestPoseArray = new float[9];
267267

268268
bool viewerHitTestOn = false;
269269

270-
private bool switchToEnd = false;
270+
private bool updatedControllersOnEnd = true;
271271

272272
private WebXRHandData leftHand = new WebXRHandData();
273273
private WebXRHandData rightHand = new WebXRHandData();
@@ -342,7 +342,7 @@ public static void OnStartVR(int viewsCount,
342342
[MonoPInvokeCallback(typeof(Action))]
343343
public static void OnEndXR()
344344
{
345-
Instance.switchToEnd = true;
345+
Instance.updatedControllersOnEnd = false;
346346
Instance.setXrState(WebXRState.NORMAL, 1, new Rect(), new Rect());
347347
}
348348

@@ -462,7 +462,7 @@ bool GetGamepadFromControllersArray(int controllerIndex, ref WebXRControllerData
462462

463463
bool GetHandFromHandsArray(int handIndex, ref WebXRHandData handObject)
464464
{
465-
int arrayPosition = handIndex * 230;
465+
int arrayPosition = handIndex * 205;
466466
int frameNumber = (int)handsArray[arrayPosition++];
467467
if (handObject.frame == frameNumber)
468468
{
@@ -481,7 +481,6 @@ bool GetHandFromHandsArray(int handIndex, ref WebXRHandData handObject)
481481

482482
for (int i = 0; i <= (int)WebXRHandJoint.pinky_finger_tip; i++)
483483
{
484-
handObject.joints[i].enabled = handsArray[arrayPosition++] != 0;
485484
handObject.joints[i].position = new Vector3(handsArray[arrayPosition++], handsArray[arrayPosition++], handsArray[arrayPosition++]);
486485
handObject.joints[i].rotation = new Quaternion(handsArray[arrayPosition++], handsArray[arrayPosition++], handsArray[arrayPosition++],
487486
handsArray[arrayPosition++]);

0 commit comments

Comments
 (0)