forked from webaverse/metachromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.html
164 lines (145 loc) · 3.74 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!doctype html>
<html>
<head>
<style>
body {
margin: 0;
}
#canvas {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
/* .buttons {
display: flex;
position: absolute;
right: 30px;
bottom: 30px;
} */
</style>
</head>
<body>
<canvas id=canvas></canvas>
<!-- <div class=buttons>
<input type=button id=enter-vr-button value="Enter XR">
</div> -->
<script type=module>
import * as THREE from 'https://raw.githack.com/mrdoob/three.js/dev/build/three.module.js';
function _createSocket(u) {
try {
return new WebSocket(u);
} catch (err) {
return null;
}
}
{
const match = navigator.userAgent.match(/Mchr\/1\.0\s*\((.+?),\s*(.+?)\)/);
const port = parseInt(match[1], 10);
const key = match[2];
if (port && key) {
const s = _createSocket('ws://' + `localhost:${port}`);
if (s) {
s.onopen = () => {
s.send(JSON.stringify({
method: 'init',
key,
}));
s.onmessage = e => {
const j = JSON.parse(e.data);
const {method} = j;
switch (method) {
case 'qrCode': {
const m = j.data;
if (m.length && m.length > 0) {
const qrText = m[0].text;
const qrPoints = m[0].points;
console.log('got qr', qrText, qrPoints);
}
break;
}
default: {
console.warn('unknown websocket method', method);
break;
}
}
};
};
}
}
}
const canvas = document.getElementById('canvas');
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.sortObjects = false;
renderer.physicallyCorrectLights = true;
renderer.xr.enabled = true;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1, 2);
const ambientLight = new THREE.AmbientLight(0xFFFFFF);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 3);
scene.add(directionalLight);
const directionalLight2 = new THREE.DirectionalLight(0xFFFFFF, 3);
scene.add(directionalLight2);
const cubeMesh = (() => {
const geometry = new THREE.BoxBufferGeometry(0.1, 0.1, 0.1);
const material = new THREE.MeshStandardMaterial({
color: 0x42a5f5,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.frustumCulled = false;
return mesh;
})();
cubeMesh.position.set(0, 1, 0);
cubeMesh.rotation.order = 'YXZ';
scene.add(cubeMesh);
function animate() {
const f = (Date.now()%2000)/2000 * Math.PI*2;
cubeMesh.rotation.x = f;
cubeMesh.rotation.y = f;
cubeMesh.rotation.z = f;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
let currentSession = null;
function onSessionStarted(session) {
session.addEventListener('end', onSessionEnded);
renderer.xr.setSession(session);
currentSession = session;
}
function onSessionEnded() {
currentSession.removeEventListener('end', onSessionEnded);
currentSession = null;
}
/* const enterVrPutton = document.getElementById('enter-vr-button');
enterVrPutton.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
if (currentSession === null) {
navigator.xr.requestSession('immersive-vr', {
optionalFeatures: [
'local-floor',
'bounded-floor',
],
}).then(onSessionStarted);
} else {
currentSession.end();
}
});
enterVrPutton.dispatchEvent(new MouseEvent('click')); */
navigator.xr.requestSession('immersive-vr', {
optionalFeatures: [
'local-floor',
'bounded-floor',
],
}).then(onSessionStarted);
</script>
</body>
</html>