diff --git a/src/engine.ts b/src/engine.ts index 59e2fe3..62e6ded 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -73,7 +73,7 @@ export async function startEngine( const { scene, floorMeshes } = await createScene(canvas, engine); const xr = await scene.createDefaultXRExperienceAsync({}); - const xrAvailable = xr.baseExperience !== void 0; + const xrAvailable = xr?.baseExperience !== void 0; let camera: FreeCamera; if (xrAvailable) { @@ -120,20 +120,22 @@ export async function startEngine( for (const gp of Array.from(navigator.getGamepads())) { if (gp !== null) { - const pos = new Vector3((gp.axes[0]), (-gp.axes[1]), (-gp.axes[3])); - if (pos.length() > 0.0625) { - if (leanBase === null) { - leanBase = { position: xr.baseExperience.camera.position }; + if (sm) { + const pos = new Vector3((gp.axes[0]), (-gp.axes[1]), (-gp.axes[3])); + if (pos.length() > 0.0625) { + if (leanBase === null) { + leanBase = { position: xr.baseExperience.camera.position }; + } + xr.baseExperience.camera.position = + pos.applyRotationQuaternion(xr.baseExperience.camera.absoluteRotation) + .scale(0.25) + .add(leanBase.position); + } else { + if (leanBase !== null) { + xr.baseExperience.camera.position = leanBase.position; + } + leanBase = null; } - xr.baseExperience.camera.position = - pos.applyRotationQuaternion(xr.baseExperience.camera.absoluteRotation) - .scale(0.25) - .add(leanBase.position); - } else { - if (leanBase !== null) { - xr.baseExperience.camera.position = leanBase.position; - } - leanBase = null; } if (sm && latch(gp, 0)) { @@ -158,12 +160,18 @@ export async function startEngine( if (sm && latch(gp, 3)) { enableVR(); } - if (sm && latch(gp, 5)) { - const q = xr.baseExperience.camera.rotationQuaternion; - const r = q.toEulerAngles(); + if (latch(gp, 5)) { + const r = sm + ? xr.baseExperience.camera.rotationQuaternion.toEulerAngles() + : camera.rotation; r.y += Math.PI; r.y %= 2 * Math.PI; - q.copyFrom(Quaternion.FromEulerAngles(r.x, r.y, r.z)); + if (sm) { + xr.baseExperience.camera.rotationQuaternion.copyFrom(r.toQuaternion()); + } + } + if (sm && latch(gp, 4)) { + xr.baseExperience.camera.rotationQuaternion.copyFrom(initialRotation.toQuaternion()); } } }