Skip to content

Commit 2a7a1d4

Browse files
authored
Merge pull request #111 from De-Panther/xrhands_optimized_api
Use fillPoses and fillJointRadii for hands data
2 parents 2c38cea + 8ef5914 commit 2a7a1d4

File tree

11 files changed

+164
-86
lines changed

11 files changed

+164
-86
lines changed

Build/Build/Build.data.unityweb

-202 Bytes
Binary file not shown.

Build/Build/Build.wasm

47 Bytes
Binary file not shown.

Build/Build/Build.wasm.framework.unityweb

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

Build/webxr.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
for (let i = 0; i < 25; i++) {
8181
this.joints.push(new XRJointData());
8282
}
83+
this.poses = new Float32Array(16 * 25);
84+
this.radii = new Float32Array(25);
85+
this.jointQuaternion = new Float32Array(4);
86+
this.jointIndex = 0;
8387
}
8488

8589
function XRJointData() {
@@ -544,6 +548,17 @@
544548
);
545549
}
546550

551+
// http://answers.unity.com/answers/11372/view.html
552+
XRManager.prototype.quaternionFromMatrix = function(offset, matrix, quaternion) {
553+
quaternion[3] = Math.sqrt( Math.max( 0, 1 + matrix[offset+0] + matrix[offset+5] + matrix[offset+10] ) ) / 2;
554+
quaternion[0] = Math.sqrt( Math.max( 0, 1 + matrix[offset+0] - matrix[offset+5] - matrix[offset+10] ) ) / 2;
555+
quaternion[1] = Math.sqrt( Math.max( 0, 1 - matrix[offset+0] + matrix[offset+5] - matrix[offset+10] ) ) / 2;
556+
quaternion[2] = Math.sqrt( Math.max( 0, 1 - matrix[offset+0] - matrix[offset+5] + matrix[offset+10] ) ) / 2;
557+
quaternion[0] *= Math.sign( quaternion[0] * ( matrix[offset+6] - matrix[offset+9] ) );
558+
quaternion[1] *= Math.sign( quaternion[1] * ( matrix[offset+8] - matrix[offset+2] ) );
559+
quaternion[2] *= Math.sign( quaternion[2] * ( matrix[offset+1] - matrix[offset+4] ) );
560+
}
561+
547562
XRManager.prototype.getXRControllersData = function(frame, inputSources, refSpace, xrData) {
548563
xrData.handLeft.enabled = 0;
549564
xrData.handRight.enabled = 0;
@@ -562,30 +577,29 @@
562577
let inputSource = inputSources[i];
563578
// Show the input source if it has a grip space
564579
if (inputSource.hand) {
565-
var hand = 1;
566580
var xrHand = xrData.handLeft;
581+
xrHand.hand = 1;
567582
if (inputSource.handedness == 'right') {
568-
hand = 2;
569583
xrHand = xrData.handRight;
584+
xrHand.hand = 2;
570585
}
571586
xrHand.enabled = 1;
572-
xrHand.hand = hand;
587+
frame.fillPoses(inputSource.hand, refSpace, xrHand.poses);
588+
frame.fillJointRadii(inputSource.hand, xrHand.radii);
573589
for (let j = 0; j < 25; j++) {
574-
let joint = null;
575-
if (inputSource.hand[j] !== null) {
576-
joint = frame.getJointPose(inputSource.hand[j], refSpace);
577-
}
578-
if (joint !== null) {
590+
xrHand.jointIndex = j*16;
591+
if (!isNaN(xrHand.poses[xrHand.jointIndex])) {
579592
xrHand.joints[j].enabled = 1;
580-
xrHand.joints[j].position[0] = joint.transform.position.x;
581-
xrHand.joints[j].position[1] = joint.transform.position.y;
582-
xrHand.joints[j].position[2] = -joint.transform.position.z;
583-
xrHand.joints[j].rotation[0] = -joint.transform.orientation.x;
584-
xrHand.joints[j].rotation[1] = -joint.transform.orientation.y;
585-
xrHand.joints[j].rotation[2] = joint.transform.orientation.z;
586-
xrHand.joints[j].rotation[3] = joint.transform.orientation.w;
587-
if (joint.radius !== null) {
588-
xrHand.joints[j].radius = joint.radius;
593+
xrHand.joints[j].position[0] = xrHand.poses[xrHand.jointIndex+12];
594+
xrHand.joints[j].position[1] = xrHand.poses[xrHand.jointIndex+13];
595+
xrHand.joints[j].position[2] = -xrHand.poses[xrHand.jointIndex+14];
596+
this.quaternionFromMatrix(xrHand.jointIndex, xrHand.poses, xrHand.jointQuaternion);
597+
xrHand.joints[j].rotation[0] = -xrHand.jointQuaternion[0];
598+
xrHand.joints[j].rotation[1] = -xrHand.jointQuaternion[1];
599+
xrHand.joints[j].rotation[2] = xrHand.jointQuaternion[2];
600+
xrHand.joints[j].rotation[3] = xrHand.jointQuaternion[3];
601+
if (!isNaN(xrHand.radii[j])) {
602+
xrHand.joints[j].radius = xrHand.radii[j];
589603
}
590604
}
591605
}

Packages/webxr-interactions/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Check if hand joints radius changed on hand update in ControllerInteraction.
810

911
## [0.5.2] - 2020-01-16
1012
### Fixed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ private void OnHandUpdate(WebXRHandData handData)
215215
{
216216
handJoints[i].localPosition = rotationOffset * (handData.joints[i].position - handData.joints[0].position);
217217
handJoints[i].localRotation = rotationOffset * handData.joints[i].rotation;
218+
if (handData.joints[i].radius != handJoints[i].localScale.x && handData.joints[i].radius > 0)
219+
{
220+
handJoints[i].localScale = new Vector3(handData.joints[i].radius, handData.joints[i].radius, handData.joints[i].radius);
221+
}
218222
}
219223
else
220224
{

Packages/webxr/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Changed
9+
- Using `frame.fillPoses` and `frame.fillJointRadii` instead of `frame.getJointPose` for XRHand.
810

911
## [0.5.2] - 2020-01-16
1012
### Added

Packages/webxr/Hidden~/WebGLTemplates/WebXR/webxr.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
for (let i = 0; i < 25; i++) {
8181
this.joints.push(new XRJointData());
8282
}
83+
this.poses = new Float32Array(16 * 25);
84+
this.radii = new Float32Array(25);
85+
this.jointQuaternion = new Float32Array(4);
86+
this.jointIndex = 0;
8387
}
8488

8589
function XRJointData() {
@@ -544,6 +548,17 @@
544548
);
545549
}
546550

551+
// http://answers.unity.com/answers/11372/view.html
552+
XRManager.prototype.quaternionFromMatrix = function(offset, matrix, quaternion) {
553+
quaternion[3] = Math.sqrt( Math.max( 0, 1 + matrix[offset+0] + matrix[offset+5] + matrix[offset+10] ) ) / 2;
554+
quaternion[0] = Math.sqrt( Math.max( 0, 1 + matrix[offset+0] - matrix[offset+5] - matrix[offset+10] ) ) / 2;
555+
quaternion[1] = Math.sqrt( Math.max( 0, 1 - matrix[offset+0] + matrix[offset+5] - matrix[offset+10] ) ) / 2;
556+
quaternion[2] = Math.sqrt( Math.max( 0, 1 - matrix[offset+0] - matrix[offset+5] + matrix[offset+10] ) ) / 2;
557+
quaternion[0] *= Math.sign( quaternion[0] * ( matrix[offset+6] - matrix[offset+9] ) );
558+
quaternion[1] *= Math.sign( quaternion[1] * ( matrix[offset+8] - matrix[offset+2] ) );
559+
quaternion[2] *= Math.sign( quaternion[2] * ( matrix[offset+1] - matrix[offset+4] ) );
560+
}
561+
547562
XRManager.prototype.getXRControllersData = function(frame, inputSources, refSpace, xrData) {
548563
xrData.handLeft.enabled = 0;
549564
xrData.handRight.enabled = 0;
@@ -562,30 +577,29 @@
562577
let inputSource = inputSources[i];
563578
// Show the input source if it has a grip space
564579
if (inputSource.hand) {
565-
var hand = 1;
566580
var xrHand = xrData.handLeft;
581+
xrHand.hand = 1;
567582
if (inputSource.handedness == 'right') {
568-
hand = 2;
569583
xrHand = xrData.handRight;
584+
xrHand.hand = 2;
570585
}
571586
xrHand.enabled = 1;
572-
xrHand.hand = hand;
587+
frame.fillPoses(inputSource.hand, refSpace, xrHand.poses);
588+
frame.fillJointRadii(inputSource.hand, xrHand.radii);
573589
for (let j = 0; j < 25; j++) {
574-
let joint = null;
575-
if (inputSource.hand[j] !== null) {
576-
joint = frame.getJointPose(inputSource.hand[j], refSpace);
577-
}
578-
if (joint !== null) {
590+
xrHand.jointIndex = j*16;
591+
if (!isNaN(xrHand.poses[xrHand.jointIndex])) {
579592
xrHand.joints[j].enabled = 1;
580-
xrHand.joints[j].position[0] = joint.transform.position.x;
581-
xrHand.joints[j].position[1] = joint.transform.position.y;
582-
xrHand.joints[j].position[2] = -joint.transform.position.z;
583-
xrHand.joints[j].rotation[0] = -joint.transform.orientation.x;
584-
xrHand.joints[j].rotation[1] = -joint.transform.orientation.y;
585-
xrHand.joints[j].rotation[2] = joint.transform.orientation.z;
586-
xrHand.joints[j].rotation[3] = joint.transform.orientation.w;
587-
if (joint.radius !== null) {
588-
xrHand.joints[j].radius = joint.radius;
593+
xrHand.joints[j].position[0] = xrHand.poses[xrHand.jointIndex+12];
594+
xrHand.joints[j].position[1] = xrHand.poses[xrHand.jointIndex+13];
595+
xrHand.joints[j].position[2] = -xrHand.poses[xrHand.jointIndex+14];
596+
this.quaternionFromMatrix(xrHand.jointIndex, xrHand.poses, xrHand.jointQuaternion);
597+
xrHand.joints[j].rotation[0] = -xrHand.jointQuaternion[0];
598+
xrHand.joints[j].rotation[1] = -xrHand.jointQuaternion[1];
599+
xrHand.joints[j].rotation[2] = xrHand.jointQuaternion[2];
600+
xrHand.joints[j].rotation[3] = xrHand.jointQuaternion[3];
601+
if (!isNaN(xrHand.radii[j])) {
602+
xrHand.joints[j].radius = xrHand.radii[j];
589603
}
590604
}
591605
}

Packages/webxr/Hidden~/WebGLTemplates/WebXR2020/webxr.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
for (let i = 0; i < 25; i++) {
8181
this.joints.push(new XRJointData());
8282
}
83+
this.poses = new Float32Array(16 * 25);
84+
this.radii = new Float32Array(25);
85+
this.jointQuaternion = new Float32Array(4);
86+
this.jointIndex = 0;
8387
}
8488

8589
function XRJointData() {
@@ -544,6 +548,17 @@
544548
);
545549
}
546550

551+
// http://answers.unity.com/answers/11372/view.html
552+
XRManager.prototype.quaternionFromMatrix = function(offset, matrix, quaternion) {
553+
quaternion[3] = Math.sqrt( Math.max( 0, 1 + matrix[offset+0] + matrix[offset+5] + matrix[offset+10] ) ) / 2;
554+
quaternion[0] = Math.sqrt( Math.max( 0, 1 + matrix[offset+0] - matrix[offset+5] - matrix[offset+10] ) ) / 2;
555+
quaternion[1] = Math.sqrt( Math.max( 0, 1 - matrix[offset+0] + matrix[offset+5] - matrix[offset+10] ) ) / 2;
556+
quaternion[2] = Math.sqrt( Math.max( 0, 1 - matrix[offset+0] - matrix[offset+5] + matrix[offset+10] ) ) / 2;
557+
quaternion[0] *= Math.sign( quaternion[0] * ( matrix[offset+6] - matrix[offset+9] ) );
558+
quaternion[1] *= Math.sign( quaternion[1] * ( matrix[offset+8] - matrix[offset+2] ) );
559+
quaternion[2] *= Math.sign( quaternion[2] * ( matrix[offset+1] - matrix[offset+4] ) );
560+
}
561+
547562
XRManager.prototype.getXRControllersData = function(frame, inputSources, refSpace, xrData) {
548563
xrData.handLeft.enabled = 0;
549564
xrData.handRight.enabled = 0;
@@ -562,30 +577,29 @@
562577
let inputSource = inputSources[i];
563578
// Show the input source if it has a grip space
564579
if (inputSource.hand) {
565-
var hand = 1;
566580
var xrHand = xrData.handLeft;
581+
xrHand.hand = 1;
567582
if (inputSource.handedness == 'right') {
568-
hand = 2;
569583
xrHand = xrData.handRight;
584+
xrHand.hand = 2;
570585
}
571586
xrHand.enabled = 1;
572-
xrHand.hand = hand;
587+
frame.fillPoses(inputSource.hand, refSpace, xrHand.poses);
588+
frame.fillJointRadii(inputSource.hand, xrHand.radii);
573589
for (let j = 0; j < 25; j++) {
574-
let joint = null;
575-
if (inputSource.hand[j] !== null) {
576-
joint = frame.getJointPose(inputSource.hand[j], refSpace);
577-
}
578-
if (joint !== null) {
590+
xrHand.jointIndex = j*16;
591+
if (!isNaN(xrHand.poses[xrHand.jointIndex])) {
579592
xrHand.joints[j].enabled = 1;
580-
xrHand.joints[j].position[0] = joint.transform.position.x;
581-
xrHand.joints[j].position[1] = joint.transform.position.y;
582-
xrHand.joints[j].position[2] = -joint.transform.position.z;
583-
xrHand.joints[j].rotation[0] = -joint.transform.orientation.x;
584-
xrHand.joints[j].rotation[1] = -joint.transform.orientation.y;
585-
xrHand.joints[j].rotation[2] = joint.transform.orientation.z;
586-
xrHand.joints[j].rotation[3] = joint.transform.orientation.w;
587-
if (joint.radius !== null) {
588-
xrHand.joints[j].radius = joint.radius;
593+
xrHand.joints[j].position[0] = xrHand.poses[xrHand.jointIndex+12];
594+
xrHand.joints[j].position[1] = xrHand.poses[xrHand.jointIndex+13];
595+
xrHand.joints[j].position[2] = -xrHand.poses[xrHand.jointIndex+14];
596+
this.quaternionFromMatrix(xrHand.jointIndex, xrHand.poses, xrHand.jointQuaternion);
597+
xrHand.joints[j].rotation[0] = -xrHand.jointQuaternion[0];
598+
xrHand.joints[j].rotation[1] = -xrHand.jointQuaternion[1];
599+
xrHand.joints[j].rotation[2] = xrHand.jointQuaternion[2];
600+
xrHand.joints[j].rotation[3] = xrHand.jointQuaternion[3];
601+
if (!isNaN(xrHand.radii[j])) {
602+
xrHand.joints[j].radius = xrHand.radii[j];
589603
}
590604
}
591605
}

Packages/webxr/Hidden~/WebGLTemplates/WebXRFullView/webxr.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
for (let i = 0; i < 25; i++) {
8181
this.joints.push(new XRJointData());
8282
}
83+
this.poses = new Float32Array(16 * 25);
84+
this.radii = new Float32Array(25);
85+
this.jointQuaternion = new Float32Array(4);
86+
this.jointIndex = 0;
8387
}
8488

8589
function XRJointData() {
@@ -544,6 +548,17 @@
544548
);
545549
}
546550

551+
// http://answers.unity.com/answers/11372/view.html
552+
XRManager.prototype.quaternionFromMatrix = function(offset, matrix, quaternion) {
553+
quaternion[3] = Math.sqrt( Math.max( 0, 1 + matrix[offset+0] + matrix[offset+5] + matrix[offset+10] ) ) / 2;
554+
quaternion[0] = Math.sqrt( Math.max( 0, 1 + matrix[offset+0] - matrix[offset+5] - matrix[offset+10] ) ) / 2;
555+
quaternion[1] = Math.sqrt( Math.max( 0, 1 - matrix[offset+0] + matrix[offset+5] - matrix[offset+10] ) ) / 2;
556+
quaternion[2] = Math.sqrt( Math.max( 0, 1 - matrix[offset+0] - matrix[offset+5] + matrix[offset+10] ) ) / 2;
557+
quaternion[0] *= Math.sign( quaternion[0] * ( matrix[offset+6] - matrix[offset+9] ) );
558+
quaternion[1] *= Math.sign( quaternion[1] * ( matrix[offset+8] - matrix[offset+2] ) );
559+
quaternion[2] *= Math.sign( quaternion[2] * ( matrix[offset+1] - matrix[offset+4] ) );
560+
}
561+
547562
XRManager.prototype.getXRControllersData = function(frame, inputSources, refSpace, xrData) {
548563
xrData.handLeft.enabled = 0;
549564
xrData.handRight.enabled = 0;
@@ -562,30 +577,29 @@
562577
let inputSource = inputSources[i];
563578
// Show the input source if it has a grip space
564579
if (inputSource.hand) {
565-
var hand = 1;
566580
var xrHand = xrData.handLeft;
581+
xrHand.hand = 1;
567582
if (inputSource.handedness == 'right') {
568-
hand = 2;
569583
xrHand = xrData.handRight;
584+
xrHand.hand = 2;
570585
}
571586
xrHand.enabled = 1;
572-
xrHand.hand = hand;
587+
frame.fillPoses(inputSource.hand, refSpace, xrHand.poses);
588+
frame.fillJointRadii(inputSource.hand, xrHand.radii);
573589
for (let j = 0; j < 25; j++) {
574-
let joint = null;
575-
if (inputSource.hand[j] !== null) {
576-
joint = frame.getJointPose(inputSource.hand[j], refSpace);
577-
}
578-
if (joint !== null) {
590+
xrHand.jointIndex = j*16;
591+
if (!isNaN(xrHand.poses[xrHand.jointIndex])) {
579592
xrHand.joints[j].enabled = 1;
580-
xrHand.joints[j].position[0] = joint.transform.position.x;
581-
xrHand.joints[j].position[1] = joint.transform.position.y;
582-
xrHand.joints[j].position[2] = -joint.transform.position.z;
583-
xrHand.joints[j].rotation[0] = -joint.transform.orientation.x;
584-
xrHand.joints[j].rotation[1] = -joint.transform.orientation.y;
585-
xrHand.joints[j].rotation[2] = joint.transform.orientation.z;
586-
xrHand.joints[j].rotation[3] = joint.transform.orientation.w;
587-
if (joint.radius !== null) {
588-
xrHand.joints[j].radius = joint.radius;
593+
xrHand.joints[j].position[0] = xrHand.poses[xrHand.jointIndex+12];
594+
xrHand.joints[j].position[1] = xrHand.poses[xrHand.jointIndex+13];
595+
xrHand.joints[j].position[2] = -xrHand.poses[xrHand.jointIndex+14];
596+
this.quaternionFromMatrix(xrHand.jointIndex, xrHand.poses, xrHand.jointQuaternion);
597+
xrHand.joints[j].rotation[0] = -xrHand.jointQuaternion[0];
598+
xrHand.joints[j].rotation[1] = -xrHand.jointQuaternion[1];
599+
xrHand.joints[j].rotation[2] = xrHand.jointQuaternion[2];
600+
xrHand.joints[j].rotation[3] = xrHand.jointQuaternion[3];
601+
if (!isNaN(xrHand.radii[j])) {
602+
xrHand.joints[j].radius = xrHand.radii[j];
589603
}
590604
}
591605
}

0 commit comments

Comments
 (0)