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

312 lines
6.5 KiB
JavaScript

/**
* Runtime Matrix4
* @param graphics
* @param parentObject
* @param apiMatrix4
* @param grain
* @constructor
*/
R3.Matrix4 = function(
graphics,
apiMatrix4,
parentObject,
grain
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (R3.Utils.UndefinedOrNull(apiMatrix4)) {
apiMatrix4 = {};
}
R3.API.Matrix4.call(
this,
apiMatrix4.rows[0],
apiMatrix4.rows[1],
apiMatrix4.rows[2],
apiMatrix4.rows[3]
);
if (R3.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
if (R3.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
}
this.grain = grain;
this.rows = this.rows.map(
function(row) {
return new R3.Vector4(
this.graphics,
row,
this,
this.grain
);
}.bind(this)
);
this.forward = new R3.Vector4(
this.graphics,
this.forward,
this,
this.grain
);
this.left = new R3.Vector4(
this.graphics,
this.left,
this,
this.grain
);
this.up = new R3.Vector4(
this.graphics,
this.up,
this,
this.grain
);
this.createInstance();
};
R3.Matrix4.prototype = Object.create(R3.Component.prototype);
R3.Matrix4.prototype.constructor = R3.Matrix4;
/**
* Creates a matrix 4 instance (currently from graphics lib)
* @param update
*/
R3.Matrix4.prototype.createInstance = function(update) {
this.instance = new THREE.Matrix4();
/**
* We transpose our matrix when we send it to three since we use a different ordering system
* They say they use
*/
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
);
};
/**
* Updates this instance
*/
R3.Matrix4.prototype.updateInstance = function() {
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
);
if (this.parentObject &&
this.parentObject.updateInstance) {
this.parentObject.updateInstance();
}
};
/**
* R3.Matrix4 to R3.API.Matrix4
* @returns {*}
*/
R3.Matrix4.prototype.toApiObject = function () {
return new R3.API.Matrix4(
this.rows[0].toApiObject(),
this.rows[1].toApiObject(),
this.rows[2].toApiObject(),
this.rows[3].toApiObject()
);
};
/**
* Creates a R3.Matrix4 from an Object matrix
* @param graphics R3.GraphicsRuntime
* @param objectMatrix Object
* @param parentObject
* @returns {R3.Matrix4}
* @constructor
*/
R3.Matrix4.FromObject = function(graphics, objectMatrix, parentObject) {
var apiMatrix = new R3.API.Matrix4.FromObject(objectMatrix);
return new R3.Matrix4(
graphics,
parentObject,
apiMatrix
)
};
/**
* Lookat
* @param position
* @param target
* @param up
* @returns {R3.Matrix4}
*/
R3.Matrix4.prototype.lookAt = function (position, target, up) {
var pv = new R3.API.Vector3(position.x, position.y, position.z);
var forward = pv.subtract(target).normalize();
if (forward.squared() === 0) {
forward.z = 1;
}
var left = up.cross(forward).normalize();
if (left.squared() === 0) {
forward.x += 0.0001;
left = up.cross(forward).normalize();
}
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;
this.left.x = left.x;
this.left.y = left.y;
this.left.z = left.z;
this.up.x = _up.x;
this.up.y = _up.y;
this.up.z = _up.z;
this.updateInstance();
return this;
};
/**
* Identity
*/
R3.Matrix4.prototype.identity = function () {
this.rows = [
new R3.Vector4(
this.graphics,
new R3.API.Vector4(1,0,0,0),
this,
this.grain
),
new R3.Vector4(
this.graphics,
new R3.API.Vector4(0,1,0,0),
this,
this.grain
),
new R3.Vector4(
this.graphics,
new R3.API.Vector4(0,0,1,0),
this,
this.grain
),
new R3.Vector4(
this.graphics,
new R3.API.Vector4(0,0,0,1),
this,
this.grain
)
];
};
/**
* Transpose
* @returns {R3.Matrix4}
*/
R3.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;
return this;
};