Skip to content

Commit 5236793

Browse files
committed
device-orientation-look: Property type and optimize math
Signed-off-by: Squareys <[email protected]>
1 parent a77cf52 commit 5236793

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

device-orientation-look.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import {Component} from '@wonderlandengine/api';
33
/**
44
* Function to convert a Euler in YXZ order to a quaternion
55
*/
6-
function quatFromEulerYXZ(out, x, y, z) {
7-
const c1 = Math.cos(x / 2);
8-
const c2 = Math.cos(y / 2);
9-
const c3 = Math.cos(z / 2);
6+
function quatFromEulerYXZDeg(out: number[], x: number, y: number, z: number) {
7+
const halfToRad = (0.5 * Math.PI) / 180.0;
8+
x *= halfToRad;
9+
y *= halfToRad;
10+
z *= halfToRad;
1011

11-
const s1 = Math.sin(x / 2);
12-
const s2 = Math.sin(y / 2);
13-
const s3 = Math.sin(z / 2);
12+
const c1 = Math.cos(x);
13+
const c2 = Math.cos(y);
14+
const c3 = Math.cos(z);
15+
16+
const s1 = Math.sin(x);
17+
const s2 = Math.sin(y);
18+
const s3 = Math.sin(z);
1419

1520
out[0] = s1 * c2 * c3 + c1 * s2 * s3;
1621
out[1] = c1 * s2 * c3 - s1 * c2 * s3;
@@ -68,19 +73,13 @@ export class DeviceOrientationLook extends Component {
6873
}
6974

7075
onDeviceOrientation = (e: DeviceOrientationEvent) => {
71-
let alpha = e.alpha || 0;
72-
let beta = e.beta || 0;
73-
let gamma = e.gamma || 0;
74-
const toRad = Math.PI / 180;
75-
quatFromEulerYXZ(
76-
this.deviceOrientation,
77-
beta * toRad,
78-
alpha * toRad,
79-
-gamma * toRad
80-
);
76+
let alpha = e.alpha ?? 0;
77+
let beta = e.beta ?? 0;
78+
let gamma = e.gamma ?? 0;
79+
quatFromEulerYXZDeg(this.deviceOrientation, beta, alpha, -gamma);
8180
};
8281

83-
onOrientationChange = (e: Event) => {
82+
onOrientationChange = () => {
8483
this.screenOrientation =
8584
window.screen?.orientation.angle ?? window.orientation ?? 0;
8685
};

0 commit comments

Comments
 (0)