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