forked from exokitxr/exokit-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.html
140 lines (119 loc) · 4.04 KB
/
example.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
<html>
<body>
<script src="three.js"></script>
<script>
let container, camera, scene, renderer, cubeMesh, iframe, fakeXrDisplay, session;
const layers = [];
async function _startFakeVrDisplay() {
fakeXrDisplay = new FakeXRDisplay();
fakeXrDisplay.width = window.innerWidth*2;
fakeXrDisplay.height = window.innerHeight;
fakeXrDisplay.position.copy(camera.position);
fakeXrDisplay.pushUpdate();
fakeXrDisplay.enable();
_enterXr();
}
async function _endFakeVrDisplay() {
renderer.vr.enabled = false;
renderer.vr.setAnimationLoop(null);
if (session) {
await session.end();
session = null;
} else if (display) {
await display.exitPresent();
display = null;
}
fakeXrDisplay.disable();
fakeXrDisplay = null;
}
async function _enterXr() {
session = await navigator.xr.requestSession({
exclusive: true,
});
session.layers = layers;
// console.log('request first frame');
session.requestAnimationFrame((timestamp, frame) => {
renderer.vr.setSession(session, {
frameOfReferenceType: 'stage',
});
const pose = frame.getViewerPose();
const viewport = session.baseLayer.getViewport(pose.views[0]);
// const width = viewport.width;
const height = viewport.height;
const fullWidth = (() => {
let result = 0;
for (let i = 0; i < pose.views.length; i++) {
result += session.baseLayer.getViewport(pose.views[i]).width;
}
return result;
})();
renderer.setSize(fullWidth, height);
/* viewportVector.set(fullWidth, height);
renderer.setAnimationLoop(null);
renderer.vr.enabled = true;
renderer.vr.setSession(session);
renderer.vr.setAnimationLoop(animate); */
console.log('loaded root in XR');
});
}
function init() {
container = document.createElement('div');
document.body.appendChild(container);
camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 0.1, 1000);
scene = new THREE.Scene();
// scene.background = new THREE.Color(0x3B3961);
const ambientLight = new THREE.AmbientLight(0x808080);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 4);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
cubeMesh = (() => {
const geometry = new THREE.BoxBufferGeometry(0.1, 0.1, 0.1);
const material = new THREE.MeshPhongMaterial({
color: 0xff0000,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.z = -1;
mesh.rotation.order = 'YXZ';
mesh.frustumCulled = false;
return mesh;
})();
scene.add(cubeMesh);
renderer = new THREE.WebGLRenderer({alpha: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
container.appendChild(renderer.domElement);
layers.push(renderer.domElement);
iframe = document.createElement('iframe');
iframe.d = 3;
iframe.src = 'example2.html';
iframe.onload = () => {
console.log('iframe loaded', iframe.src);
layers.push(iframe);
iframe.onload = null;
setTimeout(() => {
layers.splice(layers.indexOf(iframe), 1);
// iframe.contentWindow.destroy(); // XXX
iframe.src = 'example3.html';
}, 2000);
};
iframe.onerror = err => {
console.warn(err);
};
_startFakeVrDisplay();
}
function animate() {
/* renderer.state.reset();
camera.fov = fov;
camera.aspect = window.innerWidth / window.innerHeight; */
cubeMesh.rotation.x += 0.01;
cubeMesh.rotation.y += 0.01;
renderer.render(scene, camera);
camera.updateProjectionMatrix();
requestAnimationFrame(animate);
}
init();
animate();
</script>
</body>
</html>