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

240 lines
6.2 KiB
JavaScript
Raw Normal View History

/**
* Runtime Matrix4
* @param graphics
* @param parentObject
* @param matrix4
* @param grain
* @constructor
*/
2016-10-14 12:32:53 +02:00
GameLib.D3.Matrix4 = function(
graphics,
parentObject,
matrix4,
grain
2016-10-14 12:32:53 +02:00
) {
for (var property in matrix4) {
if (matrix4.hasOwnProperty(property)) {
this[property] = matrix4[property];
}
2016-10-14 12:32:53 +02:00
}
GameLib.D3.Utils.Extend(GameLib.D3.Matrix4, GameLib.D3.API.Matrix4);
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.parentObject = parentObject;
if (GameLib.D3.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
2016-10-14 12:32:53 +02:00
}
this.grain = grain;
this.rows[0] = new GameLib.D3.Vector4(
this.graphics,
this,
this.rows[0],
grain
);
this.rows[1] = new GameLib.D3.Vector4(
this.graphics,
this,
this.rows[1],
grain
);
this.rows[2] = new GameLib.D3.Vector4(
this.graphics,
this,
this.rows[2],
grain
);
this.rows[3] = new GameLib.D3.Vector4(
this.graphics,
this,
this.rows[3],
grain
);
this.instance = this.createInstance();
};
/**
* Creates a matrix 4 instance (currently from graphics lib)
* @param update
*/
GameLib.D3.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
*/
GameLib.D3.Matrix4.prototype.updateInstance = function() {
this.createInstance(true);
2016-10-14 12:32:53 +02:00
};
GameLib.D3.Matrix4.prototype.rotationMatrixX = function (radians) {
this.identity();
2016-12-02 13:00:56 +01:00
this.rows[1] = new GameLib.D3.API.Vector4(0, Math.cos(radians), -1 * Math.sin(radians), 0);
this.rows[2] = new GameLib.D3.API.Vector4(0, Math.sin(radians), Math.cos(radians), 0);
2016-10-14 12:32:53 +02:00
return this;
};
GameLib.D3.Matrix4.prototype.rotationMatrixY = function (radians) {
this.identity();
2016-12-02 13:00:56 +01:00
this.rows[0] = new GameLib.D3.API.Vector4(
2016-10-14 12:32:53 +02:00
Math.cos(radians),
0,
Math.sin(radians),
0
);
2016-12-02 13:00:56 +01:00
this.rows[2] = new GameLib.D3.API.Vector4(
2016-10-14 12:32:53 +02:00
-1 * Math.sin(radians),
0,
Math.cos(radians),
0
);
return this;
};
GameLib.D3.Matrix4.prototype.rotationMatrixZ = function (radians) {
this.identity();
2016-12-02 13:00:56 +01:00
this.rows[0] = new GameLib.D3.API.Vector4(Math.cos(radians), -1 * Math.sin(radians), 0, 0);
this.rows[1] = new GameLib.D3.API.Vector4(Math.sin(radians), Math.cos(radians), 0, 0);
2016-10-14 12:32:53 +02:00
return this;
};
GameLib.D3.Matrix4.prototype.rotateX = function (radians, point) {
this.identity();
this.rotationMatrixX(radians);
return this.multiply(point);
};
GameLib.D3.Matrix4.prototype.rotateY = function (radians, point) {
this.identity();
this.rotationMatrixY(radians);
return this.multiply(point);
};
GameLib.D3.Matrix4.prototype.rotateZ = function (radians, point) {
this.identity();
this.rotationMatrixZ(radians);
return this.multiply(point);
};
GameLib.D3.Matrix4.prototype.multiply = function (mvp) {
2016-12-02 13:00:56 +01:00
if (mvp instanceof GameLib.D3.API.Vector4) {
return new GameLib.D3.API.Vector4(
2016-10-14 12:32:53 +02:00
this.rows[0].x * mvp.x + this.rows[0].y * mvp.y + this.rows[0].z * mvp.z + this.rows[0].w * mvp.w,
this.rows[1].x * mvp.x + this.rows[1].y * mvp.y + this.rows[1].z * mvp.z + this.rows[1].w * mvp.w,
this.rows[2].x * mvp.x + this.rows[2].y * mvp.y + this.rows[2].z * mvp.z + this.rows[2].w * mvp.w,
this.rows[3].x * mvp.x + this.rows[3].y * mvp.y + this.rows[3].z * mvp.z + this.rows[3].w * mvp.w
);
2016-12-02 13:00:56 +01:00
} else if (mvp instanceof GameLib.D3.API.Vector3) {
return new GameLib.D3.API.Vector3(
2016-10-14 12:32:53 +02:00
this.rows[0].x * mvp.x + this.rows[0].y * mvp.y + this.rows[0].z * mvp.z,
this.rows[1].x * mvp.x + this.rows[1].y * mvp.y + this.rows[1].z * mvp.z,
this.rows[2].x * mvp.x + this.rows[2].y * mvp.y + this.rows[2].z * mvp.z
);
}
};
GameLib.D3.Matrix4.prototype.identity = function () {
this.rows = [
2016-12-02 13:00:56 +01:00
new GameLib.D3.API.Vector4(1, 0, 0, 0),
new GameLib.D3.API.Vector4(0, 1, 0, 0),
new GameLib.D3.API.Vector4(0, 0, 1, 0),
new GameLib.D3.API.Vector4(0, 0, 0, 1)
2016-10-14 12:32:53 +02:00
];
};
GameLib.D3.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.D3.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;
//
// return this;
2016-10-14 12:32:53 +02:00
};