-
Notifications
You must be signed in to change notification settings - Fork 119
/
hello_vr.html
144 lines (123 loc) · 4.15 KB
/
hello_vr.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
<html>
<head>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<script src="three.js"></script>
<script src="inflate.min.js"></script>
<script src="FBXLoader.js"></script>
<script>
let model;
function init() {
container = document.createElement('div');
document.body.appendChild(container);
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 1);
camera.lookAt(new THREE.Vector3());
scene = new THREE.Scene();
scene.matrixAutoUpdate = false;
// scene.background = new THREE.Color(0x3B3961);
const ambientLight = new THREE.AmbientLight(0x808080);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 1);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
/* (() => {
const geometry = new THREE.BoxBufferGeometry(0.1, 1, 0.1);
const material = new THREE.MeshPhongMaterial({
color: 0xE91E63,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.y = 0.5;
mesh.frustumCuled = false;
scene.add(mesh);
})();
(() => {
const geometry = new THREE.BoxBufferGeometry(0.1, 0.1, 0.1);
const material = new THREE.MeshPhongMaterial({
color: 0x4CAF50,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.y = 1 + 0.1;
mesh.frustumCulled = false;
scene.add(mesh);
})();
(() => {
const geometry = new THREE.BoxBufferGeometry(0.1, 0.1, 0.1);
const material = new THREE.MeshPhongMaterial({
color: 0x29B6F6,
});
const mesh = new THREE.Mesh(geometry, material);
// mesh.position.y = 0.1;
// mesh.position.z = -0.5;
mesh.frustumCulled = false;
scene.add(mesh);
})(); */
// console.log('load model 1');
const loader = new THREE.FBXLoader();
loader.load('exokit.fbx', object => {
// console.log('load model 2', !!object);
/* object.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(0.5, 0, 1).normalize()
); */
object.scale.multiplyScalar(0.001);
object.matrix.compose(object.position, object.quaternion, object.scale);
object.updateMatrixWorld(true);
// object.frustumCulled = false;
object.traverse(child => {
child.frustumCulled = false;
});
// console.log(object);
model = object;
scene.add(object);
});
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
renderer.setAnimationLoop(animate);
}
function animate() {
if (model) {
const animationTime = 4000;
const f = ((Date.now() % animationTime) / animationTime) * (Math.PI * 2);
model.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(Math.cos(f), 0, Math.sin(f)).normalize()
);
model.updateMatrixWorld();
}
renderer.render(scene, renderer.vr.enabled ? renderer.vr.getCamera(camera) : camera);
}
init();
(async () => {
console.log('request device');
const [display] = await navigator.getVRDisplays();
if (display) {
console.log('request present');
await display.requestPresent([
{
source: renderer.domElement,
}
]);
console.log('entered vr');
const {renderWidth: width, renderHeight: height} = display.getEyeParameters('left');
renderer.setSize(width * 2, height);
renderer.setAnimationLoop(null);
renderer.vr.enabled = true;
renderer.vr.setDevice(display);
renderer.vr.setAnimationLoop(animate);
} else {
console.log('no vr displays');
}
})().catch(err => {
console.warn(err.stack);
});
</script>
</body>
</html>