-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.html
415 lines (380 loc) · 12.6 KB
/
app.html
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<!doctype html>
<html>
<body>
<xr-site camera-position="0 2 3" camera-orientation="-0.2297529205473612 0 0 0.9732489894677301">
<xr-iframe src="examples/tutorial.html" position="0 0.5 0" name="Introduction"></xr-iframe>
<xr-iframe src="examples/subscene.html" position="1 1 0" name="Subscene"></xr-iframe>
<xr-iframe src="examples/floor.html" position="-1 0 0" name="Floor"></xr-iframe>
</xr-site>
<!-- optional UI follows -->
<script src="three.js"></script>
<script src="OrbitControls.js"></script>
<script src="TransformControls.js"></script>
<script src="BufferGeometryUtils.js "></script>
<script src="land.js"></script>
<script>
(async () => {
try {
console.log('app 1');
const root = document.querySelector('xr-site');
const guardianMeshes = [];
const baseMeshes = [];
const floorXrIframe = document.querySelector('xr-iframe[name=Floor]');
/* floorXrIframe.addEventListener('load', e => {
console.log('floor load', e);
}); */
const tutorialXrIframe = document.querySelector('xr-iframe[name=Introduction]');
const subsceneXrIframe = document.querySelector('xr-iframe[name=Subscene]');
const _addControls = xrIframe => {
const object = new THREE.Object3D();
object.position.fromArray(xrIframe.position);
scene.add(object);
const control = new THREE.TransformControls(vrCamera, renderer.domElement);
control.attach(object);
control.size = 3;
control.addEventListener('change', e => {
xrIframe.position = object.position.toArray();
xrIframe.orientation = object.quaternion.toArray();
xrIframe.scale = object.scale.toArray();
for (let i = 0; i < xrIframe.baseMeshes.length; i++) {
xrIframe.baseMeshes[i].updateTransform();
}
for (let i = 0; i < xrIframe.guardianMeshes.length; i++) {
xrIframe.guardianMeshes[i].updateTransform();
}
});
control.addEventListener('dragging-changed', e => {
orbitControls.enabled = !e.value;
});
control.addEventListener('mouseEnter', () => {
const xrIframes = Array.from(root.childNodes).filter(xrIframe => xrIframe instanceof XRIFrame);
if (!xrIframes.some(xrIframe => xrIframe.control.draggable)) {
xrIframe.control.draggable = true;
}
});
control.addEventListener('mouseLeave', () => {
xrIframe.control.draggable = false;
});
xrIframe.control = control;
scene.add(control);
};
const _removeControls = xrIframe => {
scene.remove(xrIframe.control);
};
const _getChildXrIframes = xrIframe => {
const contentDocument = xrIframe.contentWindow.iframe.contentDocument;
return Array.from(xrIframe)
.concat(
Array.from(contentDocument.querySelectorAll('xr-iframe'))
);
};
const _insertBefore = (a, b) => {
scene.add(a);
const aIndex = scene.children.indexOf(a);
scene.children.splice(aIndex, 1);
const bIndex = scene.children.indexOf(b);
scene.children.splice(bIndex, 0, a);
};
const colors = {
default: 0x42a5f5,
highlight: 0x9ccc65,
};
const _addGuardian = xrIframe => {
const childXrIframes = _getChildXrIframes(xrIframe);
const xrExtents = childXrIframes.map(xrIframe => {
const extents = xrIframe.getAttribute('extents');
return extents ? XRIFrame.parseExtents(extents) : [];
});
for (let i = 0; i < xrExtents.length; i++) {
const xrExtent = xrExtents[i];
if (xrExtent.length > 0) {
const childXrIframe = childXrIframes[i];
const baseMesh = new THREE.Land(xrExtent, colors.default);
baseMesh.xrIframe = childXrIframe;
baseMesh.updateTransform = function() {
this.position.fromArray(xrIframe.position);
this.quaternion.fromArray(xrIframe.orientation);
this.scale.fromArray(xrIframe.scale);
this.updateColor();
};
baseMesh.updateColor = function() {
this.setColor(this.xrIframe.visible ? colors.highlight : colors.default);
};
baseMesh.updateTransform();
if (guardianMeshes.length > 0) {
_insertBefore(baseMesh, guardianMeshes[0]);
} else {
scene.add(baseMesh);
}
xrIframe.baseMeshes.push(baseMesh);
baseMeshes.push(baseMesh);
const guardianMesh = new THREE.Guardian(xrExtent, 10, colors.default);
guardianMesh.xrIframe = childXrIframe;
guardianMesh.updateTransform = function() {
this.position.fromArray(xrIframe.position);
this.quaternion.fromArray(xrIframe.orientation);
this.scale.fromArray(xrIframe.scale);
this.updateColor();
};
guardianMesh.updateColor = function() {
this.setColor(this.xrIframe.visible ? colors.highlight : colors.default);
};
guardianMesh.updateTransform();
scene.add(guardianMesh);
xrIframe.guardianMeshes.push(guardianMesh);
guardianMeshes.push(guardianMesh);
}
}
};
const _refreshGuardians = () => {
const xrIframes = Array.from(root.childNodes).filter(xrIframe => xrIframe instanceof XRIFrame);
for (let i = 0; i < xrIframes.length; i++) {
const xrIframe = xrIframes[i];
if (!xrIframe.baseMeshes) {
xrIframe.baseMeshes = [];
xrIframe.guardianMeshes = [];
if (xrIframe.loaded) {
_addGuardian(xrIframe);
} else {
xrIframe.addEventListener('load', () => {
_addGuardian(xrIframe);
}, {
once: true,
});
}
}
}
for (let i = 0; i < baseMeshes.length; i++) {
baseMeshes[i].updateTransform();
}
for (let i = 0; i < guardianMeshes.length; i++) {
guardianMeshes[i].updateTransform();
}
};
const _postTabs = () => {
window.parentPostMessage({
method: 'tabs',
tabs: Array.from(root.childNodes)
.filter(childNode => childNode instanceof XRIFrame)
.map(childNode => childNode.name),
});
};
new MutationObserver(async mutations => {
for (let i = 0; i < mutations.length; i++) {
const {addedNodes, removedNodes} = mutations[i];
for (let j = 0; j < removedNodes.length; j++) {
const removedNode = removedNodes[j];
if (removedNode instanceof XRIFrame) {
_removeControls(removedNode);
}
}
for (let j = 0; j < addedNodes.length; j++) {
const addedNode = addedNodes[j];
if (addedNode instanceof XRIFrame) {
_addControls(addedNode);
}
}
}
}).observe(root, {
childList: true,
});
const vrCamera = new THREE.PerspectiveCamera();
vrCamera.updateMatrixWorld = () => {};
let orbitControls = null;
// main
const scene = new THREE.Scene();
// scene.fog = new THREE.Fog(new THREE.Color(0x7e57c2).offsetHSL(0, 0, -0.45), 1, 100);
// scene.background = new THREE.Color(0xFFFFFF);
scene.matrixAutoUpdate = false;
const camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.fromArray(root.cameraPosition);
camera.quaternion.fromArray(root.cameraOrientation);
scene.add(camera);
const renderer = new THREE.WebGLRenderer({
antialias: true,
});
// console.log('set size', window.innerWidth, window.innerHeight);
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.sortObjects = false;
document.body.appendChild(renderer.domElement);
root.layers.push(renderer.domElement);
_addControls(floorXrIframe);
_addControls(tutorialXrIframe);
_addControls(subsceneXrIframe);
_refreshGuardians();
_postTabs();
function animate() {
const _updateOrbitControls = () => {
if (orbitControls) {
orbitControls.update();
root.cameraPosition = orbitControls.object.position.toArray();
root.cameraOrientation = orbitControls.object.quaternion.toArray();
}
};
_updateOrbitControls();
const _updateVrCamera = () => {
vrCamera.matrixWorldInverse.fromArray(root.viewMatrix);
vrCamera.matrixWorld.getInverse(vrCamera.matrixWorldInverse);
vrCamera.projectionMatrix.fromArray(root.projectionMatrix);
vrCamera.projectionMatrixInverse.getInverse(vrCamera.projectionMatrix);
vrCamera.matrixWorld.decompose(vrCamera.position, vrCamera.quaternion, vrCamera.scale);
};
_updateVrCamera();
const _updateGuardians = () => {
for (let i = 0; i < baseMeshes.length; i++) {
baseMeshes[i].updateColor();
}
for (let i = 0; i < guardianMeshes.length; i++) {
guardianMeshes[i].updateColor();
}
};
_updateGuardians();
renderer.render(scene, camera);
}
const session = await root.requestSession();
renderer.vr.setSession(session, {
frameOfReferenceType: 'stage',
});
// console.log('set size', root.width, root.height);
renderer.setSize(root.width, root.height);
renderer.setPixelRatio(1);
renderer.setAnimationLoop(null);
renderer.vr.enabled = true;
// renderer.vr.setSession(session);
renderer.vr.setAnimationLoop(animate);
const orbitCamera = camera.clone();
orbitControls = new THREE.OrbitControls(orbitCamera, renderer.domElement);
orbitControls.screenSpacePanning = true;
orbitControls.enableKeys = false;
orbitControls.update();
const _cleanupRoot = () => {
while (root.firstChild && root.firstChild.nodeType === Node.TEXT_NODE) {
root.removeChild(root.firstChild);
}
for (let el = root.firstChild; el; el = el.nextSibling) {
while (el.nextSibling && el.nextSibling.nodeType === Node.TEXT_NODE) {
el.parentNode.removeChild(el.nextSibling);
}
}
const xrIframes = Array.from(root.childNodes);
for (let i = 0; i < xrIframes.length; i++) {
root.insertBefore(new Text('\n '), xrIframes[i]);
}
root.appendChild(new Text('\n'));
};
window.addEventListener('message', e => {
const {data} = e;
const {method} = data;
switch (method) {
case 'load': {
const xrIframe = document.createElement('xr-iframe');
xrIframe.src = data.url;
xrIframe.name = data.name;
xrIframe.position = data.origin ? [
0,
0.5,
0,
] : [
-1 + Math.random() * 2,
0.1,
-1 + Math.random() * 2,
];
const xrIframes = Array.from(root.childNodes).filter(childNode => childNode instanceof XRIFrame);
if (xrIframes.length > 0 && xrIframes[xrIframes.length - 1] === floorXrIframe) {
root.insertBefore(xrIframe, floorXrIframe);
} else {
root.appendChild(xrIframe);
}
_cleanupRoot();
_refreshGuardians();
_postTabs();
break;
}
case 'unload': {
const {index} = data;
const xrIframe = Array.from(root.childNodes).filter(childNode => childNode instanceof XRIFrame)[index];
if (xrIframe.previousSibling && xrIframe.previousSibling.nodeType === Node.TEXT_NODE) {
xrIframe.parentNode.removeChild(xrIframe.previousSibling);
}
xrIframe.parentNode.removeChild(xrIframe);
for (let i = 0; i < xrIframe.baseMeshes.length; i++) {
scene.remove(xrIframe.baseMeshes[i]);
}
for (let i = 0; i < xrIframe.guardianMeshes.length; i++) {
scene.remove(xrIframe.guardianMeshes[i]);
}
_cleanupRoot();
_refreshGuardians();
_postTabs();
break;
}
case 'getCode': {
window.parentPostMessage({
method: 'code',
code: root.outerHTML
.replace(
/(<xr-iframe src=")(examples\/)/g,
`$1${window.location.origin}/$2`
),
});
break;
}
case 'setCode': {
const {code} = data;
const match = code.match(/^\s*<xr-site[^>]*>([\s\S]*)<\/xr-site>\s*$/mi);
if (match) {
root.innerHTML = /\S/.test(match[1]) ? match[1] : '';
}
_refreshGuardians();
_postTabs();
break;
}
case 'setMode': {
const {mode} = data;
const xrIframes = Array.from(root.childNodes);
for (let i = 0; i < xrIframes.length; i++) {
const xrIframe = xrIframes[i];
if (xrIframe instanceof XRIFrame) {
xrIframe.control.setMode(mode);
}
}
break;
}
case 'setScale': {
const {scale} = data;
const xrIframes = Array.from(root.childNodes);
const dragging = xrIframes.some(xrIframe => xrIframe instanceof XRIFrame && xrIframe.control.dragging);
if (!dragging) {
for (let i = 0; i < xrIframes.length; i++) {
const xrIframe = xrIframes[i];
if (xrIframe instanceof XRIFrame) {
xrIframe.control.rotationScale = scale;
xrIframe.control.scaleScale = scale;
}
}
}
break;
}
case 'mouseenter': {
const {index} = data;
const xrIframe = Array.from(root.childNodes).filter(childNode => childNode instanceof XRIFrame)[index];
xrIframe.highlight = [0.5, 0, 0, 0.7];
break;
}
case 'mouseleave': {
const {index} = data;
const xrIframe = Array.from(root.childNodes).filter(childNode => childNode instanceof XRIFrame)[index];
xrIframe.highlight = null;
break;
}
}
});
console.log('app 2');
} catch(err) {
console.warn(err);
}
})();
</script>
</body>
</html>