UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

333 lines (332 loc) 8.26 kB
import mat4 from "../glmatrix/mat4.mjs"; import vec3 from "../glmatrix/vec3.mjs"; import quat from "../glmatrix/quat.mjs"; import mat3 from "../glmatrix/mat3.mjs"; import Vector3 from "./Vector3.mjs"; var Matrix4 = function() { this._axisX = new Vector3(); this._axisY = new Vector3(); this._axisZ = new Vector3(); this.array = mat4.create(); this._dirty = true; }; Matrix4.prototype = { constructor: Matrix4, setArray: function(arr) { for (var i = 0; i < this.array.length; i++) { this.array[i] = arr[i]; } this._dirty = true; return this; }, adjoint: function() { mat4.adjoint(this.array, this.array); this._dirty = true; return this; }, clone: function() { return new Matrix4().copy(this); }, copy: function(a) { mat4.copy(this.array, a.array); this._dirty = true; return this; }, determinant: function() { return mat4.determinant(this.array); }, fromQuat: function(q) { mat4.fromQuat(this.array, q.array); this._dirty = true; return this; }, fromRotationTranslation: function(q, v) { mat4.fromRotationTranslation(this.array, q.array, v.array); this._dirty = true; return this; }, fromMat2d: function(m2d) { Matrix4.fromMat2d(this, m2d); return this; }, frustum: function(left, right, bottom, top, near, far) { mat4.frustum(this.array, left, right, bottom, top, near, far); this._dirty = true; return this; }, identity: function() { mat4.identity(this.array); this._dirty = true; return this; }, invert: function() { mat4.invert(this.array, this.array); this._dirty = true; return this; }, lookAt: function(eye, center, up) { mat4.lookAt(this.array, eye.array, center.array, up.array); this._dirty = true; return this; }, mul: function(b) { mat4.mul(this.array, this.array, b.array); this._dirty = true; return this; }, mulLeft: function(a) { mat4.mul(this.array, a.array, this.array); this._dirty = true; return this; }, multiply: function(b) { mat4.multiply(this.array, this.array, b.array); this._dirty = true; return this; }, multiplyLeft: function(a) { mat4.multiply(this.array, a.array, this.array); this._dirty = true; return this; }, ortho: function(left, right, bottom, top, near, far) { mat4.ortho(this.array, left, right, bottom, top, near, far); this._dirty = true; return this; }, perspective: function(fovy, aspect, near, far) { mat4.perspective(this.array, fovy, aspect, near, far); this._dirty = true; return this; }, rotate: function(rad, axis) { mat4.rotate(this.array, this.array, rad, axis.array); this._dirty = true; return this; }, rotateX: function(rad) { mat4.rotateX(this.array, this.array, rad); this._dirty = true; return this; }, rotateY: function(rad) { mat4.rotateY(this.array, this.array, rad); this._dirty = true; return this; }, rotateZ: function(rad) { mat4.rotateZ(this.array, this.array, rad); this._dirty = true; return this; }, scale: function(v) { mat4.scale(this.array, this.array, v.array); this._dirty = true; return this; }, translate: function(v) { mat4.translate(this.array, this.array, v.array); this._dirty = true; return this; }, transpose: function() { mat4.transpose(this.array, this.array); this._dirty = true; return this; }, decomposeMatrix: function() { var x = vec3.create(); var y = vec3.create(); var z = vec3.create(); var m3 = mat3.create(); return function(scale, rotation, position) { var el = this.array; vec3.set(x, el[0], el[1], el[2]); vec3.set(y, el[4], el[5], el[6]); vec3.set(z, el[8], el[9], el[10]); var sx = vec3.length(x); var sy = vec3.length(y); var sz = vec3.length(z); var det = this.determinant(); if (det < 0) { sx = -sx; } if (scale) { scale.set(sx, sy, sz); } position.set(el[12], el[13], el[14]); mat3.fromMat4(m3, el); m3[0] /= sx; m3[1] /= sx; m3[2] /= sx; m3[3] /= sy; m3[4] /= sy; m3[5] /= sy; m3[6] /= sz; m3[7] /= sz; m3[8] /= sz; quat.fromMat3(rotation.array, m3); quat.normalize(rotation.array, rotation.array); rotation._dirty = true; position._dirty = true; }; }(), toString: function() { return "[" + Array.prototype.join.call(this.array, ",") + "]"; }, toArray: function() { return Array.prototype.slice.call(this.array); } }; var defineProperty = Object.defineProperty; if (defineProperty) { var proto = Matrix4.prototype; defineProperty(proto, "z", { get: function() { var el = this.array; this._axisZ.set(el[8], el[9], el[10]); return this._axisZ; }, set: function(v) { var el = this.array; v = v.array; el[8] = v[0]; el[9] = v[1]; el[10] = v[2]; this._dirty = true; } }); defineProperty(proto, "y", { get: function() { var el = this.array; this._axisY.set(el[4], el[5], el[6]); return this._axisY; }, set: function(v) { var el = this.array; v = v.array; el[4] = v[0]; el[5] = v[1]; el[6] = v[2]; this._dirty = true; } }); defineProperty(proto, "x", { get: function() { var el = this.array; this._axisX.set(el[0], el[1], el[2]); return this._axisX; }, set: function(v) { var el = this.array; v = v.array; el[0] = v[0]; el[1] = v[1]; el[2] = v[2]; this._dirty = true; } }); } Matrix4.adjoint = function(out, a) { mat4.adjoint(out.array, a.array); out._dirty = true; return out; }; Matrix4.copy = function(out, a) { mat4.copy(out.array, a.array); out._dirty = true; return out; }; Matrix4.determinant = function(a) { return mat4.determinant(a.array); }; Matrix4.identity = function(out) { mat4.identity(out.array); out._dirty = true; return out; }; Matrix4.ortho = function(out, left, right, bottom, top, near, far) { mat4.ortho(out.array, left, right, bottom, top, near, far); out._dirty = true; return out; }; Matrix4.perspective = function(out, fovy, aspect, near, far) { mat4.perspective(out.array, fovy, aspect, near, far); out._dirty = true; return out; }; Matrix4.lookAt = function(out, eye, center, up) { mat4.lookAt(out.array, eye.array, center.array, up.array); out._dirty = true; return out; }; Matrix4.invert = function(out, a) { mat4.invert(out.array, a.array); out._dirty = true; return out; }; Matrix4.mul = function(out, a, b) { mat4.mul(out.array, a.array, b.array); out._dirty = true; return out; }; Matrix4.multiply = Matrix4.mul; Matrix4.fromQuat = function(out, q) { mat4.fromQuat(out.array, q.array); out._dirty = true; return out; }; Matrix4.fromRotationTranslation = function(out, q, v) { mat4.fromRotationTranslation(out.array, q.array, v.array); out._dirty = true; return out; }; Matrix4.fromMat2d = function(m4, m2d) { m4._dirty = true; var m2d = m2d.array; var m4 = m4.array; m4[0] = m2d[0]; m4[4] = m2d[2]; m4[12] = m2d[4]; m4[1] = m2d[1]; m4[5] = m2d[3]; m4[13] = m2d[5]; return m4; }; Matrix4.rotate = function(out, a, rad, axis) { mat4.rotate(out.array, a.array, rad, axis.array); out._dirty = true; return out; }; Matrix4.rotateX = function(out, a, rad) { mat4.rotateX(out.array, a.array, rad); out._dirty = true; return out; }; Matrix4.rotateY = function(out, a, rad) { mat4.rotateY(out.array, a.array, rad); out._dirty = true; return out; }; Matrix4.rotateZ = function(out, a, rad) { mat4.rotateZ(out.array, a.array, rad); out._dirty = true; return out; }; Matrix4.scale = function(out, a, v) { mat4.scale(out.array, a.array, v.array); out._dirty = true; return out; }; Matrix4.transpose = function(out, a) { mat4.transpose(out.array, a.array); out._dirty = true; return out; }; Matrix4.translate = function(out, a, v) { mat4.translate(out.array, a.array, v.array); out._dirty = true; return out; }; var Matrix4$1 = Matrix4; export { Matrix4$1 as default };