cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
359 lines (358 loc) • 9.04 kB
JavaScript
import quat from "../glmatrix/quat.mjs";
import mat3 from "../glmatrix/mat3.mjs";
var Quaternion = function(x, y, z, w) {
x = x || 0;
y = y || 0;
z = z || 0;
w = w === void 0 ? 1 : w;
this.array = quat.fromValues(x, y, z, w);
this._dirty = true;
};
Quaternion.prototype = {
constructor: Quaternion,
add: function(b) {
quat.add(this.array, this.array, b.array);
this._dirty = true;
return this;
},
calculateW: function() {
quat.calculateW(this.array, this.array);
this._dirty = true;
return this;
},
set: function(x, y, z, w) {
this.array[0] = x;
this.array[1] = y;
this.array[2] = z;
this.array[3] = w;
this._dirty = true;
return this;
},
setArray: function(arr) {
this.array[0] = arr[0];
this.array[1] = arr[1];
this.array[2] = arr[2];
this.array[3] = arr[3];
this._dirty = true;
return this;
},
clone: function() {
return new Quaternion(this.x, this.y, this.z, this.w);
},
conjugate: function() {
quat.conjugate(this.array, this.array);
this._dirty = true;
return this;
},
copy: function(b) {
quat.copy(this.array, b.array);
this._dirty = true;
return this;
},
dot: function(b) {
return quat.dot(this.array, b.array);
},
fromMat3: function(m) {
quat.fromMat3(this.array, m.array);
this._dirty = true;
return this;
},
fromMat4: function() {
var m3 = mat3.create();
return function(m) {
mat3.fromMat4(m3, m.array);
mat3.transpose(m3, m3);
quat.fromMat3(this.array, m3);
this._dirty = true;
return this;
};
}(),
identity: function() {
quat.identity(this.array);
this._dirty = true;
return this;
},
invert: function() {
quat.invert(this.array, this.array);
this._dirty = true;
return this;
},
len: function() {
return quat.len(this.array);
},
length: function() {
return quat.length(this.array);
},
lerp: function(a, b, t) {
quat.lerp(this.array, a.array, b.array, t);
this._dirty = true;
return this;
},
mul: function(b) {
quat.mul(this.array, this.array, b.array);
this._dirty = true;
return this;
},
mulLeft: function(a) {
quat.multiply(this.array, a.array, this.array);
this._dirty = true;
return this;
},
multiply: function(b) {
quat.multiply(this.array, this.array, b.array);
this._dirty = true;
return this;
},
multiplyLeft: function(a) {
quat.multiply(this.array, a.array, this.array);
this._dirty = true;
return this;
},
normalize: function() {
quat.normalize(this.array, this.array);
this._dirty = true;
return this;
},
rotateX: function(rad) {
quat.rotateX(this.array, this.array, rad);
this._dirty = true;
return this;
},
rotateY: function(rad) {
quat.rotateY(this.array, this.array, rad);
this._dirty = true;
return this;
},
rotateZ: function(rad) {
quat.rotateZ(this.array, this.array, rad);
this._dirty = true;
return this;
},
rotationTo: function(a, b) {
quat.rotationTo(this.array, a.array, b.array);
this._dirty = true;
return this;
},
setAxes: function(view, right, up) {
quat.setAxes(this.array, view.array, right.array, up.array);
this._dirty = true;
return this;
},
setAxisAngle: function(axis, rad) {
quat.setAxisAngle(this.array, axis.array, rad);
this._dirty = true;
return this;
},
slerp: function(a, b, t) {
quat.slerp(this.array, a.array, b.array, t);
this._dirty = true;
return this;
},
sqrLen: function() {
return quat.sqrLen(this.array);
},
squaredLength: function() {
return quat.squaredLength(this.array);
},
fromEuler: function(v, order) {
return Quaternion.fromEuler(this, v, order);
},
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 = Quaternion.prototype;
defineProperty(proto, "x", {
get: function() {
return this.array[0];
},
set: function(value) {
this.array[0] = value;
this._dirty = true;
}
});
defineProperty(proto, "y", {
get: function() {
return this.array[1];
},
set: function(value) {
this.array[1] = value;
this._dirty = true;
}
});
defineProperty(proto, "z", {
get: function() {
return this.array[2];
},
set: function(value) {
this.array[2] = value;
this._dirty = true;
}
});
defineProperty(proto, "w", {
get: function() {
return this.array[3];
},
set: function(value) {
this.array[3] = value;
this._dirty = true;
}
});
}
Quaternion.add = function(out, a, b) {
quat.add(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Quaternion.set = function(out, x, y, z, w) {
quat.set(out.array, x, y, z, w);
out._dirty = true;
};
Quaternion.copy = function(out, b) {
quat.copy(out.array, b.array);
out._dirty = true;
return out;
};
Quaternion.calculateW = function(out, a) {
quat.calculateW(out.array, a.array);
out._dirty = true;
return out;
};
Quaternion.conjugate = function(out, a) {
quat.conjugate(out.array, a.array);
out._dirty = true;
return out;
};
Quaternion.identity = function(out) {
quat.identity(out.array);
out._dirty = true;
return out;
};
Quaternion.invert = function(out, a) {
quat.invert(out.array, a.array);
out._dirty = true;
return out;
};
Quaternion.dot = function(a, b) {
return quat.dot(a.array, b.array);
};
Quaternion.len = function(a) {
return quat.length(a.array);
};
Quaternion.lerp = function(out, a, b, t) {
quat.lerp(out.array, a.array, b.array, t);
out._dirty = true;
return out;
};
Quaternion.slerp = function(out, a, b, t) {
quat.slerp(out.array, a.array, b.array, t);
out._dirty = true;
return out;
};
Quaternion.mul = function(out, a, b) {
quat.multiply(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Quaternion.multiply = Quaternion.mul;
Quaternion.rotateX = function(out, a, rad) {
quat.rotateX(out.array, a.array, rad);
out._dirty = true;
return out;
};
Quaternion.rotateY = function(out, a, rad) {
quat.rotateY(out.array, a.array, rad);
out._dirty = true;
return out;
};
Quaternion.rotateZ = function(out, a, rad) {
quat.rotateZ(out.array, a.array, rad);
out._dirty = true;
return out;
};
Quaternion.setAxisAngle = function(out, axis, rad) {
quat.setAxisAngle(out.array, axis.array, rad);
out._dirty = true;
return out;
};
Quaternion.normalize = function(out, a) {
quat.normalize(out.array, a.array);
out._dirty = true;
return out;
};
Quaternion.sqrLen = function(a) {
return quat.sqrLen(a.array);
};
Quaternion.squaredLength = Quaternion.sqrLen;
Quaternion.fromMat3 = function(out, m) {
quat.fromMat3(out.array, m.array);
out._dirty = true;
return out;
};
Quaternion.setAxes = function(out, view, right, up) {
quat.setAxes(out.array, view.array, right.array, up.array);
out._dirty = true;
return out;
};
Quaternion.rotationTo = function(out, a, b) {
quat.rotationTo(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Quaternion.fromEuler = function(out, v, order) {
out._dirty = true;
v = v.array;
var target = out.array;
var c1 = Math.cos(v[0] / 2);
var c2 = Math.cos(v[1] / 2);
var c3 = Math.cos(v[2] / 2);
var s1 = Math.sin(v[0] / 2);
var s2 = Math.sin(v[1] / 2);
var s3 = Math.sin(v[2] / 2);
var order = (order || "XYZ").toUpperCase();
switch (order) {
case "XYZ":
target[0] = s1 * c2 * c3 + c1 * s2 * s3;
target[1] = c1 * s2 * c3 - s1 * c2 * s3;
target[2] = c1 * c2 * s3 + s1 * s2 * c3;
target[3] = c1 * c2 * c3 - s1 * s2 * s3;
break;
case "YXZ":
target[0] = s1 * c2 * c3 + c1 * s2 * s3;
target[1] = c1 * s2 * c3 - s1 * c2 * s3;
target[2] = c1 * c2 * s3 - s1 * s2 * c3;
target[3] = c1 * c2 * c3 + s1 * s2 * s3;
break;
case "ZXY":
target[0] = s1 * c2 * c3 - c1 * s2 * s3;
target[1] = c1 * s2 * c3 + s1 * c2 * s3;
target[2] = c1 * c2 * s3 + s1 * s2 * c3;
target[3] = c1 * c2 * c3 - s1 * s2 * s3;
break;
case "ZYX":
target[0] = s1 * c2 * c3 - c1 * s2 * s3;
target[1] = c1 * s2 * c3 + s1 * c2 * s3;
target[2] = c1 * c2 * s3 - s1 * s2 * c3;
target[3] = c1 * c2 * c3 + s1 * s2 * s3;
break;
case "YZX":
target[0] = s1 * c2 * c3 + c1 * s2 * s3;
target[1] = c1 * s2 * c3 + s1 * c2 * s3;
target[2] = c1 * c2 * s3 - s1 * s2 * c3;
target[3] = c1 * c2 * c3 - s1 * s2 * s3;
break;
case "XZY":
target[0] = s1 * c2 * c3 - c1 * s2 * s3;
target[1] = c1 * s2 * c3 - s1 * c2 * s3;
target[2] = c1 * c2 * s3 + s1 * s2 * c3;
target[3] = c1 * c2 * c3 + s1 * s2 * s3;
break;
}
};
var Quaternion$1 = Quaternion;
export { Quaternion$1 as default };