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

109 lines
2.3 KiB
JavaScript

/**
* Runtime quaternion for updating instance objects
* @param graphics GameLib.D3.Graphics
* @param parentObject GameLib.D3.*
* @param apiQuaternion GameLib.API.Quaternion
* @param grain Number
* @constructor
*/
GameLib.Quaternion = function (
graphics,
apiQuaternion,
parentObject,
grain
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiQuaternion)) {
apiQuaternion = {};
}
GameLib.API.Quaternion.call(
this,
apiQuaternion.x,
apiQuaternion.y,
apiQuaternion.z,
apiQuaternion.w,
apiQuaternion.axis,
apiQuaternion.angle
);
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
this.axis = new GameLib.Vector3(
this.graphics,
this.axis,
this,
this.grain
);
if (GameLib.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
}
this.grain = grain;
this.instance = this.createInstance();
};
GameLib.Quaternion.prototype = Object.create(GameLib.API.Quaternion.prototype);
GameLib.Quaternion.prototype.constructor = GameLib.Quaternion;
/**
* Creates an instance quaternion
* @param update
* @returns {*}
*/
GameLib.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.Quaternion.prototype.updateInstance = function() {
this.createInstance(true);
if (this.parentObject &&
this.parentObject.updateInstance) {
this.parentObject.updateInstance();
}
};
/**
* Converts runtime quaternion to API quaternion
* @returns {*}
*/
GameLib.Quaternion.prototype.toApiQuaternion = function() {
return new GameLib.API.Quaternion(
this.x,
this.y,
this.z,
this.w,
this.axis.toApiVector(),
this.angle
);
};