-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.html
504 lines (439 loc) · 21.3 KB
/
main.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Avalanche Intuition</title>
<style>
html,
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: 'Digital';
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
@font-face {
font-family: 'Digital';
src: url('./fonts/DS-DIGI.TTF') format('truetype');
/* font from https://www.dafont.com/ds-digital.font */
}
</style>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
<script src="https://preview.babylonjs.com/gui/babylon.gui.min.js"></script>
<script>
var canvas = null;
var engine = null;
/******* Add the create scene function ******/
function createScene() {
let scene, camera, arcRotateCamera, light, ground, advancedTexture, clicked;
let beaconNumbers, victim, hit, distanceToVictim, noisyTarget, victimMesh;
// Functions
// Setup functions
function setupScene() {
scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3(0.2, 0.2, 0.2);
scene.gravity = new BABYLON.Vector3(0, -1, 0);
scene.collisionsEnabled = true;
};
function setupUniversalCamera() {
camera = new BABYLON.UniversalCamera(
"Camera",
new BABYLON.Vector3(-50, 415, 0), // position
scene
);
camera.fov = 1.2;
camera.inertia = 0;
camera.keysUp = [87]; // W
camera.keysDown = [83]; // S
camera.keysLeft = [65]; // A
camera.keysRight = [68]; // D
camera.speed = 3;
camera.attachControl(canvas, true);
camera.needMoveForGravity = true;
camera.ellipsoid = new BABYLON.Vector3(0.05, 1, 0.05);
camera.applyGravity = true;
camera.checkCollisions = true;
scene.activeCamera = camera;
};
// target must be Vector3
function setupArcRotateCamera(target) {
// make smooth transition
noisyTarget = new BABYLON.Vector3(target.x + randomNumber(-15, 15), target.y, target.z + randomNumber(-15, 15));
arcRotateCamera = new BABYLON.ArcRotateCamera("arcCamera", 0, 0, 12, noisyTarget, scene);
arcRotateCamera.fov = 1.2;
arcRotateCamera.upperBetaLimit = (Math.PI / 2) * 0.99;
camera.detachControl();
arcRotateCamera.attachControl(canvas, true);
scene.activeCamera = arcRotateCamera;
arcRotateCamera.ellipsoid = new BABYLON.Vector3(2, 2, 2);
arcRotateCamera.checkCollisions = true;
};
function setupLights() {
light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene);
sun = new BABYLON.PointLight("sun", new BABYLON.Vector3(1, 1, 10), scene);
};
function setupGUI(vector = new BABYLON.Vector3.Zero()) {
advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
beaconNumbers = new BABYLON.GUI.TextBlock();
beaconNumbers.fontFamily = 'Digital';
beaconNumbers.fontStyle = 'bold';
beaconNumbers.color = "red";
beaconNumbers.fontSize = 100;
// beaconNumbers.left = "-40%";
// beaconNumbers.top = "-40%";
// update the text
beaconNumbers.onBeforeDrawObservable.add(function () {
let x_pos = camera.position.x - vector.x;
let y_pos = camera.position.y - vector.y;
let z_pos = camera.position.z - vector.z;
distanceToVictim = distance(x_pos, y_pos, z_pos)
beaconNumbers.text = distanceToVictim;
});
advancedTexture.addControl(beaconNumbers);
};
// Object functions
// make custom heightmap from contour map of Stagleap Provincial Park
function mountains() {
let groundMaterial = new BABYLON.StandardMaterial("ground", scene);
groundMaterial.diffuseColor = new BABYLON.Color3(250 / 256, 250 / 256, 255 / 256);
groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
ground = BABYLON.MeshBuilder.CreateGroundFromHeightMap("stagleap", "heightmaps/stagleap.png", {
width: 2000, height: 2000, subdivisions: 100, maxHeight: 500, minHeight: 2
});
ground.material = groundMaterial;
ground.checkCollisions = true;
};
function skybox() {
var skybox = BABYLON.MeshBuilder.CreateBox("skyBox", { size: 2000.0 }, scene);
var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("skybox/skybox4", scene);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
skybox.material = skyboxMaterial;
};
function addBeaconFromBlender() {
BABYLON.SceneLoader.ImportMesh("", "./models/", "beacon.babylon", scene, function (newMeshes) {
// make new mesh display in front of ground
newMeshes[0].renderingGroupId = 1;
newMeshes[1].renderingGroupId = 2;
newMeshes[2].renderingGroupId = 3;
// transforming beacon mesh
newMeshes[0].scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
newMeshes[0].parent = camera;
newMeshes[0].rotation.z = Math.PI / 2
newMeshes[0].rotation.y = -Math.PI / 2;
newMeshes[0].position.x = -0.5;
newMeshes[0].position.y = -0.5;
newMeshes[0].position.z = 1.5;
// requires beaconNumbers to be defined
beaconNumbers.linkWithMesh(newMeshes[1]);
});
};
function addProbeFromBlender() {
BABYLON.SceneLoader.ImportMesh("", "./models/", "probe.babylon", scene, function (mesh) {
// transforming probe mesh
mesh[0].scaling = new BABYLON.Vector3(1, 0.4, 1);
var probeMaterial = new BABYLON.StandardMaterial("probeMaterial");
probeMaterial.diffuseTexture = new BABYLON.Texture("./textures/amiga.jpg", scene);
probeMaterial.backFaceCulling = false;
mesh[0].position = noisyTarget;
mesh[0].position.y += 20;
mesh[0].material = probeMaterial;
});
};
function probeGUI() {
advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
let probeInstructions = new BABYLON.GUI.TextBlock();
probeInstructions.fontFamily = 'Digital';
probeInstructions.fontStyle = 'bold';
probeInstructions.color = "green";
probeInstructions.fontSize = 40;
probeInstructions.left = "-32%";
probeInstructions.top = "-45%";
probeInstructions.text = "click to toggle pan\npress p to probe";
advancedTexture.addControl(probeInstructions);
};
function winGUI() {
advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
let winText = new BABYLON.GUI.TextBlock();
winText.fontFamily = 'Digital';
winText.fontStyle = 'bold';
winText.color = "blue";
winText.fontSize = 250;
winText.text = "victim\nfound";
advancedTexture.addControl(winText);
}
// Determine location of victim
function victimPosition() {
// I want this to travel downwards, the more negative y the stronger the downward bias
var direction = new BABYLON.Vector3(randomNumber(10, -10), randomNumber(10, -10) - 50, randomNumber(10, -10));
let origin = new BABYLON.Vector3(200, 800, 200); // place origin over basin
// should need to travel maximum of 1415 to hit if direction is good, but doing 2000 just in case.
var ray = new BABYLON.Ray(origin, direction, 2000);
hit = scene.pickWithRay(ray, (mesh) => {
return (mesh === ground);
});
if (hit.hit) {
victim = hit.pickedPoint; // to be able to access victim outside of the function
return hit.pickedPoint;
} else {
return null;
};
};
// requires distance to victim, transitions to ArcRotate Camera over the victim area
function cameraTransition() {
// need to delay or will transition camera immediately as the ray for victim has not been cast
BABYLON.Tools.DelayAsync(100).then(() => { // some weird redundancy here with closeToVictim due to loading not all happening
if (closeToVictim(distanceToVictim)) {
advancedTexture.dispose();
// deleting beacon
scene.getMeshByName("Cube")?.dispose();
scene.getMeshByName("Plane")?.dispose();
scene.getMeshByName("Sphere")?.dispose();
distanceToVictim = 100; // cause the onRenderBeforeObservable conditional to stop running.
setupArcRotateCamera(victim);
addProbeFromBlender();
makeVictimMesh();
probeGUI();
clicked = false; // user has not clicked on canvas when probing yet
scene.onPointerObservable.add((pointerInfo) => {
switch (pointerInfo.type) {
case BABYLON.PointerEventTypes.POINTERDOWN:
// clicked is true if user has already clicked in probing context
if (clicked) {
detachProbe();
} else {
prepareProbe();
};
break;
case BABYLON.PointerEventTypes.POINTERMOVE:
if (clicked) {
attachProbe();
};
break;
};
});
moveProbe();
};
});
};
// purely testing
function testFunction() {
camera.position = victim;
};
// generate a random invisible box near victimPosition for probing
function makeVictimMesh() {
victimMesh = BABYLON.MeshBuilder.CreateBox("victimMesh", { size: 10 });
victimMesh.position = victim;
var victimMaterial = new BABYLON.StandardMaterial("victimMaterial");
victimMaterial.alpha = 0; // testing: makes victim visible
victimMesh.material = victimMaterial;
victimMesh.checkCollisions = true;
};
// gets position on ground (borrowed from BJS Drag Demo)
function getGroundPosition() {
var pickinfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh == ground; });
if (pickinfo.hit) {
return pickinfo.pickedPoint;
}
return noisyTarget;
};
// prepares probe to attach to cursor
function prepareProbe() {
arcRotateCamera.detachControl();
clicked = true;
};
// attaches probe to cursor
function attachProbe() {
var modifiedGroundPosition = getGroundPosition();
modifiedGroundPosition.y += 20;
scene.getMeshByName("Cylinder").position = modifiedGroundPosition;
}
// detaches probe from cursor
function detachProbe() {
arcRotateCamera.attachControl(canvas, true);
clicked = false;
};
// makes probing action
function moveProbe() {
scene.registerAfterRender(function () {
if (scene.getMeshByName("Cylinder")) {
// using imprecise for now
if (scene.getMeshByName("Cylinder").intersectsMesh(victimMesh, false)) {
advancedTexture.dispose();
winGUI();
};
// detect if probe is in animation, and adds snow particles onto probe
if (scene.getMeshByName("Cylinder").position.y < (getGroundPosition().y + 5)) {
let particleLocation = scene.getMeshByName("Cylinder").position;
particleLocation.y -= 20;
snowParticles(particleLocation);
};
};
});
scene.onKeyboardObservable.add((kbInfo) => {
if ((kbInfo.type == BABYLON.KeyboardEventTypes.KEYDOWN) && (kbInfo.event.key == "p" || kbInfo.event.key == "P")) {
animateProbe();
};
});
};
// contains actual probe animation
function animateProbe() {
let frameRate = 10;
let moveY = new BABYLON.Animation("moveY", "position.y", frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
var keyFrames = [];
var originalPosition = scene.getMeshByName("Cylinder").position.y
keyFrames.push({
frame: 0,
value: originalPosition
});
keyFrames.push({
frame: frameRate,
value: originalPosition - 20
});
keyFrames.push({
frame: 2 * frameRate,
value: originalPosition
});
moveY.setKeys(keyFrames);
scene.getMeshByName("Cylinder").animations.push(moveY);
scene.beginAnimation(scene.getMeshByName("Cylinder"), 0, 2 * frameRate, false);
};
function snowParticles(position) {
var emitter = BABYLON.MeshBuilder.CreateBox("emitter", { size: 0.01 }, scene);
emitter.position = position;
var particles = new BABYLON.ParticleSystem("sparks", 1000, scene);
particles.emitter = emitter;
particles.minEmitBox = new BABYLON.Vector3(0, 0, 0);
particles.maxEmitBox = new BABYLON.Vector3(0, 0, 0);
particles.particleTexture = new BABYLON.Texture("./textures/snow.png"); // from the babylon.js demo
particles.minEmitPower = 1;
particles.maxEmitPower = 4;
particles.emitRate = 1500;
particles.color1 = new BABYLON.Color4(1, 1, 1, 1.0);
particles.color2 = new BABYLON.Color4(1, 1, 1, 0.4);
particles.colorDead = new BABYLON.Color4(0.4, 0.4, 0.4, 0.2);
particles.minSize = 0.01;
particles.maxSize = 0.1;
particles.minLifeTime = 0.1;
particles.maxLifeTime = 0.2;
particles.direction1 = new BABYLON.Vector3(1, 0.5, 1);
particles.direction2 = new BABYLON.Vector3(-1, 1, -1);
// particles.minAngularSpeed = 0;
// particles.maxAngularSpeed = 0.5;
particles.targetStopDuration = 0.1;
particles.start();
};
// Other functions (calculations, etc.)
// returns absolute distance given that x y z are numbers
function distance(x, y, z) {
return Math.round(Math.sqrt(x ** 2 + y ** 2 + z ** 2));
};
// returns a random number between min and max
function randomNumber(min, max) {
return Math.random() * (max - min) + min;
};
// returns true if near enough to victim to trigger camera switch
function closeToVictim(distance) {
// guessing ten testing
if (distance > 10) {
return false;
} else {
return true;
}
}
// Scene function calls
setupScene();
setupUniversalCamera();
setupLights();
mountains();
skybox();
// scene functions
scene.executeWhenReady(function () {
var vector = victimPosition();
setupGUI(vector);
addBeaconFromBlender(); // adding this here so I can attach gui to beacon
// sounds
var barryvoxOn = new BABYLON.Sound("barryvoxOn", "./sounds/BarryvoxOn.wav", scene, null, {
loop: false,
autoplay: true
});
var barryvoxFar = new BABYLON.Sound("barryvoxFar", "./sounds/BarryvoxFar.wav", scene, null, {
loop: true,
autoplay: true
});
var barryvoxMedium = new BABYLON.Sound("barryvoxMedium", "./sounds/BarryvoxMedium.wav", scene, null, {
loop: true,
autoplay: false
});
var barryvoxClose = new BABYLON.Sound("barryvoxClose", "./sounds/BarryvoxClose.wav", scene, null, {
loop: true,
autoplay: false
});
var barryvoxScanning = new BABYLON.Sound("barryvoxScanning", "./sounds/BarryvoxScanning.wav", scene, null, {
loop: true,
autoplay: false
});
// very boring and long solution to playing a bunch of sounds
let needMedium = true;
let needClose = true;
let needScanning = true;
scene.onBeforeRenderObservable.add(function () {
if (closeToVictim(distanceToVictim)) {
cameraTransition();
// testFunction(); // to automatically position over victim testing
};
// using need bools to determine whether to play or not
if (distanceToVictim < 200 && needMedium) {
console.log("here");
barryvoxFar.stop();
barryvoxMedium.play();
needMedium = false;
} else if (distanceToVictim < 100 && needClose) {
console.log("here1");
barryvoxMedium.stop();
barryvoxClose.play();
needClose = false;
} else if (distanceToVictim < 30 && needScanning) {
console.log("here2");
barryvoxClose.stop();
barryvoxScanning.play();
needScanning = false;
};
});
});
return scene;
};
/******* End of the create scene function ******/
function init() {
canvas = document.getElementById("renderCanvas");
engine = new BABYLON.Engine(
canvas,
true,
{ preserveDrawingBuffer: true, stencil: true, disableWebGL2Support: false }
);
let scene = createScene();
// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function () {
scene.render();
});
// Watch for browser/canvas resize events
window.addEventListener("resize", function () {
engine.resize();
});
}
</script>
</head>
<body onload="init()">
<canvas id="renderCanvas"></canvas>
</body>
</html>