Skip to content

Commit

Permalink
feat(playcanvas): operator for Vec4
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Jun 25, 2020
1 parent 2d7f198 commit 5f84b3d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ add `https://unpkg.com/@js-basics/vector/build/iife/adapter/playcanvas.min.js` t
- `pc.cross(...)` short notation `Vec3().cross`
- `pc.vec3(...)` short notation `new pc.Vec3(...)`
- `pc.vec2(...)` short notation `new pc.Vec2(...)`
- `pc.vec4(...)` short notation `new pc.Vec4(...)`
- `Vec3().len` getter for `Vec3().length()`
- `Vec2().len` getter for `Vec2().length()`
- `Vec4().len` getter for `Vec4().length()`

### pc.calc()

Expand Down
21 changes: 20 additions & 1 deletion src/adapter/playcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function fallbackWindow() {
}
export function hijackPlayCanvas(pc) {
const {
Vec2, Vec3, Quat, Mat4, math
Vec2, Vec3, Vec4, Quat, Mat4, math
} = pc;
const {
LEFT, FORWARD, UP, RIGHT, ZERO
Expand All @@ -37,6 +37,16 @@ export function hijackPlayCanvas(pc) {
return vec3Factory(x, y, z);
};

cachedValueOf(Vec4);
defineVectorLength(Vec4, 4);
const vec4Factory = cachedFactory(Vec4);
pc.vec4 = (x, y, z, w) => {
if (typeof x === 'function') {
return operatorCalc(x, new Vec4());
}
return vec4Factory(x, y, z, w);
};

pc.calc = operatorCalc;

Object.defineProperty(Quat.prototype, 'left', {
Expand Down Expand Up @@ -134,6 +144,15 @@ export function hijackPlayCanvas(pc) {
throw new Error('set len not allowed');
}
});

Object.defineProperty(Vec4.prototype, 'len', {
get() {
return this.length();
},
set() {
throw new Error('set len not allowed');
}
});
}

// eslint-disable-next-line no-undef
Expand Down

0 comments on commit 5f84b3d

Please sign in to comment.