forked from toji/xr-dinosaurs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
417 lines (369 loc) · 15.7 KB
/
index.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
416
417
<!doctype html>
<!--
Copyright 2019 Brandon Jones
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'>
<meta name='mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<link rel="manifest" href="manifest.json">
<link rel='icon' type='image/png' sizes='32x32' href='media/textures/dino-icon-32.png'>
<link rel='icon' type='image/png' sizes='96x96' href='media/textures/dino-icon-96.png'>
<link rel='stylesheet' href='css/dinosaurs.css'>
<title>WebXR Dinosaurs</title>
</head>
<body>
<script src="js/third-party/three.js/examples/js/vr/HelioWebXRPolyfill.js"></script>
<script src='js/third-party/dat.gui.min.js'></script>
<article id='landingPage' class='not-hidden'>
<header>
<h1>XR Dinosaurs</h1>
<section>
<h2>Welcome to the web's virtual Dinosaur Park!</h2>
<p>We've used the magic of your browser to bring back a friendly pack of prehistoric pals.</p>
<p>Our dinosaurs can be viewed with a variety of Virtual Reality headsets, Augmented Reality
headsets and phones, or directly in your browser.</p>
</section>
</header>
<main class='loading'>
<div class='lds-ellipsis'>
<div></div><div></div><div></div><div></div>
</div>
<ul class='menu main-menu'>
<li id='view2DButton'>
See the Dinosaurs in your Browser.
</li>
<li class='disabled' id='viewVRButton'>
Visit our Dinosaurs in Virtual Reality.
</li>
<li class='disabled' id='viewARButton'>
Bring a Dinosaur home with Augmented Reality.
</li>
</ul>
</main>
<footer>
<div class='webxr-experience'>
XR Dinosaurs is a<br/>
<a href='https://immersiveweb.dev/'>WebXR Experience</a>
</div>
<div class='credits'>
<a class='source-link' href='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/toji/xr-dinosaurs'>
Get the source on GitHub
</a>
Brought to you with the help of:
<ul>
<li><a href='https://threejs.org/'>Three.js</a></li>
<li><a href='https://www.khronos.org/gltf/'>glTF</a></li>
<li><a href='https://google.github.io/draco/'>Draco</a></li>
<li><a href='https://workshop.chromeexperiments.com/examples/gui/'>dat.gui</a></li>
</ul>
</div>
<h3>
<!--a href="intent:https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Avocado/glTF/Avocado.gltf#Intent;scheme=https;package=com.google.android.googlequicksearchbox;action=android.intent.action.VIEW;S.browser_fallback_url=https://developers.google.com/ar;end;">Avocado</a-->
<a href="intent:https://www.xrdinosaurs.com/media/models/ankylosaurus/compressed.glb#Intent;scheme=https;package=com.google.android.googlequicksearchbox;action=android.intent.action.VIEW;S.browser_fallback_url=https://developers.google.com/ar;end;">Rawr!</a>
</h3>
</footer>
</article>
<article id='dinosaurSelection' class='hidden'>
<div class='button-bar'>
<button id='closeSelection' alt='Close Dinosaur Selection'>Close</button>
<select id='arScale' alt='The size of the dinosaur in AR'>
<option value='1'>1:1 - Full Size</option>
<option value='0.25' selected>1:4 - Quarter Size</option>
<option value='0.0625'>1:16 - Toy Size</option>
</select>
</div>
<ul id='dinosaurList' class='menu'>
<li data-dinosaur='ankylosaurus'>
Anklyosaurus
<p>She may be covered in spikes, but our Ankylosaur is actually quite cuddly
and loves to meet new friends! Think of her as a big, prehistoric puppy.</p>
</li>
<li data-dinosaur='brachiosaurus'>
Brachiosaurus
<p>Our biggest dinosaur by far, she weighs as much as 12 elephants! But don't
worry, she's very friendly and we've taught her to be <i>extremely</i> careful about
where she's stepping!</p>
</li>
<li data-dinosaur='pachycephalosaurus'>
Pachycephalosaurus
<p>This sweet "Pachy" is one of our older dinosaurs. She's a bit shy and
gets nervous around new people, but is incredibly gentle.</p>
</li>
<li data-dinosaur='parasaurolophus'>
Parasaurolophus
<p>Our Parasaurolophus is a mischevious, silly dinosaur. The long crest on
her head acts like a trumpet, which she loves using to surprise park guests.</p>
</li>
<li data-dinosaur='raptor'>
Utahraptor
<p>You've probably heard these smart dinosaurs called "Velociraptors", but
those were actually quite small. Our Utahraptor is much closer in size to
the dinosaurs you know and love from the big screen.</p>
</li>
<li data-dinosaur='stegosaurus'>
Stegosaurus
<p>Strong and proud, the Stegosaurus is one of our more popular dinosaurs.
Her favorite thing is getting her plates scrubbed clean so they stay such
a beautiful, shiny red.</p>
</li>
<li data-dinosaur='triceratops'>
Triceratops
<p>Our youngest dinosaur, this juvenile Triceratops is a smaller than a
full grown adult would be. Spunky and playful, she loves being the center
of attention.</p>
</li>
<li data-dinosaur='trex'>
Tyrannosaurus Rex
<p>Don't let the big teeth scare you, our T-Rex is really a big softie that
loves showing off. Her hobbies include making stompy noises with her feet
and going for runs around her pen.</p>
</li>
</ul>
</article>
<article id='canvasContainer' class='hidden loading'>
<div class='button-bar'>
<button id='showSelection' alt='Select a different Dinosaur'>Back</button>
<button id='showDinosaurVR' alt='View Dinosaur in VR' class='hidden'>Enter VR</button>
<button id='showDinosaurAR' alt='View Dinosaur in AR' class='hidden'>Enter AR</button>
</div>
<div class='lds-ellipsis'>
<div></div><div></div><div></div><div></div>
</div>
<!-- Main WebGL content, injected by dinosaur-app.js -->
</article>
<script type="module">
import WebXRPolyfill from './js/third-party/webxr-polyfill/build/webxr-polyfill.module.js';
import { QueryArgs } from './js/query-args.js';
import { PreloadDinosaurApp, RunDinosaurApp, SetStateChangeCallback, EndXRSession } from './js/dinosaur-app.js';
const SCREEN = {
Home: 0,
Select: 1,
View: 2
};
const VIEW_MODE = {
Flat: 0,
VR: 1,
AR: 2
};
let appState = {
screen: SCREEN.Home,
viewMode: VIEW_MODE.Flat,
dinosaur: null,
debug: QueryArgs.getBool('debug'),
inXR: false
};
// Initialized the polyfill unless specifically requested not to.
let usePolyfill = QueryArgs.getBool('polyfill', true);
if (usePolyfill) {
let polyfill = new WebXRPolyfill();
}
let landingPage = document.getElementById('landingPage');
let mainElement = document.querySelector('main');
let selectionElement = document.getElementById('dinosaurSelection');
let containerElement = document.getElementById('canvasContainer');
let closeSelectionButton = document.getElementById('closeSelection');
let showSelectionButton = document.getElementById('showSelection');
let showDinosaurVRButton = document.getElementById('showDinosaurVR');
let showDinosaurARButton = document.getElementById('showDinosaurAR');
let view2DButton = document.getElementById('view2DButton');
let viewVRButton = document.getElementById('viewVRButton');
let viewARButton = document.getElementById('viewARButton');
let arScaleSelect = document.getElementById('arScale');
let vrSupported = false;
let arSupported = false;
function updateAppState(pushState = true) {
if (pushState) {
history.pushState(appState, null, null);
}
switch (appState.screen) {
case SCREEN.Home:
landingPage.classList.remove('hidden');
containerElement.classList.add('hidden');
selectionElement.classList.add('hidden');
break;
case SCREEN.Select:
selectionElement.classList.remove('hidden');
if (appState.viewMode == VIEW_MODE.AR) {
selectionElement.classList.add('ar');
} else {
selectionElement.classList.remove('ar');
}
break;
case SCREEN.View:
landingPage.classList.add('hidden');
selectionElement.classList.add('hidden');
containerElement.classList.remove('hidden');
if (appState.viewMode == VIEW_MODE.Flat) {
setTimeout(() => {
RunDinosaurApp(containerElement, { dinosaur: appState.dinosaur });
}, 100);
} else {
let immersiveMode = null;
if (appState.viewMode == VIEW_MODE.VR) {
immersiveMode = 'immersive-vr';
} else if (appState.viewMode == VIEW_MODE.AR) {
immersiveMode = 'immersive-ar';
}
RunDinosaurApp(containerElement, {
xrSessionMode: immersiveMode,
dinosaur: appState.dinosaur,
arScale: parseFloat(arScaleSelect.value)
});
}
break;
}
}
SetStateChangeCallback((state) => {
if (state.dinosaur) {
appState.dinosaur = state.dinosaur;
history.replaceState(appState, null, null);
}
if (state.xrSessionStarted) {
appState.inXR = true;
document.body.classList.add('xr');
document.body.classList.add(appState.viewMode == VIEW_MODE.AR ? 'ar' : 'vr');
}
if (state.xrSessionEnded) {
appState.inXR = false;
document.body.classList.remove('xr');
document.body.classList.remove('ar');
document.body.classList.remove('vr');
if (appState.viewMode == VIEW_MODE.AR) {
appState.screen = SCREEN.Select;
} else if (appState.viewMode == VIEW_MODE.VR) {
appState.viewMode = VIEW_MODE.Flat;
}
updateAppState(false);
history.replaceState(appState, null, null);
}
});
window.addEventListener("popstate", function(e) {
appState = e.state;
updateAppState(false);
});
closeSelectionButton.addEventListener('click', () => {
appState.screen = SCREEN.Home;
updateAppState();
});
showSelectionButton.addEventListener('click', () => {
appState.screen = SCREEN.Select;
updateAppState();
});
view2DButton.addEventListener('click', () => {
appState.screen = SCREEN.Select;
appState.viewMode = VIEW_MODE.Flat;
updateAppState();
});
function startVR() {
if (!vrSupported) return;
if (appState.inXR) {
EndXRSession();
return;
}
appState.screen = SCREEN.View;
appState.viewMode = VIEW_MODE.VR;
updateAppState();
}
viewVRButton.addEventListener('click', startVR);
showDinosaurVRButton.addEventListener('click', startVR);
function startAR() {
if (!arSupported) return;
if (appState.inXR) {
EndXRSession();
return;
}
appState.screen = SCREEN.View;
appState.viewMode = VIEW_MODE.AR;
updateAppState();
}
viewARButton.addEventListener('click', () => {
appState.screen = SCREEN.Select;
appState.viewMode = VIEW_MODE.AR;
updateAppState();
});
showDinosaurARButton.addEventListener('click', startAR);
// Wire up each of the dinosaur selection options
let dinosaurOptions = document.querySelectorAll('#dinosaurList>li');
for (let dinosaur of dinosaurOptions) {
let dinosaurType = dinosaur.getAttribute('data-dinosaur');
dinosaur.addEventListener('click', () => {
appState.screen = SCREEN.View;
appState.dinosaur = dinosaurType;
updateAppState();
});
}
// Check to see if WebXR is available
if ( 'xr' in navigator ) {
if (viewVRButton) {
navigator.xr.isSessionSupported('immersive-vr').then((supported) => {
vrSupported = supported;
if (supported) {
viewVRButton.classList.remove('disabled');
showDinosaurVRButton.classList.remove('hidden');
} else {
let errorMessage = document.createElement('p');
errorMessage.innerText = 'Your browser supports WebXR, but Virtual Reality is not available';
viewVRButton.appendChild(errorMessage);
}
});
}
if (viewARButton) {
if (appState.debug) {
viewARButton.classList.remove('disabled');
showDinosaurARButton.classList.remove('hidden');
}
navigator.xr.isSessionSupported('immersive-ar').then((supported) => {
arSupported = supported;
if (supported) {
viewARButton.classList.remove('disabled');
showDinosaurARButton.classList.remove('hidden');
} else {
let errorMessage = document.createElement('p');
errorMessage.innerText = 'Your browser supports WebXR, but Augmented Reality is not available';
viewARButton.appendChild(errorMessage);
}
});
}
} else {
if (viewVRButton) {
let errorMessage = document.createElement('p');
errorMessage.innerText = 'Your browser does not support WebXR';
viewVRButton.appendChild(errorMessage);
}
if (viewARButton) {
let errorMessage = document.createElement('p');
errorMessage.innerText = 'Your browser does not support WebXR';
viewARButton.appendChild(errorMessage);
}
}
// Begin loading the app
// The timeout is to give the main page a chance to update before we kick
// of a fairly heavy preload process.
setTimeout(() => {
PreloadDinosaurApp(appState.debug).then(() => {
// When the app's primary resources are ready to display, show the
// main menu buttons.
mainElement.classList.remove('loading');
containerElement.classList.remove('loading');
});
}, 100);
</script>
</body>
</html>