Skip to content

Commit

Permalink
fix(playcanvas): setFromOrientation
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Sep 29, 2019
1 parent f086695 commit a1731e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/adapter/playcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
operatorCalc, cachedValueOf, defineVectorLength, cachedFactory, cachedFunction
} from '../operator';
import { fromEulerYXZ } from '../util';

function fallbackWindow() {
return {
Expand All @@ -10,7 +11,7 @@ function fallbackWindow() {
}
export function hijackPlayCanvas(pc) {
const {
Vec2, Vec3, Quat, Mat4
Vec2, Vec3, Quat, Mat4, math
} = pc;

cachedValueOf(Vec2);
Expand Down Expand Up @@ -60,14 +61,34 @@ export function hijackPlayCanvas(pc) {
}
});
pc.quat = cachedFunction((x, y, z, w) => {
if (typeof dir === 'number') {
if (typeof x === 'number') {
return new Quat(x, y, z, w);
}
if (!x) {
return new Quat();
}
if (typeof y === 'number') {
return new Quat().setFromAxisAngle(x, y);
}
return new Quat().setFromMat4(new Mat4().setLookAt(Vec3.ZERO, x, y || Vec3.UP));
});

const LEFT90 = pc.quat(Vec3.LEFT, 90);

Quat.prototype.setFromOrientation = function ({ alpha, beta, gamma }, orientation) {
const x = beta * math.DEG_TO_RAD;
const y = alpha * math.DEG_TO_RAD;
const z = -gamma * math.DEG_TO_RAD;
let rot = pc.quat(fromEulerYXZ(x, y, z));
rot.mul(LEFT90);

if (orientation) {
const { dir } = rot;
const local = pc.quat(dir, orientation);
rot = local.mul(rot);
}
this.copy(rot);
};
}

// eslint-disable-next-line no-undef
Expand Down
2 changes: 1 addition & 1 deletion test/adapter/playcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const pc = {
Quat,
};

describe('override valueOf of a new class', () => {
describe('override valueOf of playcanvas Vec3', () => {
it('calculations with the new class', () => {
hijackPlayCanvas(pc);

Expand Down

0 comments on commit a1731e0

Please sign in to comment.