r3-legacy/src/game-lib-quaternion.js

141 lines
2.9 KiB
JavaScript

/**
* Runtime quaternion for updating instance objects
* @param graphics GameLib.D3.Graphics
* @param parentObject GameLib.D3.*
* @param quaternion GameLib.D3.API.Quaternion
* @param grain Number
* @constructor
*/
GameLib.D3.Quaternion = function RuntimeQuaternion(
graphics,
parentObject,
quaternion,
grain
) {
for (var property in quaternion) {
if (quaternion.hasOwnProperty(property)) {
this[property] = quaternion[property];
}
}
GameLib.D3.Utils.Extend(GameLib.D3.Quaternion, GameLib.D3.API.Quaternion);
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.parentObject = parentObject;
if (GameLib.D3.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
}
this.grain = grain;
this.axis = new GameLib.D3.Vector3(
this.graphics,
this,
this.axis,
0.001
);
this.instance = this.createInstance();
};
/**
* Creates an instance quaternion
* @param update
* @returns {*}
*/
GameLib.D3.Quaternion.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
instance.x = this.x;
instance.y = this.y;
instance.z = this.z;
instance.w = this.w;
} else {
instance = new THREE.Quaternion(this.x, this.y, this.z, this.w);
}
return instance;
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
GameLib.D3.Quaternion.prototype.updateInstance = function() {
this.createInstance(true);
if (this.parentObject.updateInstance) {
this.parentObject.updateInstance();
}
};
/**
* Converts runtime quaternion to API quaternion
* @returns {*}
*/
GameLib.D3.Quaternion.prototype.toApiQuaternion = function() {
return new GameLib.D3.API.Quaternion(
this.x,
this.y,
this.z,
this.w,
this.axis.toApiVector(),
this.angle
);
};
GameLib.D3.Quaternion.prototype.normalize = function () {
this.updateInstance();
this.instance.normalize();
this.x = this.instance.x;
this.y = this.instance.y;
this.z = this.instance.z;
this.w = this.instance.w;
return this;
};
/**
*
* @param matrix4 GameLib.D3.Matrix4
*/
GameLib.D3.Quaternion.prototype.setFromRotationMatrix = function(matrix4) {
this.instance.setFromRotationMatrix(matrix4.instance);
this.x = this.instance.x;
this.y = this.instance.y;
this.z = this.instance.z;
this.w = this.instance.w;
};
/**
*
* @param quaternion GameLib.D3.Quaternion
* @param t
* @returns {GameLib.D3.Quaternion}
*/
GameLib.D3.Quaternion.prototype.slerp = function (quaternion, t) {
this.updateInstance();
this.instance.slerp(quaternion.instance, t);
this.x = this.instance.x;
this.y = this.instance.y;
this.z = this.instance.z;
this.w = this.instance.w;
return this;
};