This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
exobot.html
149 lines (121 loc) · 4.1 KB
/
exobot.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
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#008000"/>
<title>exobot</title>
<link rel="manifest" href="exobot_manifest.json">
<script>
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('/exobot_sw.js')
.then(function() {
console.log('Service Worker Registered');
});
}
</script>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<h1>exobot</h1>
<script src="three.js"></script>
<script src="GLTFLoader.js"></script>
<script>
let container, scene, camera, session, model;
function init() {
container = document.createElement('div');
document.body.appendChild(container);
scene = new THREE.Scene();
scene.matrixAutoUpdate = false;
// scene.background = new THREE.Color(0x3B3961);
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
// camera.position.set(0, 1, 0);
camera.lookAt(new THREE.Vector3());
scene.add(camera);
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 loader = new THREE.GLTFLoader(); // .setPath( 'models/' );
loader.load( 'exobot.glb', function ( o ) {
o = o.scene;
// o.position.z = -1;
o.rotation.order = 'YXZ';
o.scale.set(0.2, 0.2, 0.2);
/* o.traverse(e => {
e.castShadow = true;
}); */
/* o.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, -1),
new THREE.Vector3(0, 0, 1)
); */
o.updateMatrixWorld();
// o.frustumCulled = false;
for (let i = 0; i < o.children.length; i++) {
o.children[i].frustumCulled = false;
}
model = o;
scene.add(o);
// scene.add(o.children[0]);
// scene.add(o.children[0]);
}, undefined, function ( e ) {
console.error( e );
} );
}
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// window.browser.magicleap.RequestDepthPopulation(true);
// renderer.autoClear = false;
container.appendChild(renderer.domElement);
renderer.setAnimationLoop(animate);
}
function animate(time, frame) {
if (model) {
const animationTime = 4000;
const f = ((Date.now() % animationTime) / animationTime) * (Math.PI * 2);
model.position.y = Math.sin(f) * 0.05;
model.rotation.y = Math.sin(f*2) * Math.PI*2*0.05;
model.rotation.z = Math.cos(f*2) * Math.PI*2*0.05;
model.updateMatrixWorld();
}
renderer.render(scene, renderer.vr.enabled ? renderer.vr.getCamera(camera) : camera);
}
init();
(async () => {
console.log('request session');
session = await navigator.xr.requestSession({
exclusive: true,
}).catch(err => Promise.resolve(null));
if (session) {
// console.log('request first frame');
session.requestAnimationFrame((timestamp, frame) => {
renderer.vr.setSession(session, {
frameOfReferenceType: 'stage',
});
const {views} = frame.getViewerPose();
const viewport = session.baseLayer.getViewport(views[0]);
const width = viewport.width;
const height = viewport.height;
renderer.setSize(width * 2, height);
renderer.setAnimationLoop(null);
renderer.vr.enabled = true;
renderer.vr.setAnimationLoop(animate);
console.log('running!');
});
} else {
console.log('no xr devices');
}
})();
</script>
</body>
</html>