Skip to content

Commit a1731e0

Browse files
committed
fix(playcanvas): setFromOrientation
1 parent f086695 commit a1731e0

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/adapter/playcanvas.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {
33
operatorCalc, cachedValueOf, defineVectorLength, cachedFactory, cachedFunction
44
} from '../operator';
5+
import { fromEulerYXZ } from '../util';
56

67
function fallbackWindow() {
78
return {
@@ -10,7 +11,7 @@ function fallbackWindow() {
1011
}
1112
export function hijackPlayCanvas(pc) {
1213
const {
13-
Vec2, Vec3, Quat, Mat4
14+
Vec2, Vec3, Quat, Mat4, math
1415
} = pc;
1516

1617
cachedValueOf(Vec2);
@@ -60,14 +61,34 @@ export function hijackPlayCanvas(pc) {
6061
}
6162
});
6263
pc.quat = cachedFunction((x, y, z, w) => {
63-
if (typeof dir === 'number') {
64+
if (typeof x === 'number') {
6465
return new Quat(x, y, z, w);
6566
}
6667
if (!x) {
6768
return new Quat();
6869
}
70+
if (typeof y === 'number') {
71+
return new Quat().setFromAxisAngle(x, y);
72+
}
6973
return new Quat().setFromMat4(new Mat4().setLookAt(Vec3.ZERO, x, y || Vec3.UP));
7074
});
75+
76+
const LEFT90 = pc.quat(Vec3.LEFT, 90);
77+
78+
Quat.prototype.setFromOrientation = function ({ alpha, beta, gamma }, orientation) {
79+
const x = beta * math.DEG_TO_RAD;
80+
const y = alpha * math.DEG_TO_RAD;
81+
const z = -gamma * math.DEG_TO_RAD;
82+
let rot = pc.quat(fromEulerYXZ(x, y, z));
83+
rot.mul(LEFT90);
84+
85+
if (orientation) {
86+
const { dir } = rot;
87+
const local = pc.quat(dir, orientation);
88+
rot = local.mul(rot);
89+
}
90+
this.copy(rot);
91+
};
7192
}
7293

7394
// eslint-disable-next-line no-undef

test/adapter/playcanvas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const pc = {
2424
Quat,
2525
};
2626

27-
describe('override valueOf of a new class', () => {
27+
describe('override valueOf of playcanvas Vec3', () => {
2828
it('calculations with the new class', () => {
2929
hijackPlayCanvas(pc);
3030

0 commit comments

Comments
 (0)