/** * Runtime Matrix4 * @param graphics * @param parentObject * @param apiMatrix4 * @param grain * @constructor */ GameLib.Matrix4 = function( graphics, parentObject, apiMatrix4, grain ) { for (var property in apiMatrix4) { if (apiMatrix4.hasOwnProperty(property)) { this[property] = apiMatrix4[property]; } } GameLib.Utils.Extend(GameLib.Matrix4, GameLib.API.Matrix4); this.graphics = graphics; this.graphics.isNotThreeThrow(); this.parentObject = parentObject; if (GameLib.Utils.UndefinedOrNull(grain)) { grain = 0.001; } this.grain = grain; this.rows[0] = new GameLib.Quaternion( this.graphics, this, this.rows[0], grain ); this.rows[1] = new GameLib.Quaternion( this.graphics, this, this.rows[1], grain ); this.rows[2] = new GameLib.Quaternion( this.graphics, this, this.rows[2], grain ); this.rows[3] = new GameLib.Quaternion( this.graphics, this, this.rows[3], grain ); this.instance = this.createInstance(); }; /** * Creates a matrix 4 instance (currently from graphics lib) * @param update */ GameLib.Matrix4.prototype.createInstance = function(update) { var instance = null; if (update) { instance = this.instance; } if (!instance) { instance = new THREE.Matrix4(); } instance.set( this.rows[0].x, this.rows[0].y, this.rows[0].z, this.rows[0].w, this.rows[1].x, this.rows[1].y, this.rows[1].z, this.rows[1].w, this.rows[2].x, this.rows[2].y, this.rows[2].z, this.rows[2].w, this.rows[3].x, this.rows[3].y, this.rows[3].z, this.rows[3].w ); return instance; }; /** * Updates this instance */ GameLib.Matrix4.prototype.updateInstance = function() { this.createInstance(true); if (this.parentObject) { if (this.parentObject.updateInstance) { this.parentObject.updateInstance(); } } }; GameLib.Matrix4.prototype.lookAt = function (position, target, up) { this.instance.lookAt(position.instance, target.instance, up.instance); this.rows[0].x = this.instance.elements[0]; this.rows[0].y = this.instance.elements[1]; this.rows[0].z = this.instance.elements[2]; this.rows[0].w = this.instance.elements[3]; this.rows[1].x = this.instance.elements[4]; this.rows[1].y = this.instance.elements[5]; this.rows[1].z = this.instance.elements[6]; this.rows[1].w = this.instance.elements[7]; this.rows[2].x = this.instance.elements[8]; this.rows[2].y = this.instance.elements[9]; this.rows[2].z = this.instance.elements[10]; this.rows[2].w = this.instance.elements[11]; this.rows[3].x = this.instance.elements[12]; this.rows[3].y = this.instance.elements[13]; this.rows[3].z = this.instance.elements[14]; this.rows[3].w = this.instance.elements[15]; }; /** * GameLib.Matrix4 to GameLib.API.Matrix4 * @returns {*} */ GameLib.Matrix4.prototype.toApiMatrix = function () { return new GameLib.API.Matrix4( this.rows[0].toApiQuaternion(), this.rows[1].toApiQuaternion(), this.rows[2].toApiQuaternion(), this.rows[3].toApiQuaternion() ) }; /** * Creates a GameLib.Matrix4 from an Object matrix * @param graphics GameLib.D3.Graphics * @param objectMatrix Object * @param parentObject * @returns {GameLib.Matrix4} * @constructor */ GameLib.Matrix4.FromObjectMatrix = function(graphics, objectMatrix, parentObject) { var apiMatrix = new GameLib.API.Matrix4( new GameLib.API.Quaternion( objectMatrix[0], objectMatrix[1], objectMatrix[2], objectMatrix[3] ), new GameLib.API.Quaternion( objectMatrix[4], objectMatrix[5], objectMatrix[6], objectMatrix[7] ), new GameLib.API.Quaternion( objectMatrix[8], objectMatrix[9], objectMatrix[10], objectMatrix[11] ), new GameLib.API.Quaternion( objectMatrix[12], objectMatrix[13], objectMatrix[14], objectMatrix[15] ) ); return new GameLib.Matrix4( graphics, parentObject, apiMatrix ) };