r3-legacy/src/r3-api-matrix4.js

232 lines
5.5 KiB
JavaScript

/**
* R3.API.Matrix4
* @param apiComponent
*
* @property rows [R3.API.Vector4]
*
* @constructor
*/
R3.API.Matrix4 = function (
apiComponent
) {
__DEREGISTER_COMPONENT__;
__API_COMPONENT__;
if (R3.Utils.UndefinedOrNull(apiComponent.rows)) {
apiComponent.rows = [];
apiComponent.rows.push(
new R3.API.Vector4(
{
parent : this,
register : this.register,
x : 1,
y : 0,
z : 0,
w : 0
}
)
);
apiComponent.rows.push(
new R3.API.Vector4(
{
parent : this,
register : this.register,
x : 0,
y : 1,
z : 0,
w : 0
}
)
);
apiComponent.rows.push(
new R3.API.Vector4(
{
parent : this,
register : this.register,
x : 0,
y : 0,
z : 1,
w : 0
}
)
);
apiComponent.rows.push(
new R3.API.Vector4(
{
parent : this,
register : this.register,
x : 0,
y : 0,
z : 0,
w : 1
}
)
);
}
this.rows = apiComponent.rows;
};
R3.API.Matrix4.prototype = Object.create(R3.API.Component.prototype);
R3.API.Matrix4.prototype.constructor = R3.API.Matrix4;
R3.API.Matrix4.prototype.rotationMatrixX = function(radians) {
this.identity();
this.rows[1] = new R3.API.Vector4(
{
parent : this,
register : this.register,
x : 0,
y : Math.cos(radians),
z : -1 * Math.sin(radians),
w : 0
}
);
this.rows[2] = new R3.API.Vector4(
{
parent : this,
register : this.register,
x : 0,
y : Math.sin(radians),
z : Math.cos(radians),
w : 0
}
);
return this;
};
R3.API.Matrix4.prototype.rotationMatrixY = function(radians) {
this.identity();
this.rows[0] = new R3.API.Vector4(
{
parent : this,
register : this.register,
x : Math.cos(radians),
y : 0,
z : Math.sin(radians),
w : 0
}
);
this.rows[2] = new R3.API.Vector4(
{
parent : this,
register : this.register,
x : -1 * Math.sin(radians),
y : 0,
z : Math.cos(radians),
w : 0
}
);
return this;
};
R3.API.Matrix4.prototype.rotationMatrixZ = function(radians) {
this.identity();
this.rows[0] = new R3.API.Vector4(
{
parent : this,
register : this.register,
x : Math.cos(radians),
y : -1 * Math.sin(radians),
z : 0,
w : 0
}
);
this.rows[1] = new R3.API.Vector4(
{
parent : this,
register : this.register,
x : Math.sin(radians),
y : Math.cos(radians),
z : 0,
w : 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(
{
parent : this,
register : this.register,
x : this.rows[0].x * mvp.x + this.rows[0].y * mvp.y + this.rows[0].z * mvp.z + this.rows[0].w * mvp.w,
y : this.rows[1].x * mvp.x + this.rows[1].y * mvp.y + this.rows[1].z * mvp.z + this.rows[1].w * mvp.w,
z : this.rows[2].x * mvp.x + this.rows[2].y * mvp.y + this.rows[2].z * mvp.z + this.rows[2].w * mvp.w,
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(
{
parent : this,
register : this.register,
x : this.rows[0].x * mvp.x + this.rows[0].y * mvp.y + this.rows[0].z * mvp.z,
y : this.rows[1].x * mvp.x + this.rows[1].y * mvp.y + this.rows[1].z * mvp.z,
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[0].x = 1;
this.rows[0].y = 0;
this.rows[0].z = 0;
this.rows[0].w = 0;
this.rows[1].x = 0;
this.rows[1].y = 1;
this.rows[1].z = 0;
this.rows[1].w = 0;
this.rows[2].x = 0;
this.rows[2].y = 0;
this.rows[2].z = 1;
this.rows[2].w = 0;
this.rows[3].x = 0;
this.rows[3].y = 0;
this.rows[3].z = 0;
this.rows[3].w = 1;
};