From ee31b0f7451af2b931243d40cad6e3caa7409b08 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Thu, 21 Nov 2024 14:34:17 +0000 Subject: [PATCH 1/5] Make camera controls script play nice with XR --- scripts/camera-controls.mjs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/scripts/camera-controls.mjs b/scripts/camera-controls.mjs index 42aa17194e9..85cd08ebb0e 100644 --- a/scripts/camera-controls.mjs +++ b/scripts/camera-controls.mjs @@ -1,4 +1,4 @@ -import { Vec2, Vec3, Ray, Plane, Entity, Script, math } from 'playcanvas'; +import { math, Entity, Plane, Quat, Ray, Script, Vec2, Vec3 } from 'playcanvas'; /** @import { CameraComponent } from 'playcanvas' */ @@ -308,6 +308,17 @@ class CameraControls extends Script { this.pitchRange = pitchRange ?? this.pitchRange; this.zoomMin = zoomMin ?? this.zoomMin; this.zoomMax = zoomMax ?? this.zoomMax; + + const position = new Vec3(); + const rotation = new Quat(); + this.app.xr.on('start', () => { + position.copy(this.entity.getPosition()); + rotation.copy(this.entity.getRotation()); + }); + this.app.xr.on('end', () => { + this.entity.setPosition(position); + this.entity.setRotation(rotation); + }); } /** @@ -943,6 +954,10 @@ class CameraControls extends Script { * @param {number} dt - The delta time. */ update(dt) { + if (this.app.xr.active) { + return; + } + if (!this._camera) { return; } From ee9d943394d55b5bbd07447c814a491e2b43e662 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Thu, 28 Nov 2024 10:37:25 +0000 Subject: [PATCH 2/5] Set camera height to zero on entering XR --- scripts/esm/camera-controls.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/esm/camera-controls.mjs b/scripts/esm/camera-controls.mjs index fd960d52ccb..bc555133f18 100644 --- a/scripts/esm/camera-controls.mjs +++ b/scripts/esm/camera-controls.mjs @@ -1,5 +1,5 @@ /* eslint-disable-next-line import/no-unresolved */ -import { Vec2, Vec3, Ray, Plane, Mat4, Quat, Script, math } from 'playcanvas'; +import { math, Mat4, Plane, Quat, Ray, Script, Vec2, Vec3 } from 'playcanvas'; /** @import { CameraComponent } from 'playcanvas' */ @@ -319,10 +319,16 @@ class CameraControls extends Script { const position = new Vec3(); const rotation = new Quat(); this.app.xr.on('start', () => { + // Store the position and rotation of the camera position.copy(this.entity.getPosition()); rotation.copy(this.entity.getRotation()); + + // Set the camera's position to the ground + const pos = this.entity.getPosition(); + this.entity.setPosition(pos.x, 0, pos.z); }); this.app.xr.on('end', () => { + // Restore the camera's position and rotation this.entity.setPosition(position); this.entity.setRotation(rotation); }); From dc8b39ba92e2d6370426d2b8243924fbf9782f05 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 3 Dec 2024 11:35:22 +0000 Subject: [PATCH 3/5] Update camera-controls.mjs --- scripts/esm/camera-controls.mjs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/scripts/esm/camera-controls.mjs b/scripts/esm/camera-controls.mjs index 429f258db02..169a907a852 100644 --- a/scripts/esm/camera-controls.mjs +++ b/scripts/esm/camera-controls.mjs @@ -319,13 +319,9 @@ class CameraControls extends Script { const position = new Vec3(); const rotation = new Quat(); this.app.xr.on('start', () => { - // Store the position and rotation of the camera + // Store the camera's position and rotation position.copy(this.entity.getPosition()); rotation.copy(this.entity.getRotation()); - - // Set the camera's position to the ground - const pos = this.entity.getPosition(); - this.entity.setPosition(pos.x, 0, pos.z); }); this.app.xr.on('end', () => { // Restore the camera's position and rotation From 05e7be8c8f2f54c50bd83507efd2f3a29777619e Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 3 Dec 2024 12:42:32 +0000 Subject: [PATCH 4/5] Update camera-controls.mjs --- scripts/esm/camera-controls.mjs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/scripts/esm/camera-controls.mjs b/scripts/esm/camera-controls.mjs index 169a907a852..868a76a3dad 100644 --- a/scripts/esm/camera-controls.mjs +++ b/scripts/esm/camera-controls.mjs @@ -1,5 +1,5 @@ /* eslint-disable-next-line import/no-unresolved */ -import { math, Mat4, Plane, Quat, Ray, Script, Vec2, Vec3 } from 'playcanvas'; +import { math, Mat4, Plane, Ray, Script, Vec2, Vec3 } from 'playcanvas'; /** @import { CameraComponent } from 'playcanvas' */ @@ -315,19 +315,6 @@ class CameraControls extends Script { this.pitchRange = pitchRange ?? this.pitchRange; this.zoomMin = zoomMin ?? this.zoomMin; this.zoomMax = zoomMax ?? this.zoomMax; - - const position = new Vec3(); - const rotation = new Quat(); - this.app.xr.on('start', () => { - // Store the camera's position and rotation - position.copy(this.entity.getPosition()); - rotation.copy(this.entity.getRotation()); - }); - this.app.xr.on('end', () => { - // Restore the camera's position and rotation - this.entity.setPosition(position); - this.entity.setRotation(rotation); - }); } /** From 2a9be2bd575da4dc28373206b1f8cb6f1d56cae3 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 3 Dec 2024 12:43:41 +0000 Subject: [PATCH 5/5] Update camera-controls.mjs --- scripts/esm/camera-controls.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/esm/camera-controls.mjs b/scripts/esm/camera-controls.mjs index 868a76a3dad..9d1c995f655 100644 --- a/scripts/esm/camera-controls.mjs +++ b/scripts/esm/camera-controls.mjs @@ -1,5 +1,5 @@ /* eslint-disable-next-line import/no-unresolved */ -import { math, Mat4, Plane, Ray, Script, Vec2, Vec3 } from 'playcanvas'; +import { Vec2, Vec3, Ray, Plane, Mat4, Quat, Script, math } from 'playcanvas'; /** @import { CameraComponent } from 'playcanvas' */