Fixes for desktop/cardboard use

This commit is contained in:
Tony Garnock-Jones 2023-01-05 10:11:13 +01:00
parent a36f696389
commit 0e6ae0b958
1 changed files with 26 additions and 18 deletions

View File

@ -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());
}
}
}