Skip to content

Commit d273fd3

Browse files
committed
fix: world should no longer load before ready
1 parent 9181e8a commit d273fd3

File tree

6 files changed

+65
-43
lines changed

6 files changed

+65
-43
lines changed

src/index.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,24 @@ function toGuide(e) {
115115
* Load all entities for the world and car
116116
*/
117117
function loadEntities() {
118+
guide.innerHTML = `<p>Now Loading...</p>
119+
<div class="spinner-border" role="status">
120+
</div>`;
118121
world = new LivingRoom(gltfLoader, scene, physicsWorld, groundMaterial, camera, () => {
119122
world.car = new car(gltfLoader, scene, physicsWorld, world.carStart, wheelMaterial, camera);
120123
});
121124

122-
const threeLoaderPromise =
123-
new Promise((resolve) => {
124-
loadingManager.onLoad = () => {
125-
resolve();
126-
};
127-
});
125+
const checkLoaded = () => {
126+
if (world.isLoaded) {
127+
UITools.removeUIElement(guide);
128+
// Add renderer to the document
129+
document.body.appendChild(renderer.domElement);
130+
} else {
131+
requestAnimationFrame(checkLoaded); // Check again in the next frame
132+
}
133+
};
128134

129-
// Wait for loading manager and Tone to be loaded
130-
Promise.all([threeLoaderPromise, Tone.loaded()]).then(() => {
131-
UITools.removeUIElement(guide);
132-
});
135+
checkLoaded();
133136
}
134137

135138
/**
@@ -177,10 +180,9 @@ function init() {
177180
groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0);
178181
physicsWorld.addBody(groundBody);
179182

180-
//specify our renderer and add it to our document
183+
//specify our renderer
181184
renderer = new THREE.WebGLRenderer({antialias: true});
182185
renderer.setSize(window.innerWidth, window.innerHeight);
183-
document.body.appendChild(renderer.domElement);
184186

185187
// lighting
186188
colour = 0xffffff;

src/scripts/cars/Car.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,17 @@ export default class Car {
8787

8888
// Wait for vehicle to load before proceeding
8989
const checkVehicleLoaded = () => {
90-
if (this.chassis && this.isLoaded) {
90+
if (this.chassis) {
91+
// Configure honk and engine noise
92+
this._createHonk();
93+
this._createEngineNoise();
94+
this._createCollisionNoise();
9195
this.isLoaded = true;
9296
} else {
9397
requestAnimationFrame(checkVehicleLoaded); // Check again in the next frame
9498
}
9599
};
96100
checkVehicleLoaded();
97-
98-
// Configure honk and engine noise
99-
this._createHonk();
100-
this._createEngineNoise();
101-
this._createCollisionNoise();
102101
});
103102
}
104103

@@ -285,6 +284,25 @@ export default class Car {
285284

286285
// Add the vehicle to the physics world
287286
this.vehicle.addToWorld(physicsWorld);
287+
288+
this._syncToPhysics();
289+
}
290+
291+
/**
292+
* Sync models to their physics body
293+
* @protected
294+
*/
295+
_syncToPhysics() {
296+
// Sync the chassis
297+
this.models.chassis.mesh.position.copy(this.chassis.position);
298+
this.models.chassis.mesh.quaternion.copy(this.chassis.quaternion);
299+
300+
// Sync each wheel mesh with the wheel body
301+
this.vehicle.wheelBodies.forEach((wheelBody, index) => {
302+
const wheelMesh = this.models.wheels[index].mesh
303+
wheelMesh.position.copy(wheelBody.position);
304+
wheelMesh.quaternion.copy(wheelBody.quaternion);
305+
});
288306
}
289307

290308
/**
@@ -330,7 +348,6 @@ export default class Car {
330348

331349
// Save instance
332350
Car.instance = this;
333-
this.isLoaded = true;
334351
}
335352

336353
/**
@@ -573,23 +590,14 @@ export default class Car {
573590
* @public
574591
*/
575592
update() {
576-
// Do nothing if the care isn't load
593+
// Do nothing if the car isn't loaded
577594
if (!this.isLoaded) return;
578595

579596
// Drive is enabled
580597
if (this.isDriving) this._drive();
581598

582599
// Sync models to their physics body
583-
// Sync the chassis
584-
this.models.chassis.mesh.position.copy(this.chassis.position);
585-
this.models.chassis.mesh.quaternion.copy(this.chassis.quaternion);
586-
587-
// Sync each wheel mesh with the wheel body
588-
this.vehicle.wheelBodies.forEach((wheelBody, index) => {
589-
const wheelMesh = this.models.wheels[index].mesh
590-
wheelMesh.position.copy(wheelBody.position);
591-
wheelMesh.quaternion.copy(wheelBody.quaternion);
592-
});
600+
this._syncToPhysics();
593601

594602
// Control proximity volume between car and camera
595603
proximityVolume(this.chassis.position, this.camera, this.gainNode, -0.3);

src/scripts/objects/Radio.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default class Radio {
1919
this.currentStationIndex = 0;
2020
this.isPlaying = true;
2121
this.radioPosition = this.radioObject.position;
22+
this.isLoaded = false;
2223

2324
// Load radio stations
2425
this.stations = [
@@ -30,6 +31,7 @@ export default class Radio {
3031
// When all stations are loaded, set up the proximity audio
3132
Tone.loaded().then(() => {
3233
this._setupProximityAudio();
34+
this.isLoaded = true;
3335
});
3436
}
3537

src/scripts/objects/TV.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default class TV {
1616
*/
1717
constructor(tvObject, listener, scene) {
1818
this.listener = listener;
19+
this.isLoaded = false;
1920

2021
// Create the video element and texture
2122
this.video = document.createElement('video');
@@ -45,8 +46,17 @@ export default class TV {
4546
tvObject.mesh.visible = false;
4647
}
4748

48-
// Setup proximity audio
49-
this._setupProximityAudio();
49+
// Add video audio to Tone.Player
50+
this.audio = new Tone.Player({
51+
url: "video/jerma_the_saga.mp4",
52+
loop: true,
53+
}).toDestination();
54+
55+
Tone.loaded().then(() => {
56+
this._setupProximityAudio();
57+
this.isLoaded = true;
58+
});
59+
5060
this.isPlaying = false;
5161
}
5262

@@ -55,11 +65,6 @@ export default class TV {
5565
* @protected
5666
*/
5767
_setupProximityAudio() {
58-
// Add video audio to Tone.Player
59-
this.audio = new Tone.Player({
60-
url: "video/jerma_the_saga.mp4",
61-
loop: true,
62-
}).toDestination();
6368

6469
// Set volume
6570
this.audio.volume.value = -10;

src/scripts/worlds/LivingRoom.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ export default class LivingRoom extends World {
137137
// Create tv and radio
138138
this.radio = new Radio(this.objects["Radio"], this.camera);
139139
this.tv = new TV(this.objects["TVScreen"], this.camera, this.scene);
140+
// Wait for assets to load before proceeding
141+
const checkLoaded = () => {
142+
if ((this.radio && this.radio.isLoaded) && (this.tv && this.tv.isLoaded)) {
143+
this.isLoaded = true;
144+
} else {
145+
requestAnimationFrame(checkLoaded); // Check again in the next frame
146+
}
147+
};
148+
149+
checkLoaded();
140150
}
141151

142152
/**

src/scripts/worlds/World.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ export default class World {
4646
// Reference the camera
4747
this.camera = camera;
4848

49-
this.loader = loader;
50-
51-
this.physicsWorld = physicsWorld;
52-
5349
// Reference the car once it's created
5450
this.car = null
5551

@@ -73,7 +69,6 @@ export default class World {
7369
const checkCarLoaded = () => {
7470
if (this.car && this.car.isLoaded) {
7571
this._start();
76-
this.isLoaded = true;
7772
} else {
7873
requestAnimationFrame(checkCarLoaded); // Check again in the next frame
7974
}
@@ -286,7 +281,7 @@ export default class World {
286281
* @protected
287282
*/
288283
_start() {
289-
284+
this.isLoaded = true;
290285
}
291286

292287
/**

0 commit comments

Comments
 (0)