-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvillage.html
More file actions
649 lines (539 loc) · 25.9 KB
/
Copy pathvillage.html
File metadata and controls
649 lines (539 loc) · 25.9 KB
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Babylon.js sample code</title>
<!-- Babylon.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.2/dat.gui.min.js"></script>
<script src="https://assets.babylonjs.com/generated/Assets.js"></script>
<script src="https://preview.babylonjs.com/ammo.js"></script>
<script src="https://preview.babylonjs.com/cannon.js"></script>
<script src="https://preview.babylonjs.com/Oimo.js"></script>
<script src="https://preview.babylonjs.com/earcut.min.js"></script>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.min.js"></script>
<script src="https://preview.babylonjs.com/proceduralTexturesLibrary/babylonjs.proceduralTextures.min.js"></script>
<script src="https://preview.babylonjs.com/postProcessesLibrary/babylonjs.postProcess.min.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.js"></script>
<script src="https://preview.babylonjs.com/serializers/babylonjs.serializers.min.js"></script>
<script src="https://preview.babylonjs.com/gui/babylon.gui.min.js"></script>
<script src="https://preview.babylonjs.com/inspector/babylon.inspector.bundle.js"></script>
<style>
html,
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
</head>
<body>
<canvas id="renderCanvas"></canvas>
<script>
var canvas = document.getElementById("renderCanvas");
var startRenderLoop = function (engine) {
engine.runRenderLoop(function () {
if (sceneToRender && sceneToRender.activeCamera) {
sceneToRender.render();
}
});
}
var engine = null;
var scene = null;
var sceneToRender = null;
var createDefaultEngine = function () { return new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true, disableWebGL2Support: false }); };
const createScene = () => {
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2.2, Math.PI / 2.2, 15, new BABYLON.Vector3(0, 0, 0));
camera.upperBetaLimit = Math.PI / 2.2;
camera.attachControl(canvas, true);
//const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0));
const light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0, -1, 1), scene);
light.position = new BABYLON.Vector3(0, 50, -100);
//light.intensity = 0.3;
// Shadow generator
const shadowGenerator = new BABYLON.ShadowGenerator(10240, light);
const sound = new BABYLON.Sound("sound", Assets.sound.cannonBlast.path, scene);
//Leave time for the sound file to load before playing it
//setInterval(() => sound.play(), 3000);
//box
const trees = spriteTrees();
const sky = buildSky(scene);
const buildings = buildDwellings();
const car = buildCar(scene,shadowGenerator);
const dude = buildDude(scene, shadowGenerator);
const fountain = buildFountain(scene);
const streetLight = buildStreetLight(scene, shadowGenerator);
const lightPanel = buildLightPanel(light);
//shadowGenerator.addShadowCaster(car, true);
shadowGenerator.addShadowCaster(fountain, true);
for (let i = 0; i < buildings.length; i++) {
shadowGenerator.addShadowCaster(buildings[i], true);
}
const ground = buildGround();
ground.receiveShadows = true;
return scene;
}
const buildLightPanel = (light) => {
const adt = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
const panel = new BABYLON.GUI.StackPanel();
panel.width = "220px";
panel.top = "-50px";
panel.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT;
panel.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
adt.addControl(panel);
const header = new BABYLON.GUI.TextBlock();
header.text = "Night to Day";
header.height = "30px";
header.color = "white";
panel.addControl(header);
const slider = new BABYLON.GUI.Slider();
slider.minimum = 0;
slider.maximum = 1;
slider.borderColor = "black";
slider.color = "#AAAAAA";
slider.background = "#white";
slider.value = 1;
slider.height = "20px";
slider.width = "200px";
light.intensity = slider.value;
slider.onValueChangedObservable.add((value) => {
if (light) {
light.intensity = value;
}
});
panel.addControl(slider);
}
const buildStreetLight = (scene, shadowGenerator) => {
BABYLON.SceneLoader.ImportMeshAsync("", "https://assets.babylonjs.com/meshes/", "lamp.babylon").then(() => {
const lampLight = new BABYLON.SpotLight("lampLight", BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, -1, 0), 0.6 * Math.PI, 7, scene);
lampLight.diffuse = BABYLON.Color3.Yellow();
lampLight.parent = scene.getMeshByName("bulb");
const lamp = scene.getMeshByName("lamp");
lamp.position = new BABYLON.Vector3(-0.5, 0, 0);
lamp.rotation = BABYLON.Vector3.Zero();
lamp.rotation.y = -Math.PI / 4;
lamp1 = lamp.clone("lamp1");
lamp1.position.x = -7.5;
lamp1.position.z = 1.7;
lamp1.rotation.y = Math.PI / 2;
lamp2 = lamp1.clone("lamp2");
lamp2.position.x = -3.5;
lamp2.position.z = -0.1;
lamp2.rotation.y = -Math.PI / 2;
lamp3 = lamp.clone("lamp3");
lamp3.position.x = 1.2;
lamp3.position.z = -6.3;
lamp3.rotation.y = Math.PI / 32;
lamp4 = lamp.clone("lamp4");
lamp4.position.x = 4;
lamp4.position.z = -3.7;
lamp4.rotation.y = Math.PI;
shadowGenerator.addShadowCaster(lamp, true);
shadowGenerator.addShadowCaster(lamp1, true);
shadowGenerator.addShadowCaster(lamp2, true);
shadowGenerator.addShadowCaster(lamp3, true);
shadowGenerator.addShadowCaster(lamp4, true);
});
}
const buildFountain = (scene) => {
const fountainProfile = [
new BABYLON.Vector3(0, 0, 0),
new BABYLON.Vector3(0.5, 0, 0),
new BABYLON.Vector3(0.5, 0.2, 0),
new BABYLON.Vector3(0.4, 0.2, 0),
new BABYLON.Vector3(0.4, 0.05, 0),
new BABYLON.Vector3(0.05, 0.1, 0),
new BABYLON.Vector3(0.05, 0.8, 0),
new BABYLON.Vector3(0.15, 0.9, 0)
];
const fountain = BABYLON.MeshBuilder.CreateLathe("fountain", { shape: fountainProfile, sideOrientation: BABYLON.Mesh.DOUBLESIDE }, scene);
//fountain.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
fountain.position.x = -4;
fountain.position.z = -6;
// Create a particle system
var particleSystem = new BABYLON.ParticleSystem("particles", 250, scene);
//Texture of each particle
particleSystem.particleTexture = new BABYLON.Texture(Assets.textures.flare_png.path, scene);
// Where the particles come from
particleSystem.emitter = new BABYLON.Vector3(-4, 0.9, -6); // the starting object, the emitter
particleSystem.minEmitBox = new BABYLON.Vector3(-0.05, 0, 0); // Starting all from
particleSystem.maxEmitBox = new BABYLON.Vector3(0.05, 0, 0); // To...
// Colors of all particles
particleSystem.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0);
particleSystem.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0);
particleSystem.colorDead = new BABYLON.Color4(0, 0, 0.2, 0.0);
// Size of each particle (random between...
particleSystem.minSize = 0.04;
particleSystem.maxSize = 0.20;
// Life time of each particle (random between...
particleSystem.minLifeTime = 2;
particleSystem.maxLifeTime = 3.5;
// Emission rate
particleSystem.emitRate = 1500;
// Blend mode : BLENDMODE_ONEONE, or BLENDMODE_STANDARD
particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
// Set the gravity of all particles
particleSystem.gravity = new BABYLON.Vector3(0, -0.3, 0);
// Direction of each particle after it has been emitted
particleSystem.direction1 = new BABYLON.Vector3(-0.1, 0.4, 0.1);
particleSystem.direction2 = new BABYLON.Vector3(0.1, 0.4, -0.1);
// Angular speed, in radians
particleSystem.minAngularSpeed = 0;
particleSystem.maxAngularSpeed = Math.PI;
// Speed
particleSystem.minEmitPower = 0.5;
particleSystem.maxEmitPower = 1.5;
particleSystem.updateSpeed = 0.25;
//Switch fountain on and off
let switched = false;
const pointerDown = (mesh) => {
if (mesh === fountain) {
switched = !switched;
if (switched) {
// Start the particle system
particleSystem.start();
}
else {
// Stop the particle system
particleSystem.stop();
}
}
}
scene.onPointerObservable.add((pointerInfo) => {
switch (pointerInfo.type) {
case BABYLON.PointerEventTypes.POINTERDOWN:
if (pointerInfo.pickInfo.hit) {
pointerDown(pointerInfo.pickInfo.pickedMesh)
}
break;
}
});
// Start the particle system
//particleSystem.start();
return fountain;
}
const spriteTrees = () => {
const spriteManagerTrees = new BABYLON.SpriteManager("treesManager", Assets.sprites.palm_png.path, 2000, { width: 512, height: 1024 }, scene);
//We create trees at random positions
for (let i = 0; i < 500; i++) {
const tree = new BABYLON.Sprite("tree", spriteManagerTrees);
tree.position.x = Math.random() * (-30);
tree.position.z = Math.random() * 20 + 8;
tree.position.y = 0.5;
}
for (let i = 0; i < 500; i++) {
const tree = new BABYLON.Sprite("tree", spriteManagerTrees);
tree.position.x = Math.random() * (25) + 7;
tree.position.z = Math.random() * -35 + 8;
tree.position.y = 0.5;
}
}
const buildSky = (scene) => {
const skybox = BABYLON.MeshBuilder.CreateBox("skyBox", { size: 150 }, scene);
const skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("https://assets.babylonjs.com/skyboxes/skybox/skybox", 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;
}
const buildGround = () => {
const largeGroundMat = new BABYLON.StandardMaterial("largeGroundMat");
largeGroundMat.diffuseTexture = new BABYLON.Texture("https://assets.babylonjs.com/environments/valleygrass.png");
const largeGround = BABYLON.MeshBuilder.CreateGroundFromHeightMap("largeGround", "https://assets.babylonjs.com/environments/villageheightmap.png", { width: 150, height: 150, subdivisions: 20, minHeight: 0, maxHeight: 10 });
largeGround.material = largeGroundMat;
//调节灯数量,默认3个
largeGround.material.maxSimultaneousLights = 10;
//const ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 15, height: 16 });
//const groundMat = new BABYLON.StandardMaterial("groundMat");
//groundMat.diffuseColor = new BABYLON.Color3(0, 0.3, 0);
//ground.material = groundMat; //Place the material property of the ground
//ground.receiveShadows = true;
return largeGround;
}
const buildDude = (scene, shadowGenerator) => {
const walk = function (turn, dist) {
this.turn = turn;
this.dist = dist;
}
const track = [];
track.push(new walk(86, 7));
track.push(new walk(-85, 14.8));
track.push(new walk(-93, 16.5));
track.push(new walk(48, 25.5));
track.push(new walk(-112, 30.5));
track.push(new walk(-72, 33.2));
track.push(new walk(42, 37.5));
track.push(new walk(-98, 45.2));
track.push(new walk(0, 47));
// Dude
BABYLON.SceneLoader.ImportMeshAsync("him", Assets.meshes.dude.rootUrl, Assets.meshes.dude.filename, scene).then((result) => {
const dude = result.meshes[0];
dude.scaling = new BABYLON.Vector3(0.008, 0.008, 0.008);
shadowGenerator.addShadowCaster(dude, true);
dude.position = new BABYLON.Vector3(-6, 0, 0);
dude.rotate(BABYLON.Axis.Y, BABYLON.Tools.ToRadians(-95), BABYLON.Space.LOCAL);
const startRotation = dude.rotationQuaternion.clone();
scene.beginAnimation(result.skeletons[0], 0, 100, true, 1.0);
let distance = 0;
let step = 0.015;
let p = 0;
scene.onBeforeRenderObservable.add(() => {
dude.movePOV(0, 0, step);
distance += step;
if (distance > track[p].dist) {
dude.rotate(BABYLON.Axis.Y, BABYLON.Tools.ToRadians(track[p].turn), BABYLON.Space.LOCAL);
p += 1;
p %= track.length;
if (p === 0) {
distance = 0;
dude.position = new BABYLON.Vector3(-6, 0, 0);
dude.rotationQuaternion = startRotation.clone();
}
}
});
return dude;
});
}
const buildDwellings = () => {
const detached_house = buildHouse(1);
detached_house.rotation.y = -Math.PI / 16;
detached_house.position.x = -6.8;
detached_house.position.z = 2.5;
const semi_house = buildHouse(2);
semi_house.rotation.y = -Math.PI / 16;
semi_house.position.x = -4.5;
semi_house.position.z = 3;
const places = []; //each entry is an array [house type, rotation, x, z]
places.push([1, -Math.PI / 16, -6.8, 2.5]);
places.push([2, -Math.PI / 16, -4.5, 3]);
places.push([2, -Math.PI / 16, -1.5, 4]);
places.push([2, -Math.PI / 3, 1.5, 6]);
places.push([2, 15 * Math.PI / 16, -6.4, -1.5]);
places.push([1, 15 * Math.PI / 16, -4.1, -1]);
places.push([2, 15 * Math.PI / 16, -2.1, -0.5]);
places.push([1, 5 * Math.PI / 4, 0, -1]);
places.push([1, Math.PI + Math.PI / 2.5, 0.5, -3]);
places.push([2, Math.PI + Math.PI / 2.1, 0.75, -5]);
places.push([1, Math.PI + Math.PI / 2.25, 0.75, -7]);
places.push([2, Math.PI / 1.9, 4.75, -1]);
places.push([1, Math.PI / 1.95, 4.5, -3]);
places.push([2, Math.PI / 1.9, 4.75, -5]);
places.push([1, Math.PI / 1.9, 4.75, -7]);
places.push([2, -Math.PI / 3, 5.25, 2]);
places.push([1, -Math.PI / 3, 6, 4]);
//Create instances from the first two that were built
const houses = [];
for (let i = 0; i < places.length; i++) {
if (places[i][0] === 1) {
houses[i] = detached_house.createInstance("house" + i);
}
else {
houses[i] = semi_house.createInstance("house" + i);
}
houses[i].rotation.y = places[i][1];
houses[i].position.x = places[i][2];
houses[i].position.z = places[i][3];
}
return houses;
}
const buildHouse = (width) => {
const box = buildBox(width);
const roof = buildRoof(width);
return BABYLON.Mesh.MergeMeshes([box, roof], true, false, null, false, true);
}
const buildRoof = (width) => {
//basic house
const roof = BABYLON.MeshBuilder.CreateCylinder("roof", { diameter: 1.3, height: 1.2, tessellation: 3 });
roof.scaling.x = 0.75;
roof.scaling.y = width;
roof.rotation.z = Math.PI / 2;
//roof.rotation.y = Math.PI / 4;
roof.position.y = 1.22;
const roofMat = new BABYLON.StandardMaterial("roofMat");
roofMat.diffuseTexture = new BABYLON.Texture("https://assets.babylonjs.com/environments/roof.jpg", scene);
roof.material = roofMat;
return roof;
}
const buildBox = (width) => {
//texture
const boxMat = new BABYLON.StandardMaterial("boxMat");
const faceUV = [];
if (width == 2) {
boxMat.diffuseTexture = new BABYLON.Texture("https://assets.babylonjs.com/environments/semihouse.png")
faceUV[0] = new BABYLON.Vector4(0.6, 0.0, 1.0, 1.0); //rear face
faceUV[1] = new BABYLON.Vector4(0.0, 0.0, 0.4, 1.0); //front face
faceUV[2] = new BABYLON.Vector4(0.4, 0, 0.6, 1.0); //right side
faceUV[3] = new BABYLON.Vector4(0.4, 0, 0.6, 1.0); //left side
}
else {
boxMat.diffuseTexture = new BABYLON.Texture("https://assets.babylonjs.com/environments/cubehouse.png");
faceUV[0] = new BABYLON.Vector4(0.5, 0.0, 0.75, 1.0); //rear face
faceUV[1] = new BABYLON.Vector4(0.0, 0.0, 0.25, 1.0); //front face
faceUV[2] = new BABYLON.Vector4(0.25, 0, 0.5, 1.0); //right side
faceUV[3] = new BABYLON.Vector4(0.75, 0, 1.0, 1.0); //left side
}
const box = BABYLON.MeshBuilder.CreateBox("box", { faceUV: faceUV, wrap: true });
//box.scaling = new BABYLON.Vector3(2, 1.5, 3);
// box.position.x = -2;
box.position.y = 0.5;
box.scaling.x = width;
// box.position.z = 0.1;
//box.rotation.y = Math.PI / 4;
//box.rotation.y = BABYLON.Tools.ToRadians(45);
// const boxMat = new BABYLON.StandardMaterial("boxMat");
// boxMat.diffuseTexture = new BABYLON.Texture("https://www.babylonjs-playground.com/textures/floor.png");
box.material = boxMat;
return box;
}
const buildCar = (scene,shadowGenerator) => {
const carBody = buildCarBody();
const carWheels = buildWheels(carBody, scene);
carBody.rotation = new BABYLON.Vector3(-Math.PI / 2, Math.PI, -Math.PI / 2);
carBody.position.y = 0.16;
carBody.position.x = 3;
carBody.position.z = 8;
shadowGenerator.addShadowCaster(carBody, true);
const animCar = new BABYLON.Animation("carAnimation", "position.z", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
const carKeys = [];
carKeys.push({
frame: 0,
value: 8
});
carKeys.push({
frame: 150,
value: -7
});
carKeys.push({
frame: 200,
value: -7
});
animCar.setKeys(carKeys);
carBody.animations = [];
carBody.animations.push(animCar);
scene.beginAnimation(carBody, 0, 200, true);
return carBody;
}
const buildCarBody = (shadowGenerator) => {
const outline = [
new BABYLON.Vector3(-0.3, 0, -0.1),
new BABYLON.Vector3(0.2, 0, -0.1),
]
//curved front
for (let i = 0; i < 20; i++) {
outline.push(new BABYLON.Vector3(0.2 * Math.cos(i * Math.PI / 40), 0, 0.2 * Math.sin(i * Math.PI / 40) - 0.1));
}
//top
outline.push(new BABYLON.Vector3(0, 0, 0.1));
outline.push(new BABYLON.Vector3(-0.3, 0, 0.1));
//car face UVs
const faceUV = [];
faceUV[0] = new BABYLON.Vector4(0, 0.5, 0.38, 1);
faceUV[1] = new BABYLON.Vector4(0, 0, 1, 0.5);
faceUV[2] = new BABYLON.Vector4(0.38, 1, 0, 0.5);
//car material
const carMat = new BABYLON.StandardMaterial("carMat");
carMat.diffuseTexture = new BABYLON.Texture("https://assets.babylonjs.com/environments/car.png");
const carBody = BABYLON.MeshBuilder.ExtrudePolygon("carBody", { shape: outline, depth: 0.2, faceUV: faceUV, wrap: true });
carBody.material = carMat;
return carBody;
}
const buildWheels = (carBody, scene) => {
//car material
const wheelMat = new BABYLON.StandardMaterial("wheelMat");
wheelMat.diffuseTexture = new BABYLON.Texture("https://assets.babylonjs.com/environments/wheel.png");
const wheelRB = BABYLON.MeshBuilder.CreateCylinder("wheelRB", { diameter: 0.125, height: 0.05 });
wheelRB.material = wheelMat;
wheelRB.parent = carBody;
wheelRB.position.z = -0.1;
wheelRB.position.x = -0.2;
wheelRB.position.y = 0.035;
const wheelRF = wheelRB.clone("wheelRF");
wheelRF.position.x = 0.1;
const wheelLB = wheelRB.clone("wheelLB");
wheelLB.position.y = -0.2 - 0.035;
const wheelLF = wheelRF.clone("wheelLF");
wheelLF.position.y = -0.2 - 0.035;
const animWheel = wheelAnimation();
//Link this animation to the right back wheel
wheelRB.animations = [];
wheelRB.animations.push(animWheel);
wheelRF.animations = [];
wheelRF.animations.push(animWheel);
wheelLB.animations = [];
wheelLB.animations.push(animWheel);
wheelLF.animations = [];
wheelLF.animations.push(animWheel);
scene.beginAnimation(wheelRB, 0, 30, true);
scene.beginAnimation(wheelRF, 0, 30, true);
scene.beginAnimation(wheelLB, 0, 30, true);
scene.beginAnimation(wheelLF, 0, 30, true);
}
const carAnimation = () => {
const animCar = new BABYLON.Animation("carAnimation", "position.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
const carKeys = [];
carKeys.push({
frame: 0,
value: -4
});
carKeys.push({
frame: 150,
value: 4
});
carKeys.push({
frame: 210,
value: 4
});
animCar.setKeys(carKeys);
return animCar;
}
const wheelAnimation = () => {
const animWheel = new BABYLON.Animation("wheelAnimation", "rotation.y", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
const wheelKeys = [];
//At the animation key 0, the value of rotation.y is 0
wheelKeys.push({
frame: 0,
value: 0
});
//At the animation key 30, (after 1 sec since animation fps = 30) the value of rotation.y is 2PI for a complete rotation
wheelKeys.push({
frame: 30,
value: 2 * Math.PI
});
//set the keys
animWheel.setKeys(wheelKeys);
return animWheel;
}
window.initFunction = async function () {
var asyncEngineCreation = async function () {
try {
return createDefaultEngine();
} catch (e) {
console.log("the available createEngine function failed. Creating the default engine instead");
return createDefaultEngine();
}
}
window.engine = await asyncEngineCreation();
if (!engine) throw 'engine should not be null.';
startRenderLoop(engine, canvas);
window.scene = createScene();
};
initFunction().then(() => {
sceneToRender = scene
});
// Resize
window.addEventListener("resize", function () {
engine.resize();
});
</script>
</body>
</html>