Fix scene doubling on reload and add dynamic inspector import
All checks were successful
Build / build (push) Successful in 1m45s

- Dispose old scene before creating new one (cleans up physics)
- Stop existing render loop before starting new one
- Dynamically import @babylonjs/inspector on 'i' key press

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Michael Mainguy 2025-11-29 06:06:39 -06:00
parent dfa46c85a6
commit cf3a74ff0b
2 changed files with 17 additions and 9 deletions

View File

@ -37,6 +37,8 @@ export async function setupScene(
const audioEngine = await createAudioEngine(); const audioEngine = await createAudioEngine();
reporter.reportProgress(30, 'Audio engine ready'); reporter.reportProgress(30, 'Audio engine ready');
// Stop any existing render loop before starting new one (prevents doubling on reload)
engine.stopRenderLoop();
engine.runRenderLoop(() => DefaultScene.MainScene.render()); engine.runRenderLoop(() => DefaultScene.MainScene.render());
return { engine, audioEngine }; return { engine, audioEngine };
@ -50,6 +52,10 @@ function createEngine(canvas: HTMLCanvasElement): Engine {
} }
function createMainScene(engine: Engine): void { function createMainScene(engine: Engine): void {
// Dispose old scene if it exists (prevents doubling on reload)
if (DefaultScene.MainScene && !DefaultScene.MainScene.isDisposed) {
DefaultScene.MainScene.dispose();
}
DefaultScene.MainScene = new Scene(engine); DefaultScene.MainScene = new Scene(engine);
DefaultScene.MainScene.ambientColor = new Color3(.2, .2, .2); DefaultScene.MainScene.ambientColor = new Color3(.2, .2, .2);
DefaultScene.MainScene.clearColor = new Color3(0, 0, 0).toColor4(); DefaultScene.MainScene.clearColor = new Color3(0, 0, 0).toColor4();

View File

@ -100,7 +100,8 @@ export class KeyboardInput {
document.onkeydown = (ev) => { document.onkeydown = (ev) => {
// Always allow inspector and camera toggle, even when disabled // Always allow inspector and camera toggle, even when disabled
if (ev.key === 'i') { if (ev.key === 'i') {
// Toggle Babylon Inspector // Dynamically import inspector on first use (keeps it out of main bundle)
import('@babylonjs/inspector').then(() => {
if (this._scene.debugLayer.isVisible()) { if (this._scene.debugLayer.isVisible()) {
this._scene.debugLayer.hide(); this._scene.debugLayer.hide();
} else { } else {
@ -109,6 +110,7 @@ export class KeyboardInput {
showExplorer: true, showExplorer: true,
}); });
} }
});
return; return;
} }