2016-12-07 15:20:41 +01:00
|
|
|
/**
|
|
|
|
* Runtime Matrix4
|
|
|
|
* @param graphics
|
|
|
|
* @param parentObject
|
2016-12-09 20:32:09 +01:00
|
|
|
* @param apiMatrix4
|
2016-12-07 15:20:41 +01:00
|
|
|
* @param grain
|
|
|
|
* @constructor
|
|
|
|
*/
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.Matrix4 = function(
|
2016-12-07 15:20:41 +01:00
|
|
|
graphics,
|
2016-12-09 20:32:09 +01:00
|
|
|
apiMatrix4,
|
2017-01-17 13:24:45 +01:00
|
|
|
parentObject,
|
2016-12-07 15:20:41 +01:00
|
|
|
grain
|
2016-10-14 12:32:53 +02:00
|
|
|
) {
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiMatrix4)) {
|
|
|
|
apiMatrix4 = {};
|
|
|
|
}
|
|
|
|
|
2017-05-11 04:48:02 +02:00
|
|
|
if (apiMatrix4 instanceof GameLib.Matrix4) {
|
|
|
|
return apiMatrix4;
|
|
|
|
}
|
|
|
|
|
2016-12-22 17:22:19 +01:00
|
|
|
GameLib.API.Matrix4.call(
|
|
|
|
this,
|
|
|
|
apiMatrix4.rows[0],
|
|
|
|
apiMatrix4.rows[1],
|
|
|
|
apiMatrix4.rows[2],
|
|
|
|
apiMatrix4.rows[3]
|
|
|
|
);
|
2016-12-07 15:20:41 +01:00
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
|
|
|
|
parentObject = null;
|
|
|
|
}
|
2016-12-07 15:20:41 +01:00
|
|
|
this.parentObject = parentObject;
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(grain)) {
|
2016-12-07 15:20:41 +01:00
|
|
|
grain = 0.001;
|
2016-10-14 12:32:53 +02:00
|
|
|
}
|
2016-12-07 15:20:41 +01:00
|
|
|
this.grain = grain;
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
this.rows = this.rows.map(
|
|
|
|
function(row) {
|
|
|
|
if (row instanceof GameLib.API.Vector4) {
|
|
|
|
return new GameLib.Vector4(
|
|
|
|
this.graphics,
|
|
|
|
row,
|
|
|
|
this,
|
|
|
|
this.grain
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.warn('Attempted conversion of wrong instance');
|
|
|
|
throw new Error('Attempted conversion of wrong instance');
|
|
|
|
}
|
|
|
|
}.bind(this)
|
2016-12-07 15:20:41 +01:00
|
|
|
);
|
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
this.forward = new GameLib.Vector4(
|
|
|
|
this.graphics,
|
|
|
|
this.forward,
|
2017-01-17 13:24:45 +01:00
|
|
|
this,
|
|
|
|
this.grain
|
2017-01-02 17:05:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.left = new GameLib.Vector4(
|
|
|
|
this.graphics,
|
|
|
|
this.left,
|
2017-01-17 13:24:45 +01:00
|
|
|
this,
|
|
|
|
this.grain
|
2017-01-02 17:05:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.up = new GameLib.Vector4(
|
|
|
|
this.graphics,
|
|
|
|
this.up,
|
2017-01-17 13:24:45 +01:00
|
|
|
this,
|
|
|
|
this.grain
|
2017-01-02 17:05:40 +01:00
|
|
|
);
|
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.createInstance();
|
2016-12-07 15:20:41 +01:00
|
|
|
};
|
|
|
|
|
2018-01-07 12:40:16 +01:00
|
|
|
GameLib.Matrix4.prototype = Object.create(GameLib.Component.prototype);
|
2016-12-22 17:22:19 +01:00
|
|
|
GameLib.Matrix4.prototype.constructor = GameLib.Matrix4;
|
|
|
|
|
2016-12-07 15:20:41 +01:00
|
|
|
/**
|
|
|
|
* 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) {
|
2016-12-07 15:20:41 +01:00
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance = new THREE.Matrix4();
|
2016-12-07 15:20:41 +01:00
|
|
|
|
2016-12-22 17:22:19 +01:00
|
|
|
/**
|
|
|
|
* We transpose our matrix when we send it to three since we use a different ordering system
|
|
|
|
* They say they use
|
|
|
|
*/
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance.set(
|
2016-12-07 15:20:41 +01:00
|
|
|
this.rows[0].x,
|
|
|
|
this.rows[1].x,
|
|
|
|
this.rows[2].x,
|
|
|
|
this.rows[3].x,
|
2016-12-22 17:22:19 +01:00
|
|
|
this.rows[0].y,
|
|
|
|
this.rows[1].y,
|
|
|
|
this.rows[2].y,
|
2016-12-07 15:20:41 +01:00
|
|
|
this.rows[3].y,
|
2016-12-22 17:22:19 +01:00
|
|
|
this.rows[0].z,
|
|
|
|
this.rows[1].z,
|
|
|
|
this.rows[2].z,
|
2016-12-07 15:20:41 +01:00
|
|
|
this.rows[3].z,
|
2016-12-22 17:22:19 +01:00
|
|
|
this.rows[0].w,
|
|
|
|
this.rows[1].w,
|
|
|
|
this.rows[2].w,
|
2016-12-07 15:20:41 +01:00
|
|
|
this.rows[3].w
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates this instance
|
|
|
|
*/
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.Matrix4.prototype.updateInstance = function() {
|
2016-12-12 17:24:05 +01:00
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance.set(
|
|
|
|
this.rows[0].x,
|
|
|
|
this.rows[1].x,
|
|
|
|
this.rows[2].x,
|
|
|
|
this.rows[3].x,
|
|
|
|
this.rows[0].y,
|
|
|
|
this.rows[1].y,
|
|
|
|
this.rows[2].y,
|
|
|
|
this.rows[3].y,
|
|
|
|
this.rows[0].z,
|
|
|
|
this.rows[1].z,
|
|
|
|
this.rows[2].z,
|
|
|
|
this.rows[3].z,
|
|
|
|
this.rows[0].w,
|
|
|
|
this.rows[1].w,
|
|
|
|
this.rows[2].w,
|
|
|
|
this.rows[3].w
|
|
|
|
);
|
2016-12-12 17:24:05 +01:00
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (this.parentObject &&
|
|
|
|
this.parentObject.updateInstance) {
|
|
|
|
this.parentObject.updateInstance();
|
2016-12-12 17:24:05 +01:00
|
|
|
}
|
2016-10-14 12:32:53 +02:00
|
|
|
};
|
|
|
|
|
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 {*}
|
|
|
|
*/
|
2017-05-16 14:51:57 +02:00
|
|
|
GameLib.Matrix4.prototype.toApiObject = function () {
|
2017-01-17 13:24:45 +01:00
|
|
|
|
2017-09-19 21:27:11 +02:00
|
|
|
return new GameLib.API.Matrix4(
|
|
|
|
this.rows[0].toApiObject(),
|
|
|
|
this.rows[1].toApiObject(),
|
|
|
|
this.rows[2].toApiObject(),
|
|
|
|
this.rows[3].toApiObject()
|
2017-01-17 13:24:45 +01:00
|
|
|
);
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-12-15 14:53:39 +01:00
|
|
|
* Creates a GameLib.Matrix4 from an Object matrix
|
2017-12-02 11:49:27 +01:00
|
|
|
* @param graphics GameLib.GraphicsRuntime
|
2016-12-09 20:32:09 +01:00
|
|
|
* @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
|
|
|
|
*/
|
2017-06-14 14:21:57 +02:00
|
|
|
GameLib.Matrix4.FromObject = function(graphics, objectMatrix, parentObject) {
|
2017-01-02 17:05:40 +01:00
|
|
|
|
2017-06-14 14:21:57 +02:00
|
|
|
var apiMatrix = new GameLib.API.Matrix4.FromObject(objectMatrix);
|
2016-12-09 20:32:09 +01:00
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
return new GameLib.Matrix4(
|
2016-12-09 20:32:09 +01:00
|
|
|
graphics,
|
|
|
|
parentObject,
|
|
|
|
apiMatrix
|
|
|
|
)
|
2016-12-20 17:27:36 +01:00
|
|
|
};
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
/**
|
|
|
|
* Lookat
|
|
|
|
* @param position
|
|
|
|
* @param target
|
|
|
|
* @param up
|
|
|
|
* @returns {GameLib.Matrix4}
|
|
|
|
*/
|
2016-12-20 17:27:36 +01:00
|
|
|
GameLib.Matrix4.prototype.lookAt = function (position, target, up) {
|
|
|
|
|
2016-12-22 17:22:19 +01:00
|
|
|
var pv = new GameLib.API.Vector3(position.x, position.y, position.z);
|
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
var forward = pv.subtract(target).normalize();
|
2016-12-22 17:22:19 +01:00
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
if (forward.squared() === 0) {
|
|
|
|
forward.z = 1;
|
2016-12-22 17:22:19 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
var left = up.cross(forward).normalize();
|
2016-12-22 17:22:19 +01:00
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
if (left.squared() === 0) {
|
|
|
|
forward.x += 0.0001;
|
|
|
|
left = up.cross(forward).normalize();
|
2016-12-22 17:22:19 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
var _up = forward.cross(left);
|
|
|
|
|
|
|
|
this.rows[0].x = left.x;
|
|
|
|
this.rows[0].y = left.y;
|
|
|
|
this.rows[0].z = left.z;
|
|
|
|
|
|
|
|
this.rows[1].x = _up.x;
|
|
|
|
this.rows[1].y = _up.y;
|
|
|
|
this.rows[1].z = _up.z;
|
|
|
|
|
|
|
|
this.rows[2].x = forward.x;
|
|
|
|
this.rows[2].y = forward.y;
|
|
|
|
this.rows[2].z = forward.z;
|
|
|
|
|
|
|
|
this.forward.x = forward.x;
|
|
|
|
this.forward.y = forward.y;
|
|
|
|
this.forward.z = forward.z;
|
2016-12-22 17:22:19 +01:00
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
this.left.x = left.x;
|
|
|
|
this.left.y = left.y;
|
|
|
|
this.left.z = left.z;
|
2016-12-22 17:22:19 +01:00
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
this.up.x = _up.x;
|
|
|
|
this.up.y = _up.y;
|
|
|
|
this.up.z = _up.z;
|
2016-12-22 17:22:19 +01:00
|
|
|
|
|
|
|
this.updateInstance();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
/**
|
|
|
|
* Identity
|
|
|
|
*/
|
2016-12-22 17:22:19 +01:00
|
|
|
GameLib.Matrix4.prototype.identity = function () {
|
|
|
|
this.rows = [
|
|
|
|
new GameLib.Vector4(
|
|
|
|
this.graphics,
|
|
|
|
new GameLib.API.Vector4(1,0,0,0),
|
2017-01-17 13:24:45 +01:00
|
|
|
this,
|
2016-12-22 17:22:19 +01:00
|
|
|
this.grain
|
|
|
|
),
|
|
|
|
new GameLib.Vector4(
|
|
|
|
this.graphics,
|
|
|
|
new GameLib.API.Vector4(0,1,0,0),
|
2017-01-17 13:24:45 +01:00
|
|
|
this,
|
2016-12-22 17:22:19 +01:00
|
|
|
this.grain
|
|
|
|
),
|
|
|
|
new GameLib.Vector4(
|
|
|
|
this.graphics,
|
|
|
|
new GameLib.API.Vector4(0,0,1,0),
|
2017-01-17 13:24:45 +01:00
|
|
|
this,
|
2016-12-22 17:22:19 +01:00
|
|
|
this.grain
|
|
|
|
),
|
|
|
|
new GameLib.Vector4(
|
|
|
|
this.graphics,
|
|
|
|
new GameLib.API.Vector4(0,0,0,1),
|
2017-01-17 13:24:45 +01:00
|
|
|
this,
|
2016-12-22 17:22:19 +01:00
|
|
|
this.grain
|
|
|
|
)
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
/**
|
|
|
|
* Transpose
|
|
|
|
* @returns {GameLib.Matrix4}
|
|
|
|
*/
|
2016-12-22 17:22:19 +01:00
|
|
|
GameLib.Matrix4.prototype.transpose = function () {
|
|
|
|
|
|
|
|
this.temp[0].x = this.rows[0].x;
|
|
|
|
this.temp[0].y = this.rows[1].x;
|
|
|
|
this.temp[0].z = this.rows[2].x;
|
|
|
|
this.temp[0].w = this.rows[3].x;
|
|
|
|
|
|
|
|
this.temp[1].x = this.rows[0].y;
|
|
|
|
this.temp[1].y = this.rows[1].y;
|
|
|
|
this.temp[1].z = this.rows[2].y;
|
|
|
|
this.temp[1].w = this.rows[3].y;
|
|
|
|
|
|
|
|
this.temp[2].x = this.rows[0].z;
|
|
|
|
this.temp[2].y = this.rows[1].z;
|
|
|
|
this.temp[2].z = this.rows[2].z;
|
|
|
|
this.temp[2].w = this.rows[3].z;
|
|
|
|
|
|
|
|
this.temp[3].x = this.rows[0].w;
|
|
|
|
this.temp[3].y = this.rows[1].w;
|
|
|
|
this.temp[3].z = this.rows[2].w;
|
|
|
|
this.temp[3].w = this.rows[3].w;
|
|
|
|
|
|
|
|
this.rows[0].x = this.temp[0].x;
|
|
|
|
this.rows[0].y = this.temp[0].y;
|
|
|
|
this.rows[0].z = this.temp[0].z;
|
|
|
|
this.rows[0].w = this.temp[0].w;
|
|
|
|
|
|
|
|
this.rows[1].x = this.temp[1].x;
|
|
|
|
this.rows[1].y = this.temp[1].y;
|
|
|
|
this.rows[1].z = this.temp[1].z;
|
|
|
|
this.rows[1].w = this.temp[1].w;
|
|
|
|
|
|
|
|
this.rows[2].x = this.temp[2].x;
|
|
|
|
this.rows[2].y = this.temp[2].y;
|
|
|
|
this.rows[2].z = this.temp[2].z;
|
|
|
|
this.rows[2].w = this.temp[2].w;
|
|
|
|
|
|
|
|
this.rows[3].x = this.temp[3].x;
|
|
|
|
this.rows[3].y = this.temp[3].y;
|
|
|
|
this.rows[3].z = this.temp[3].z;
|
|
|
|
this.rows[3].w = this.temp[3].w;
|
2016-12-20 17:27:36 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|