-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
520 lines (449 loc) · 16.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
<meta name="mobile-web-app-capable" content="yes">
<title>AR Engagement Tool - GPI Techlab</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<link rel="stylesheet" href="./css/bulma.css" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="./js/canvas2image.js"></script>
<script src='./js/three.min.js'></script>
<script src="./js/shaders/SSAOShader.js"></script>
<script src="./js/shaders/CopyShader.js"></script>
<script src="./js/postprocessing/EffectComposer.js"></script>
<script src="./js/postprocessing/RenderPass.js"></script>
<script src="./js/postprocessing/ShaderPass.js"></script>
<script src="./js/postprocessing/MaskPass.js"></script>
<script src="./js/postprocessing/SSAOPass.js"></script>
<script src="./js/OBJLoader.js"></script>
<script src="./js/MTLLoader.js"></script>
<script src="./js/ColladaLoader.js"></script>
<script src="./js/ar.js"></script>
<script src="https://unpkg.com/merge-images"></script>
<script src="./js/download.js"></script>
<!-- custom javascript tools -->
<script src="./js/tools.js"></script>
<!-- asset related json objects as variables in these scripts -->
<!-- editorial note: I hate JavaScript -->
<script src="./js/islands.js"></script>
<script src="./js/trees.js"></script>
<script>
THREEx.ArToolkitContext.baseURL = './'
</script>
<!-- external CSS style sheet -->
<link rel="stylesheet" type="text/css" href="style.css" media="screen">
</head>
<body>
<div id="landing">
<div class="hero is-large has-bg-img">
<div class="hero-body has-bg-img">
<div class="container has-text-centered">
<div class="column is-6 is-offset-3">
<h1 id="formtitle" class="title has-text-dark">TechLab AR Project</h1>
<br>
<h2 class="subtitle has-text-white">
<b id="description">Estimate your personal carbon footprint and see what plausible impact it has on floating islands, in terms of deforestation and plastic and oil consumption.<br><br></b>
<!-- button to begin calculation of footprint -->
<a class="button is-primary has-text-dark" id="calculationBox" onclick="Begin()">
<b> Calculate your footprint </b>
</a>
</div>
<!-- Question One - Carbon -->
<div class="column is-8 is-offset-2">
<div class="box" id="boxyBox">
<div class="field is-grouped" id="questionOne">
<p class="control is-expanded">
<a class="control has-text-dark" type="text"><b>How many hours do you fly per year?</b>
</p>
<div class="columns">
<p class="control column">
<a class="button is-success" onclick="setRange('oil', 1, 'questionOne', 'questionTwo')">
0-10 hours
</a>
</p>
<p class="control column">
<a class="button is-warning" onclick="setRange('oil', 2, 'questionOne', 'questionTwo')">
10-20 hours
</a>
</p>
<p class="control column">
<a class="button is-danger" onclick="setRange('oil', 3, 'questionOne', 'questionTwo')">
20+ hours
</a>
</p>
</div>
</div>
<!-- Question Two - Plastic -->
<!-- <div class="box hidden" > -->
<div class="field is-grouped" id="questionTwo" hidden>
<p class="control is-expanded">
<a class="control has-text-dark" type="text"><b>How many items using plastic packaging do you consume per week?</b>
</p>
<div class="columns">
<p class="control column">
<a class="button is-success" onclick="setRange('plastic', 1, 'questionTwo', 'questionThree')">
0-10 items
</a>
</p>
<p class="control column">
<a class="button is-warning" onclick="setRange('plastic', 2, 'questionTwo', 'questionThree')">
10-20 items
</a>
</p>
<p class="control column">
<a class="button is-danger" onclick="setRange('plastic', 3, 'questionTwo', 'questionThree')">
20+ items
</a>
</p>
</div>
</div>
<!-- Question Three - Oil -->
<!-- <div class="box hidden"> -->
<div class="field is-grouped" id="questionThree" hidden>
<p class="control is-expanded">
<a class="control has-text-dark" type="text"><b>How would you best describe your diet?</b>
</p>
<div class="columns">
<p class="control column">
<a class="button is-success" onclick="setRange('forest', 1, [], [], ['landing', 'questionThree'])">
Vegan/Vegetarian
</a>
</p>
<p class="control column">
<a class="button is-warning" onclick="setRange('forest', 0.8, [], [], ['landing', 'questionThree'])">
I eat meat rarely
</a>
</p>
<p class="control column">
<a class="button is-danger" onclick="setRange('forest', 0.6, [], [], ['landing', 'questionThree'])">
I eat meat everyday
</a>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- overlay -->
<div id="blinds" style="position:absolute;margin:0;width:100%;height:100%;background:rgba(0,0,0,0.7);line-height:100%;top:0">
Find the Marker
<img style="position:absolute;width:120px;height:120px;margin-left:-60px;margin-top:-10px;left:50%;top:50%;" src="./assets/gp.png" alt="">
</div>
<!-- buttons at the bottom -->
<div id="ui">
<i class="fab fa-envira" data-size="large"></i>
<i class="fab fa-twitter" data-size="large"></i>
<i class="fab fa-facebook" data-size="large"></i>
<i class="fas fa-shoe-prints" data-size="large"></i>
</div>
<script>
///////////////////////////////////////////////////////////////////////////
///// Init /////
///////////////////////////////////////////////////////////////////////////
/* NOTE: the 'impact' keyword contains an arry of one-argument functions
which are called upon answering the corresponding question */
let pollutionIndicators = {
"oil": {
"range": 0
},
"plastic": {
"range": 0
},
"forest": {
"range": 0,
"impact": [setGrassGreenness, setWaterClarity]
}
}
initForm() // Hide boxes and questions 2,3
document.body.addEventListener('touchmove', e => { e.preventDefault() }, false)
$(".fa-twitter").click( () => { snappysnap('twitter') }) // twitter handle
$(".fa-facebook").click( () => { snappysnap('facebook') }) // fb handle
$(".fa-envira").click( () => { hideMyImpact() }) // undo human impact
$(".fa-shoe-prints").click( () => { showMyImpact() }) // show impact
let date
let orbitRadius = 12
// array of functions for the rendering loop
let onRenderFcts = []
// init scene and camera
let scene = new THREE.Scene()
///////////////////////////////////////////////////////////////////////////
//// Initialize a basic camera + lights ////
///////////////////////////////////////////////////////////////////////////
// Create a camera
let camera = new THREE.PerspectiveCamera
scene.add(camera)
// add lights to scene and dynamic camera based light
let ambientLight = new THREE.AmbientLight(0x212223, 8)
scene.add(ambientLight)
let pointLight = new THREE.DirectionalLight(0xffffff, 4)
pointLight.position.set(-.5, -12, 1.5)
pointLight.castShadow = true
scene.add(pointLight)
scene.add(pointLight.target)
let cameraLight = new THREE.DirectionalLight(0xc4dce5, 4)
camera.add(cameraLight)
// init renderer
let renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
preserveDrawingBuffer: true // needed for screenshots
})
renderer.setPixelRatio(4)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.domElement.style.position = 'absolute'
renderer.domElement.style.top = '0px'
renderer.domElement.style.left = '0px'
renderer.shadowMapEnabled = true
renderer.shadowMapSoft = true
renderer.shadowCameraNear = 0.1
renderer.shadowCameraFar = 9999999999999
renderer.shadowCameraFov = 150
renderer.shadowMapBias = 0.0039
renderer.shadowMapDarkness = 0.2
renderer.shadowMapWidth = 1024
renderer.shadowMapHeight = 1024
document.body.appendChild(renderer.domElement)
////////////////////////////////////////////////////////////////////////////////
// handle arToolkitSource
////////////////////////////////////////////////////////////////////////////////
let arToolkitSource = new THREEx.ArToolkitSource({
// to read from the webcam
sourceType: 'webcam',
sourceWidth: 1980,
sourceHeight: 1080
})
arToolkitSource.init(function onReady() {
onResize()
})
// handle resize
window.addEventListener('resize', function() {
onResize()
})
function onResize() {
arToolkitSource.onResizeElement();
arToolkitSource.copyElementSizeTo(renderer.domElement)
if (arToolkitContext.arController !== null) {
arToolkitSource.copyElementSizeTo(arToolkitContext.arController.canvas)
}
}
///////////////////////////////////////////////////////////////////////////
//// initialize arToolkitContext ////
///////////////////////////////////////////////////////////////////////////
// create atToolkitContext
let arToolkitContext = new THREEx.ArToolkitContext({
cameraParametersUrl: './assets/camera_para.dat',
detectionMode: 'mono',
maxDetectionRate: 90,
canvasWidth: 80 * 3,
canvasHeight: 60 * 3,
})
// initialize it
arToolkitContext.init(function onCompleted() {
// copy projection matrix to camera
camera.projectionMatrix.copy(arToolkitContext.getProjectionMatrix());
})
// update artoolkit on every frame
onRenderFcts.push(function() {
if (arToolkitSource.ready == false) return
arToolkitContext.update(arToolkitSource.domElement)
})
///////////////////////////////////////////////////////////////////////////
//// Create a ArMarkerControls ////
///////////////////////////////////////////////////////////////////////////
let markerRoot = new THREE.Group()
scene.add(markerRoot)
let artoolkitMarker = new THREEx.ArMarkerControls(arToolkitContext, markerRoot, {
type: 'pattern',
patternUrl: './assets/patt.gp',
size: 1
})
// build a smoothedControls
let smoothedRoot = new THREE.Group()
scene.add(smoothedRoot)
let smoothedControls = new THREEx.ArSmoothedControls(smoothedRoot, {
lerpPosition: 0.25,
lerpQuaternion: 0.125,
lerpScale: 1,
})
onRenderFcts.push(function(delta) { // NOTE: what is delta needed for ?
smoothedControls.update(markerRoot)
})
///////////////////////////////////////////////////////////////////////////
//// add an object in the scene ////
///////////////////////////////////////////////////////////////////////////
let arWorldRoot = smoothedRoot
arWorldRoot.name = "arWorldRoot" // makes finding it easier
let geometry = new THREE.SphereGeometry(0.3, 32, 32)
let material = new THREE.MeshBasicMaterial({ color: 0xffff00 })
let sphere = new THREE.Mesh(geometry, material)
arWorldRoot.add(sphere)
let mtlLoader = new THREE.MTLLoader() // initiate material loader
// iterate over all islands -----------------------------------------------
Object.keys(islands).forEach(island => {
loadAsset(islands, "./assets/islands/", island)
})
// create grid for tree placement
let grid = createGrid([-1.3, -1.3], [0.2, 0.2], 0.1)
let shuffledGrid = shuffle(grid)
let forestsize = 10
shuffledGrid = shuffledGrid.slice(0, forestsize)
// iterate over all trees
let typesOfTrees = Object.keys(trees)
let numberOfTrees = typesOfTrees.length
let randType
for (let k = 0; k < shuffledGrid.length; k++) { // tree creation ----------
// create random decision between the trees:
randType = Math.floor(Math.random() * numberOfTrees)
// extract gridposition
let [x, z] = shuffledGrid[k]
// load asset at that position
loadAsset(trees, "./assets/trees/", typesOfTrees[randType], [x, 0.4, z])
}
///////////////////////////////////////////////////////////////////////////
//// render the whole thing on the page ////
///////////////////////////////////////////////////////////////////////////
// render the scene
onRenderFcts.push(function() {
renderer.render(scene, camera)
})
// run the rendering loop
let lastTimeMsec = null
requestAnimationFrame(function animate(nowMsec) {
// keep looping
requestAnimationFrame(animate)
// lift or lower blinds based on whether marker is visible or not.
if (markerRoot.visible) {
$("#blinds").fadeOut(100)
$("#ui").fadeIn()
} else {
$("#blinds").fadeIn()
$("#ui").fadeOut(100)
}
// measure time
lastTimeMsec = lastTimeMsec || nowMsec - 1000 / 60
let deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
onRenderFcts.forEach(function(onRenderFct) {
onRenderFct(deltaMsec / 1000, nowMsec / 1000)
})
// iterate over all available islands
let availableIslands = []
// push all available islands to array
scene.getObjectByName(arWorldRoot.name).children.forEach(obj => {
if (obj.name !== "") {
availableIslands.push(obj.name)
}
})
// actual iteration
availableIslands.forEach(island => {
let islandModel = model(island)
let dir = 1 // short direction variable name for convenience
// switch-case:
switch (island) {
case "island_forest":
if (islandModel.position.y < 2) {
islands[island].direction = 1
} else if (islandModel.position.y > 2.4) {
islands[island].direction = -1
}
dir = islands[island].direction
islandModel.position.set(islandModel.position.x,
islandModel.position.y + 0.001 * dir,
islandModel.position.z)
// tree placement
islands[island].group.children.forEach(tree => {
tree.position.set(
tree.position.x,
islandModel.position.y + 0.001 * dir,
tree.position.z)
})
break;
case "island_main":
if (islandModel.position.y < 1.5) {
islands[island].direction = 1
} else if (islandModel.position.y > 2.9) {
islands[island].direction = -1
}
dir = islands[island].direction
islandModel.position.set(islandModel.position.x,
islandModel.position.y + 0.002 * dir,
islandModel.position.z)
break;
case "island_lake":
if (islandModel.position.y < 1.8) {
islands[island].direction = 1
} else if (islandModel.position.y > 2.4) {
islands[island].direction = -1
}
dir = islands[island].direction
islandModel.position.set(islandModel.position.x,
islandModel.position.y + 0.003 * dir,
islandModel.position.z)
break;
default:
//console.log(`entered default case for variable ${island}.`)
}
})
date = Date.now() * 0.0006
pointLight.position.set(
Math.cos(date) * orbitRadius,
Math.sin(date) * orbitRadius,
Math.sin(date) * orbitRadius
)
sphere.position.set(
Math.cos(date) * orbitRadius,
Math.sin(date) * orbitRadius,
Math.sin(date) * orbitRadius
)
})
///////////////////////////////////////////////////////////////////////////
// CUSTOM FUNCTIONS //
///////////////////////////////////////////////////////////////////////////
function showMyImpact(){
/**
* set all functions/values to show the humans impact here
**/
setGrassGreenness(pollutionIndicators.forest)
setWaterClarity(pollutionIndicators.forest)
}
function hideMyImpact(){
/**
* revert all values to default/idyllic values
**/
setGrassGreenness(1)
setWaterClarity(1)
}
// NOTE: Legacy code below, left for now ----------------------------------
/*
function findMat(iname, tname) {
materials = model(iname).children[0].material;
for (var x = 0; x < materials.length; x++) {
if (materials[x].name == tname) {
return materials[x]
}
}
}
function animate() {
console.log("placeholder function cause gabe forgot")
}
//Island Deleter
function deleteIslands() {
for (var i = 0; i < arWorldRoot.children.length; i++) {
if (arWorldRoot.children[i].name) {
var toRemove = model(arWorldRoot.children[i].name);
arWorldRoot.remove(toRemove);
// animate();
}
}
}
*/
</script>
</body>
</html>