160 lines
4.6 KiB
JavaScript
160 lines
4.6 KiB
JavaScript
|
/**
|
||
|
* Api Matrix 4
|
||
|
* @param row0 R3.API.Vector4
|
||
|
* @param row1 R3.API.Vector4
|
||
|
* @param row2 R3.API.Vector4
|
||
|
* @param row3 R3.API.Vector4
|
||
|
* @constructor
|
||
|
*/
|
||
|
R3.API.Matrix4 = function ApiMatrix4(
|
||
|
row0,
|
||
|
row1,
|
||
|
row2,
|
||
|
row3
|
||
|
) {
|
||
|
this.rows = [];
|
||
|
|
||
|
if (R3.Utils.UndefinedOrNull(row0)) {
|
||
|
row0 = new R3.API.Vector4(1, 0, 0, 0);
|
||
|
}
|
||
|
this.rows[0] = row0;
|
||
|
|
||
|
if (R3.Utils.UndefinedOrNull(row1)) {
|
||
|
row1 = new R3.API.Vector4(0, 1, 0, 0);
|
||
|
}
|
||
|
this.rows[1] = row1;
|
||
|
|
||
|
if (R3.Utils.UndefinedOrNull(row2)) {
|
||
|
row2 = new R3.API.Vector4(0, 0, 1, 0);
|
||
|
}
|
||
|
this.rows[2] = row2;
|
||
|
|
||
|
if (R3.Utils.UndefinedOrNull(row3)) {
|
||
|
row3 = new R3.API.Vector4(0, 0, 0, 1);
|
||
|
}
|
||
|
this.rows[3] = row3;
|
||
|
|
||
|
this.temp = [];
|
||
|
this.temp.push(
|
||
|
new R3.API.Vector4()
|
||
|
);
|
||
|
|
||
|
this.temp.push(
|
||
|
new R3.API.Vector4()
|
||
|
);
|
||
|
|
||
|
this.temp.push(
|
||
|
new R3.API.Vector4()
|
||
|
);
|
||
|
|
||
|
this.temp.push(
|
||
|
new R3.API.Vector4()
|
||
|
);
|
||
|
|
||
|
this.forward = new R3.API.Vector4();
|
||
|
this.left = new R3.API.Vector4();
|
||
|
this.up = new R3.API.Vector4();
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Returns an API matrix from an Object matrix
|
||
|
* @param objectMatrix
|
||
|
* @constructor
|
||
|
*/
|
||
|
R3.API.Matrix4.FromObject = function(objectMatrix) {
|
||
|
|
||
|
if (objectMatrix.rows) {
|
||
|
return new R3.API.Matrix4(
|
||
|
R3.API.Vector4.FromObject(objectMatrix.rows[0]),
|
||
|
R3.API.Vector4.FromObject(objectMatrix.rows[1]),
|
||
|
R3.API.Vector4.FromObject(objectMatrix.rows[2]),
|
||
|
R3.API.Vector4.FromObject(objectMatrix.rows[3])
|
||
|
);
|
||
|
} else if (objectMatrix instanceof Array) {
|
||
|
return new R3.API.Matrix4(
|
||
|
R3.API.Vector4.FromObject(objectMatrix[0]),
|
||
|
R3.API.Vector4.FromObject(objectMatrix[1]),
|
||
|
R3.API.Vector4.FromObject(objectMatrix[2]),
|
||
|
R3.API.Vector4.FromObject(objectMatrix[3])
|
||
|
);
|
||
|
} else {
|
||
|
console.warn('Unsupported object matrix type - whats your DB version?');
|
||
|
throw new Error('Unsupported object matrix type - whats your DB version?');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
R3.API.Matrix4.prototype.rotationMatrixX = function (radians) {
|
||
|
this.identity();
|
||
|
this.rows[1] = new R3.API.Vector4(0, Math.cos(radians), -1 * Math.sin(radians), 0);
|
||
|
this.rows[2] = new R3.API.Vector4(0, Math.sin(radians), Math.cos(radians), 0);
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
R3.API.Matrix4.prototype.rotationMatrixY = function (radians) {
|
||
|
this.identity();
|
||
|
this.rows[0] = new R3.API.Vector4(
|
||
|
Math.cos(radians),
|
||
|
0,
|
||
|
Math.sin(radians),
|
||
|
0
|
||
|
);
|
||
|
this.rows[2] = new R3.API.Vector4(
|
||
|
-1 * Math.sin(radians),
|
||
|
0,
|
||
|
Math.cos(radians),
|
||
|
0
|
||
|
);
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
R3.API.Matrix4.prototype.rotationMatrixZ = function (radians) {
|
||
|
this.identity();
|
||
|
this.rows[0] = new R3.API.Vector4(Math.cos(radians), -1 * Math.sin(radians), 0, 0);
|
||
|
this.rows[1] = new R3.API.Vector4(Math.sin(radians), Math.cos(radians), 0, 0);
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
R3.API.Matrix4.prototype.rotateX = function (radians, point) {
|
||
|
this.identity();
|
||
|
this.rotationMatrixX(radians);
|
||
|
return this.multiply(point);
|
||
|
};
|
||
|
|
||
|
R3.API.Matrix4.prototype.rotateY = function (radians, point) {
|
||
|
this.identity();
|
||
|
this.rotationMatrixY(radians);
|
||
|
return this.multiply(point);
|
||
|
};
|
||
|
|
||
|
R3.API.Matrix4.prototype.rotateZ = function (radians, point) {
|
||
|
this.identity();
|
||
|
this.rotationMatrixZ(radians);
|
||
|
return this.multiply(point);
|
||
|
};
|
||
|
|
||
|
R3.API.Matrix4.prototype.multiply = function (mvp) {
|
||
|
if (mvp instanceof R3.API.Quaternion || mvp instanceof R3.API.Vector4) {
|
||
|
return new R3.API.Quaternion(
|
||
|
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
|
||
|
);
|
||
|
} else if (mvp instanceof R3.API.Vector3) {
|
||
|
return new R3.API.Vector3(
|
||
|
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
|
||
|
);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
R3.API.Matrix4.prototype.identity = function () {
|
||
|
this.rows = [
|
||
|
new R3.API.Vector4(1, 0, 0, 0),
|
||
|
new R3.API.Vector4(0, 1, 0, 0),
|
||
|
new R3.API.Vector4(0, 0, 1, 0),
|
||
|
new R3.API.Vector4(0, 0, 0, 1)
|
||
|
];
|
||
|
};
|