Fix initialization errors when navigating to db/public/local
- Fix null reference error in buildColor.ts by initializing metadata.tools array - Add physics engine availability check in buildRig to prevent PhysicsAggregate creation before engine is ready - Remove duplicate scene initialization by eliminating redundant initializeEngine() call - These fixes resolve WebGL shader compilation errors and prevent app crashes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
9d5234b629
commit
58668443c4
@ -37,6 +37,9 @@ export function buildRig(xr: WebXRDefaultExperience): Mesh {
|
|||||||
axis.zAxis.rotation.y = Math.PI;
|
axis.zAxis.rotation.y = Math.PI;
|
||||||
rigMesh.lookAt(new Vector3(0, 0.01, 0));
|
rigMesh.lookAt(new Vector3(0, 0.01, 0));
|
||||||
rigMesh.visibility = 1;
|
rigMesh.visibility = 1;
|
||||||
|
|
||||||
|
// Only create physics aggregate if physics engine is available
|
||||||
|
if (scene.getPhysicsEngine()) {
|
||||||
const rigAggregate =
|
const rigAggregate =
|
||||||
new PhysicsAggregate(
|
new PhysicsAggregate(
|
||||||
rigMesh,
|
rigMesh,
|
||||||
@ -44,5 +47,20 @@ export function buildRig(xr: WebXRDefaultExperience): Mesh {
|
|||||||
{friction: 0, center: Vector3.Zero(), mass: 50, restitution: .01},
|
{friction: 0, center: Vector3.Zero(), mass: 50, restitution: .01},
|
||||||
scene);
|
scene);
|
||||||
rigAggregate.body.setMotionType(PhysicsMotionType.DYNAMIC);
|
rigAggregate.body.setMotionType(PhysicsMotionType.DYNAMIC);
|
||||||
|
} else {
|
||||||
|
// Add physics aggregate once physics is initialized
|
||||||
|
scene.onReadyObservable.addOnce(() => {
|
||||||
|
if (scene.getPhysicsEngine()) {
|
||||||
|
const rigAggregate =
|
||||||
|
new PhysicsAggregate(
|
||||||
|
rigMesh,
|
||||||
|
PhysicsShapeType.CYLINDER,
|
||||||
|
{friction: 0, center: Vector3.Zero(), mass: 50, restitution: .01},
|
||||||
|
scene);
|
||||||
|
rigAggregate.body.setMotionType(PhysicsMotionType.DYNAMIC);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return rigMesh;
|
return rigMesh;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,7 +35,7 @@ export async function buildColor(color: Color3, scene: Scene, parent: TransformN
|
|||||||
colorBoxMesh.position.y = -Math.floor(index / rowLength) * .1;
|
colorBoxMesh.position.y = -Math.floor(index / rowLength) * .1;
|
||||||
|
|
||||||
colorBoxMesh.parent = parent;
|
colorBoxMesh.parent = parent;
|
||||||
colorBoxMesh.metadata = {tool: 'color'};
|
colorBoxMesh.metadata = {tool: 'color', tools: []};
|
||||||
|
|
||||||
let i = 0;
|
let i = 0;
|
||||||
const tools = [];
|
const tools = [];
|
||||||
@ -48,7 +48,9 @@ export async function buildColor(color: Color3, scene: Scene, parent: TransformN
|
|||||||
toolMap.set(newItem.id, newItem);
|
toolMap.set(newItem.id, newItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (colorBoxMesh.metadata) {
|
||||||
colorBoxMesh.metadata.tools = tools;
|
colorBoxMesh.metadata.tools = tools;
|
||||||
|
}
|
||||||
return colorBoxMesh;
|
return colorBoxMesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
src/vrApp.ts
12
src/vrApp.ts
@ -15,13 +15,14 @@ import {CustomEnvironment} from "./util/customEnvironment";
|
|||||||
import {Spinner} from "./objects/spinner";
|
import {Spinner} from "./objects/spinner";
|
||||||
import {addSceneInspector} from "./util/functions/sceneInspector";
|
import {addSceneInspector} from "./util/functions/sceneInspector";
|
||||||
import {groundMeshObserver} from "./util/functions/groundMeshObserver";
|
import {groundMeshObserver} from "./util/functions/groundMeshObserver";
|
||||||
|
import {initializeEngine} from "./vrcore/initializeEngine";
|
||||||
import {DefaultScene} from "./defaultScene";
|
import {DefaultScene} from "./defaultScene";
|
||||||
import {Introduction} from "./tutorial/introduction";
|
import {Introduction} from "./tutorial/introduction";
|
||||||
import {PouchData} from "./integration/database/pouchData";
|
import {PouchData} from "./integration/database/pouchData";
|
||||||
|
|
||||||
const webGpu = false;
|
const webGpu = false;
|
||||||
|
|
||||||
log.setLevel('error', false);
|
log.setLevel('debug', false);
|
||||||
log.getLogger('PouchdbPersistenceManager').setLevel('debug', false);
|
log.getLogger('PouchdbPersistenceManager').setLevel('debug', false);
|
||||||
export default class VrApp {
|
export default class VrApp {
|
||||||
//preTasks = [havokModule];
|
//preTasks = [havokModule];
|
||||||
@ -36,7 +37,14 @@ export default class VrApp {
|
|||||||
this._canvas = canvas;
|
this._canvas = canvas;
|
||||||
this._dbName = dbname;
|
this._dbName = dbname;
|
||||||
console.log('VrApp constructor');
|
console.log('VrApp constructor');
|
||||||
this.initializeEngine().then(() => {
|
initializeEngine({
|
||||||
|
canvas: this._canvas,
|
||||||
|
useWebGpu: webGpu,
|
||||||
|
onSceneReady: async (scene) => {
|
||||||
|
await this.initialize(scene);
|
||||||
|
}
|
||||||
|
}).then(engine => {
|
||||||
|
this._engine = engine;
|
||||||
this.logger.info('Engine initialized');
|
this.logger.info('Engine initialized');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
41
src/vrcore/initializeEngine.ts
Normal file
41
src/vrcore/initializeEngine.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import {
|
||||||
|
Color3, Engine, Scene, WebGPUEngine
|
||||||
|
} from "@babylonjs/core";
|
||||||
|
import { DefaultScene } from "../defaultScene";
|
||||||
|
import log from "loglevel";
|
||||||
|
|
||||||
|
export interface EngineInitializerParams {
|
||||||
|
canvas: HTMLCanvasElement;
|
||||||
|
useWebGpu: boolean;
|
||||||
|
onSceneReady: (scene: Scene) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function initializeEngine(params: EngineInitializerParams): Promise<Engine | WebGPUEngine> {
|
||||||
|
const logger = log.getLogger('EngineInitializer');
|
||||||
|
|
||||||
|
if (!params.canvas) {
|
||||||
|
logger.error('Canvas not found');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let engine = null;
|
||||||
|
if (params.useWebGpu) {
|
||||||
|
engine = new WebGPUEngine(params.canvas);
|
||||||
|
await (engine as WebGPUEngine).initAsync();
|
||||||
|
} else {
|
||||||
|
engine = new Engine(params.canvas, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.setHardwareScalingLevel(1 / window.devicePixelRatio);
|
||||||
|
const scene = new Scene(engine);
|
||||||
|
DefaultScene.Scene = scene;
|
||||||
|
scene.ambientColor = new Color3(.1, .1, .1);
|
||||||
|
|
||||||
|
await params.onSceneReady(scene);
|
||||||
|
|
||||||
|
engine.runRenderLoop(() => {
|
||||||
|
scene.render();
|
||||||
|
});
|
||||||
|
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user