Skip to content

Commit 8ec332b

Browse files
author
Avaer Kazmer
committed
Add helio webxr polyfill file
1 parent 558af41 commit 8ec332b

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

src/HelioWebXRPolyfill.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/**
2+
* @author mvilledieu / http://github.com/mvilledieu
3+
*/
4+
5+
if ( /(Helio)/g.test( navigator.userAgent ) && "xr" in navigator ) {
6+
7+
console.log( "Helio WebXR Polyfill (Lumin 0.97.0)" );
8+
9+
const isHelio96 = navigator.userAgent.includes("Chrome/73");
10+
11+
// WebXRManager - XR.supportSession() Polyfill - WebVR.js line 147
12+
13+
if (
14+
"supportsSession" in navigator.xr === false &&
15+
"supportsSessionMode" in navigator.xr
16+
) {
17+
18+
navigator.xr.supportsSession = function ( sessionType ) {
19+
20+
// Force using immersive-ar
21+
return navigator.xr.supportsSessionMode( 'immersive-ar' );
22+
23+
};
24+
25+
}
26+
27+
if ( "requestSession" in navigator.xr ) {
28+
29+
const tempRequestSession = navigator.xr.requestSession.bind( navigator.xr );
30+
31+
navigator.xr.requestSession = function ( sessionType ) {
32+
33+
return new Promise( function ( resolve, reject ) {
34+
35+
const sessionType = (isHelio96 ? {
36+
mode: 'immersive-ar' // Force using immersive-ar
37+
} : 'immersive-ar');
38+
39+
tempRequestSession( sessionType )
40+
.then( function ( session ) {
41+
42+
// WebXRManager - xrFrame.getPose() Polyfill - line 279
43+
44+
const tempRequestAnimationFrame = session.requestAnimationFrame.bind(
45+
session
46+
);
47+
48+
session.requestAnimationFrame = function ( callback ) {
49+
50+
return tempRequestAnimationFrame( function ( time, frame ) {
51+
52+
// WebXRManager - xrFrame.getViewerPose() Polyfill - line 279
53+
// Transforms view.viewMatrix to view.transform.inverse.matrix
54+
55+
const tempGetViewerPose = frame.getViewerPose.bind( frame );
56+
57+
frame.getViewerPose = function ( referenceSpace ) {
58+
59+
const pose = tempGetViewerPose( referenceSpace );
60+
61+
pose.views.forEach( function ( view ) {
62+
63+
view.transform = {
64+
inverse: {
65+
matrix: view.viewMatrix
66+
}
67+
};
68+
69+
} );
70+
71+
return pose;
72+
73+
};
74+
75+
// WebXRManager - xrFrame.getPose() Polyfill - line 259
76+
77+
const tempGetPose = (isHelio96 ? null : frame.getPose.bind( frame ));
78+
79+
frame.getPose = function ( targetRaySpace, referenceSpace ) {
80+
81+
if (isHelio96) {
82+
83+
const inputPose = frame.getInputPose(
84+
targetRaySpace,
85+
referenceSpace
86+
);
87+
88+
inputPose.transform = {
89+
matrix: inputPose.targetRay.transformMatrix
90+
};
91+
92+
return inputPose;
93+
94+
} else {
95+
96+
return tempGetPose(targetRaySpace.gripSpace, referenceSpace);
97+
98+
}
99+
100+
};
101+
102+
callback( time, frame );
103+
104+
} );
105+
106+
};
107+
108+
// WebXRManager - xrFrame.getPose( inputSource.targetRaySpace, referenceSpace) Polyfill - line 279
109+
110+
const tempGetInputSources = session.getInputSources.bind( session );
111+
112+
session.getInputSources = function () {
113+
114+
const res = tempGetInputSources();
115+
116+
res.forEach( function (xrInputSource ) {
117+
118+
Object.defineProperty( xrInputSource, "targetRaySpace", {
119+
get: function () {
120+
121+
return xrInputSource;
122+
123+
}
124+
} );
125+
126+
} );
127+
128+
return res;
129+
130+
};
131+
132+
// WebXRManager - xrSession.getInputSources() Polyfill Line 132 - 136
133+
134+
session.inputSources = Object.defineProperty(
135+
session,
136+
"inputSources",
137+
{
138+
get: session.getInputSources
139+
}
140+
);
141+
142+
// WebXRManager - xrSession.updateRenderState() Polyfill Line 129
143+
144+
if (isHelio96) {
145+
146+
session.updateRenderState = function ( { baseLayer } ) {
147+
148+
session.baseLayer = baseLayer;
149+
150+
// WebXRManager - xrSession.renderState.baseLayer Polyfill Line 219
151+
152+
session.renderState = {
153+
baseLayer: baseLayer
154+
};
155+
156+
};
157+
158+
}
159+
160+
// WebXRManager - xrSession.requestReferenceSpace() Polyfill Line 130
161+
162+
const tempRequestReferenceSpace = session.requestReferenceSpace.bind(
163+
session
164+
);
165+
166+
session.requestReferenceSpace = function () {
167+
168+
return tempRequestReferenceSpace( {
169+
type: "stationary",
170+
subtype: "floor-level"
171+
} );
172+
173+
};
174+
175+
resolve( session );
176+
177+
} )
178+
.catch( function ( error ) {
179+
180+
return reject( error );
181+
182+
} );
183+
184+
} );
185+
186+
};
187+
188+
}
189+
190+
}

0 commit comments

Comments
 (0)