r3-legacy/src/game-lib-matrix-4.js

258 lines
6.1 KiB
JavaScript
Raw Normal View History

/**
* Runtime Matrix4
* @param graphics
* @param parentObject
2016-12-09 20:32:09 +01:00
* @param apiMatrix4
* @param grain
* @constructor
*/
2016-12-15 14:53:39 +01:00
GameLib.Matrix4 = function(
graphics,
parentObject,
2016-12-09 20:32:09 +01:00
apiMatrix4,
grain
2016-10-14 12:32:53 +02:00
) {
2016-12-09 20:32:09 +01:00
for (var property in apiMatrix4) {
if (apiMatrix4.hasOwnProperty(property)) {
this[property] = apiMatrix4[property];
}
2016-10-14 12:32:53 +02:00
}
2016-12-15 14:53:39 +01:00
GameLib.Utils.Extend(GameLib.Matrix4, GameLib.API.Matrix4);
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.parentObject = parentObject;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
2016-10-14 12:32:53 +02:00
}
this.grain = grain;
2016-12-15 14:53:39 +01:00
this.rows[0] = new GameLib.Quaternion(
this.graphics,
this,
this.rows[0],
grain
);
2016-12-15 14:53:39 +01:00
this.rows[1] = new GameLib.Quaternion(
this.graphics,
this,
this.rows[1],
grain
);
2016-12-15 14:53:39 +01:00
this.rows[2] = new GameLib.Quaternion(
this.graphics,
this,
this.rows[2],
grain
);
2016-12-15 14:53:39 +01:00
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
*/
2016-12-15 14:53:39 +01:00
GameLib.Matrix4.prototype.createInstance = function(update) {
var instance = null;
2016-10-14 12:32:53 +02:00
if (update) {
instance = this.instance;
2016-10-14 12:32:53 +02:00
}
if (!instance) {
instance = new THREE.Matrix4();
2016-10-14 12:32:53 +02:00
}
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
*/
2016-12-15 14:53:39 +01:00
GameLib.Matrix4.prototype.updateInstance = function() {
2016-12-12 17:24:05 +01:00
this.createInstance(true);
2016-12-12 17:24:05 +01:00
if (this.parentObject) {
if (this.parentObject.updateInstance) {
this.parentObject.updateInstance();
}
}
2016-10-14 12:32:53 +02:00
};
// 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];
//
// };
2016-12-09 20:32:09 +01:00
/**
2016-12-15 14:53:39 +01:00
* GameLib.Matrix4 to GameLib.API.Matrix4
2016-12-09 20:32:09 +01:00
* @returns {*}
*/
2016-12-15 14:53:39 +01:00
GameLib.Matrix4.prototype.toApiMatrix = function () {
return new GameLib.API.Matrix4(
2016-12-09 20:32:09 +01:00
this.rows[0].toApiQuaternion(),
this.rows[1].toApiQuaternion(),
this.rows[2].toApiQuaternion(),
this.rows[3].toApiQuaternion()
)
};
/**
2016-12-15 14:53:39 +01:00
* Creates a GameLib.Matrix4 from an Object matrix
2016-12-09 20:32:09 +01:00
* @param graphics GameLib.D3.Graphics
* @param objectMatrix Object
* @param parentObject
2016-12-15 14:53:39 +01:00
* @returns {GameLib.Matrix4}
2016-12-09 20:32:09 +01:00
* @constructor
*/
2016-12-15 14:53:39 +01:00
GameLib.Matrix4.FromObjectMatrix = function(graphics, objectMatrix, parentObject) {
var apiMatrix = new GameLib.API.Matrix4(
new GameLib.API.Quaternion(
2016-12-09 20:32:09 +01:00
objectMatrix[0],
objectMatrix[1],
objectMatrix[2],
objectMatrix[3]
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Quaternion(
2016-12-09 20:32:09 +01:00
objectMatrix[4],
objectMatrix[5],
objectMatrix[6],
objectMatrix[7]
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Quaternion(
2016-12-09 20:32:09 +01:00
objectMatrix[8],
objectMatrix[9],
objectMatrix[10],
objectMatrix[11]
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Quaternion(
2016-12-09 20:32:09 +01:00
objectMatrix[12],
objectMatrix[13],
objectMatrix[14],
objectMatrix[15]
)
);
2016-12-15 14:53:39 +01:00
return new GameLib.Matrix4(
2016-12-09 20:32:09 +01:00
graphics,
parentObject,
apiMatrix
)
};
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];
// var pv = new GameLib.API.Vector3(position.x, position.y, position.z);
//
// var z = pv.subtract(target).normalize();
//
// if (z.squared() === 0) {
// z.z = 1;
// }
//
// var x = up.cross(z).normalize();
//
// if (x.squared() === 0) {
// z.x += 0.0001;
// x = up.cross(z).normalize();
// }
//
// var y = z.cross(x);
//
// this.rows[0].x = x.x;
// this.rows[0].y = x.y;
// this.rows[0].z = x.z;
//
// this.rows[1].x = y.x;
// this.rows[1].y = y.y;
// this.rows[1].z = y.z;
//
// this.rows[2].x = z.x;
// this.rows[2].y = z.y;
// this.rows[2].z = z.z;
//
// this.updateInstance();
return this;
};