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

141 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-12-01 18:37:57 +01:00
/**
2016-12-09 20:32:09 +01:00
* Runtime quaternion for updating instance objects
2016-12-01 18:37:57 +01:00
* @param graphics GameLib.D3.Graphics
* @param parentObject GameLib.D3.*
2016-12-09 20:32:09 +01:00
* @param quaternion GameLib.D3.API.Quaternion
2016-12-01 18:37:57 +01:00
* @param grain Number
* @constructor
*/
2016-12-09 20:32:09 +01:00
GameLib.D3.Quaternion = function RuntimeQuaternion(
graphics,
parentObject,
2016-12-09 20:32:09 +01:00
quaternion,
grain
) {
2016-12-01 18:37:57 +01:00
2016-12-09 20:32:09 +01:00
for (var property in quaternion) {
if (quaternion.hasOwnProperty(property)) {
this[property] = quaternion[property];
2016-12-01 18:37:57 +01:00
}
}
2016-12-09 20:32:09 +01:00
GameLib.D3.Utils.Extend(GameLib.D3.Quaternion, GameLib.D3.API.Quaternion);
2016-12-01 18:37:57 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.parentObject = parentObject;
if (GameLib.D3.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
}
this.grain = grain;
2016-12-09 20:32:09 +01:00
this.axis = new GameLib.D3.Vector3(
this.graphics,
this,
this.axis,
0.001
);
2016-12-01 18:37:57 +01:00
this.instance = this.createInstance();
};
/**
2016-12-09 20:32:09 +01:00
* Creates an instance quaternion
2016-12-01 18:37:57 +01:00
* @param update
* @returns {*}
*/
2016-12-09 20:32:09 +01:00
GameLib.D3.Quaternion.prototype.createInstance = function(update) {
2016-12-01 18:37:57 +01:00
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);
2016-12-01 18:37:57 +01:00
}
return instance;
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
2016-12-09 20:32:09 +01:00
GameLib.D3.Quaternion.prototype.updateInstance = function() {
2016-12-01 18:37:57 +01:00
this.createInstance(true);
if (this.parentObject.updateInstance) {
this.parentObject.updateInstance();
}
};
/**
2016-12-09 20:32:09 +01:00
* Converts runtime quaternion to API quaternion
* @returns {*}
2016-12-01 18:37:57 +01:00
*/
2016-12-09 20:32:09 +01:00
GameLib.D3.Quaternion.prototype.toApiQuaternion = function() {
return new GameLib.D3.API.Quaternion(
2016-12-01 18:37:57 +01:00
this.x,
this.y,
this.z,
2016-12-09 20:32:09 +01:00
this.w,
this.axis.toApiVector(),
this.angle
2016-12-01 18:37:57 +01:00
);
2016-12-02 13:00:56 +01:00
};
2016-12-09 20:32:09 +01:00
GameLib.D3.Quaternion.prototype.normalize = function () {
2016-12-02 13:00:56 +01:00
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;
2016-12-02 13:00:56 +01:00
return this;
};
/**
*
* @param matrix4 GameLib.D3.Matrix4
*/
2016-12-09 20:32:09 +01:00
GameLib.D3.Quaternion.prototype.setFromRotationMatrix = function(matrix4) {
2016-12-02 13:00:56 +01:00
this.instance.setFromRotationMatrix(matrix4.instance);
2016-12-02 13:00:56 +01:00
this.x = this.instance.x;
this.y = this.instance.y;
this.z = this.instance.z;
this.w = this.instance.w;
2016-12-02 13:00:56 +01:00
};
/**
*
2016-12-09 20:32:09 +01:00
* @param quaternion GameLib.D3.Quaternion
* @param t
2016-12-09 20:32:09 +01:00
* @returns {GameLib.D3.Quaternion}
*/
2016-12-09 20:32:09 +01:00
GameLib.D3.Quaternion.prototype.slerp = function (quaternion, t) {
this.updateInstance();
2016-12-09 20:32:09 +01:00
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;
};