@@ -85,20 +85,20 @@ mergeInto(LibraryManager.library, {
8585 _isArSupported = false ;
8686
8787 if ( ! navigator . xr ) {
88- document . dispatchEvent ( new CustomEvent ( "SimpleWebXRInitialized" , { success : false } ) ) ;
88+ document . dispatchEvent ( new CustomEvent ( "SimpleWebXRInitialized" , { success : false } ) ) ;
8989 return
9090 } ;
9191
9292 // Check if WebXR immersive VR is supported (check immersive-vr before immersive-ar to make it work on Oculus Quest Browser)
9393 navigator . xr . isSessionSupported ( 'immersive-vr' ) . then ( function ( supported ) {
9494 _isVrSupported = supported ;
95- document . dispatchEvent ( new CustomEvent ( "SimpleWebXRSessionSupported" , { xrSessionMode :'immersive-vr' , supported : supported } ) ) ;
95+ document . dispatchEvent ( new CustomEvent ( "SimpleWebXRSessionSupported" , { xrSessionMode : 'immersive-vr' , supported : supported } ) ) ;
9696 } ) ;
9797
9898 // Check if WebXR immersive AR is supported
9999 navigator . xr . isSessionSupported ( 'immersive-ar' ) . then ( function ( supported ) {
100100 _isArSupported = supported ;
101- document . dispatchEvent ( new CustomEvent ( "SimpleWebXRSessionSupported" , { xrSessionMode :'immersive-ar' , supported : supported } ) ) ;
101+ document . dispatchEvent ( new CustomEvent ( "SimpleWebXRSessionSupported" , { xrSessionMode : 'immersive-ar' , supported : supported } ) ) ;
102102 } ) ;
103103
104104 // Initialize pointers to shared arrays that contains data (projection matrix, position, orientation, input sources)
@@ -126,7 +126,7 @@ mergeInto(LibraryManager.library, {
126126 // Share position
127127 var position = view . transform . position ;
128128
129- if ( _firstFrame ) _yOffset = position . y ;
129+ if ( _firstFrame ) _yOffset = position . y ;
130130
131131 _dataArray [ floatStartId + 16 ] = position . x ;
132132 _dataArray [ floatStartId + 17 ] = position . y - _yOffset ;
@@ -220,10 +220,10 @@ mergeInto(LibraryManager.library, {
220220 _byteArray [ byteStartId + 3 ] = 0
221221 }
222222
223- if ( _dataArray [ 103 + id ] > 0 && inputSource . gamepad . hapticActuators && inputSource . gamepad . hapticActuators . length > 0 ) {
223+ if ( _dataArray [ 103 + id ] > 0 && inputSource . gamepad . hapticActuators && inputSource . gamepad . hapticActuators . length > 0 ) {
224224 // Trigger of haptic vibration pulse(intensity [0..1], duration in ms)
225- inputSource . gamepad . hapticActuators [ 0 ] . pulse ( _dataArray [ 101 + id ] , _dataArray [ 103 + id ] ) ;
226- _dataArray [ 103 + id ] = 0 ; // reset flag once it's done
225+ inputSource . gamepad . hapticActuators [ 0 ] . pulse ( _dataArray [ 101 + id ] , _dataArray [ 103 + id ] ) ;
226+ _dataArray [ 103 + id ] = 0 ; // reset flag once it's done
227227 }
228228 }
229229 else {
@@ -253,31 +253,80 @@ mergeInto(LibraryManager.library, {
253253
254254 // Hand detection
255255 // https://immersive-web.github.io/webxr-hand-input/#skeleton-joints-section
256- if ( inputSource . hand && inputSource . hand . length == 25 ) {
257- _byteArray [ 46 + id ] = 1 ; // hand supported
258- var delta = ( _useLocalSpaceForInput ? 0 : _dataArray [ 100 ] )
259- for ( var j = 0 ; j < 25 ; j ++ ) {
260- if ( inputSource . hand [ j ] !== null ) {
261- var joint = frame . getJointPose ( inputSource . hand [ j ] , _useLocalSpaceForInput ? _arSession . localSpace : _arSession . localFloorSpace ) ;
262- if ( joint !== null ) {
263- var i = id * 200 + j * 8 ;
264- _handArray [ i ] = joint . transform . position . x ;
265- _handArray [ i + 1 ] = joint . transform . position . y - delta - _yOffset ;
266- _handArray [ i + 2 ] = joint . transform . position . z ;
267- _handArray [ i + 3 ] = joint . transform . orientation . x ;
268- _handArray [ i + 4 ] = joint . transform . orientation . y ;
269- _handArray [ i + 5 ] = joint . transform . orientation . z ;
270- _handArray [ i + 6 ] = joint . transform . orientation . w ;
271- if ( joint . radius !== null ) {
272- _handArray [ i + 7 ] = joint . radius ;
256+ if ( inputSource . hand ) {
257+
258+ // For browsers that support fillPoses
259+ if ( typeof frame . fillPoses === "function" ) {
260+ _byteArray [ 46 + id ] = 1 ; // hand supported
261+
262+ var delta = ( _useLocalSpaceForInput ? 0 : _dataArray [ 100 ] )
263+
264+ var refSpace = _useLocalSpaceForInput ? _arSession . localSpace : _arSession . localFloorSpace ;
265+
266+ var radii = new Float32Array ( 25 ) ;
267+ var poses = new Float32Array ( 16 * 25 ) ;
268+
269+ if ( inputSource . hand . values ) {
270+ frame . fillPoses ( inputSource . hand . values ( ) , refSpace , poses ) ;
271+ frame . fillJointRadii ( inputSource . hand . values ( ) , radii ) ;
272+ } else {
273+ frame . fillPoses ( inputSource . hand , refSpace , poses ) ;
274+ frame . fillJointRadii ( inputSource . hand , radii ) ;
275+ }
276+
277+ for ( var j = 0 ; j < 25 ; j ++ ) {
278+ var jointIndex = j * 16 ;
279+
280+ var i = id * 200 + j * 8 ;
281+ _handArray [ i ] = poses [ jointIndex + 12 ] ;
282+ _handArray [ i + 1 ] = poses [ jointIndex + 13 ] - delta - _yOffset ;
283+ _handArray [ i + 2 ] = poses [ jointIndex + 14 ] ;
284+
285+
286+ _handArray [ i + 7 ] = radii [ j ] ;
287+
288+ var quaternion = new Float32Array ( 4 ) ;
289+
290+ quaternion [ 3 ] = Math . sqrt ( Math . max ( 0 , 1 + poses [ jointIndex + 0 ] + poses [ jointIndex + 5 ] + poses [ jointIndex + 10 ] ) ) / 2 ;
291+ quaternion [ 0 ] = Math . sqrt ( Math . max ( 0 , 1 + poses [ jointIndex + 0 ] - poses [ jointIndex + 5 ] - poses [ jointIndex + 10 ] ) ) / 2 ;
292+ quaternion [ 1 ] = Math . sqrt ( Math . max ( 0 , 1 - poses [ jointIndex + 0 ] + poses [ jointIndex + 5 ] - poses [ jointIndex + 10 ] ) ) / 2 ;
293+ quaternion [ 2 ] = Math . sqrt ( Math . max ( 0 , 1 - poses [ jointIndex + 0 ] - poses [ jointIndex + 5 ] + poses [ jointIndex + 10 ] ) ) / 2 ;
294+ quaternion [ 0 ] *= Math . sign ( quaternion [ 0 ] * ( poses [ jointIndex + 6 ] - poses [ jointIndex + 9 ] ) ) ;
295+ quaternion [ 1 ] *= Math . sign ( quaternion [ 1 ] * ( poses [ jointIndex + 8 ] - poses [ jointIndex + 2 ] ) ) ;
296+ quaternion [ 2 ] *= Math . sign ( quaternion [ 2 ] * ( poses [ jointIndex + 1 ] - poses [ jointIndex + 4 ] ) ) ;
297+
298+ _handArray [ i + 3 ] = quaternion [ 0 ] ;
299+ _handArray [ i + 4 ] = quaternion [ 1 ] ;
300+ _handArray [ i + 5 ] = quaternion [ 2 ] ;
301+ _handArray [ i + 6 ] = quaternion [ 3 ] ;
302+ }
303+ }
304+ else {
305+ _byteArray [ 46 + id ] = 1 ; // hand supported
306+
307+ for ( var j = 0 ; j < 25 ; j ++ ) {
308+ if ( inputSource . hand [ j ] !== null ) {
309+ var joint = frame . getJointPose ( inputSource . hand [ j ] , refSpace ) ;
310+ if ( joint !== null ) {
311+ var i = id * 200 + j * 8 ;
312+ _handArray [ i ] = joint . transform . position . x ;
313+ _handArray [ i + 1 ] = joint . transform . position . y - delta - _yOffset ;
314+ _handArray [ i + 2 ] = joint . transform . position . z ;
315+ _handArray [ i + 3 ] = joint . transform . orientation . x ;
316+ _handArray [ i + 4 ] = joint . transform . orientation . y ;
317+ _handArray [ i + 5 ] = joint . transform . orientation . z ;
318+ _handArray [ i + 6 ] = joint . transform . orientation . w ;
319+ if ( joint . radius !== null ) {
320+ _handArray [ i + 7 ] = joint . radius ;
321+ }
322+ else {
323+ _handArray [ i + 7 ] = NaN ;
324+ }
273325 }
274326 else {
275- _handArray [ i + 7 ] = NaN ;
327+ _byteArray [ 46 + id ] = 0 ; // hand not fully supported
276328 }
277329 }
278- else {
279- _byteArray [ 46 + id ] = 0 ; // hand not fully supported
280- }
281330 }
282331 }
283332 }
@@ -434,7 +483,7 @@ mergeInto(LibraryManager.library, {
434483 }
435484 } ( GLctx . bindFramebuffer ) ;
436485
437- document . dispatchEvent ( new CustomEvent ( "SimpleWebXRInitialized" , { success : true } ) ) ;
486+ document . dispatchEvent ( new CustomEvent ( "SimpleWebXRInitialized" , { success : true } ) ) ;
438487 } ,
439488
440489 /****************************************************************************/
@@ -468,7 +517,7 @@ mergeInto(LibraryManager.library, {
468517 GLctx . ARSessionStarted = _isArSupported ;
469518 session . isInSession = true ; // add field in session to indicate that a session in running
470519
471- document . dispatchEvent ( new CustomEvent ( "SimpleWebXRSessionStarted" , { session : session , GLctx : GLctx } ) ) ;
520+ document . dispatchEvent ( new CustomEvent ( "SimpleWebXRSessionStarted" , { session : session , GLctx : GLctx } ) ) ;
472521
473522 var glLayer = new XRWebGLLayer ( session , GLctx ) ;
474523
@@ -531,7 +580,7 @@ mergeInto(LibraryManager.library, {
531580 _orientationInfo [ 0 ] = 0 ;
532581
533582 _onDeviceOrientation = function ( event ) {
534- if ( _orientationInfo [ 0 ] == 0 ) document . dispatchEvent ( new CustomEvent ( "SimpleWebXRDeviceOrientationStarted" ) ) ;
583+ if ( _orientationInfo [ 0 ] == 0 ) document . dispatchEvent ( new CustomEvent ( "SimpleWebXRDeviceOrientationStarted" ) ) ;
535584
536585 _orientationInfo [ 0 ] = 1 ;
537586 _orientationArray [ 0 ] = event . alpha ;
0 commit comments