Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class vec4 {
- constructor(x, y, z, w) {
- this.x = x;
- this.y = y;
- this.z = z;
- this.w = w;
- }
- }
- function point(x, y, z) {
- return new vec4(x, y, z, 1);
- }
- function direction(x, y, z) {
- return new vec4(x, y, z, 0);
- }
- // 4D-Matrix representation
- // [ a1 b1 c1 d1 ]
- // [ a2 b2 c2 d2 ]
- // [ a3 b3 c3 d3 ]
- // [ a4 b4 c4 d4 ]
- class mat4 {
- constructor() {
- this.a1 = 0;
- this.a2 = 0;
- this.a3 = 0;
- this.a4 = 0;
- this.b1 = 0;
- this.b2 = 0;
- this.b3 = 0;
- this.b4 = 0;
- this.c1 = 0;
- this.c2 = 0;
- this.c3 = 0;
- this.c4 = 0;
- this.d1 = 0;
- this.d2 = 0;
- this.d3 = 0;
- this.d4 = 0;
- }
- mul(other) {
- if (other instanceof mat4) {
- let result = new mat4();
- result.a1 = this.a1 * other.a1 + this.b1 * other.a2 + this.c1 * other.a3 + this.d1 * other.a4;
- result.a2 = this.a2 * other.a1 + this.b2 * other.a2 + this.c2 * other.a3 + this.d2 * other.a4;
- result.a3 = this.a3 * other.a1 + this.b3 * other.a2 + this.c3 * other.a3 + this.d3 * other.a4;
- result.a4 = this.a4 * other.a1 + this.b4 * other.a2 + this.c4 * other.a3 + this.d4 * other.a4;
- result.b1 = this.a1 * other.b1 + this.b1 * other.b2 + this.c1 * other.b3 + this.d1 * other.b4;
- result.b2 = this.a2 * other.b1 + this.b2 * other.b2 + this.c2 * other.b3 + this.d2 * other.b4;
- result.b3 = this.a3 * other.b1 + this.b3 * other.b2 + this.c3 * other.b3 + this.d3 * other.b4;
- result.b4 = this.a4 * other.b1 + this.b4 * other.b2 + this.c4 * other.b3 + this.d4 * other.b4;
- result.c1 = this.a1 * other.c1 + this.b1 * other.c2 + this.c1 * other.c3 + this.d1 * other.c4;
- result.c2 = this.a2 * other.c1 + this.b2 * other.c2 + this.c2 * other.c3 + this.d2 * other.c4;
- result.c3 = this.a3 * other.c1 + this.b3 * other.c2 + this.c3 * other.c3 + this.d3 * other.c4;
- result.c4 = this.a4 * other.c1 + this.b4 * other.c2 + this.c4 * other.c3 + this.d4 * other.c4;
- result.d1 = this.a1 * other.d1 + this.b1 * other.d2 + this.c1 * other.d3 + this.d1 * other.d4;
- result.d2 = this.a2 * other.d1 + this.b2 * other.d2 + this.c2 * other.d3 + this.d2 * other.d4;
- result.d3 = this.a3 * other.d1 + this.b3 * other.d2 + this.c3 * other.d3 + this.d3 * other.d4;
- result.d4 = this.a4 * other.d1 + this.b4 * other.d2 + this.c4 * other.d3 + this.d4 * other.d4;
- return result;
- }
- if (other instanceof vec4) {
- return new vec4(
- other.x * this.a1 + other.y * this.b1 + other.z * this.c1 + other.w * this.d1,
- other.x * this.a2 + other.y * this.b2 + other.z * this.c2 + other.w * this.d2,
- other.x * this.a3 + other.y * this.b3 + other.z * this.c3 + other.w * this.d3,
- other.x * this.a4 + other.y * this.b4 + other.z * this.c4 + other.w * this.d4
- );
- }
- }
- }
- // In all rotate functions, theta is in radians
- // rotateX(theta) returns a mat4 with the values below:
- // rotateX(theta) = [ 1 0 0 0 ]
- // [ 0 cos(theta) -sin(theta) 0 ]
- // [ 0 sin(theta) costheta) 0 ]
- // [ 0 0 0 1 ]
- function rotateX(theta) {
- let result = new mat4();
- result.a1 = 1;
- result.b2 = Math.cos(theta);
- result.c2 = -Math.sin(theta);
- result.b3 = Math.sin(theta);
- result.c3 = Math.cos(theta);
- result.d4 = 1;
- return result;
- }
- // rotateY(theta) returns a mat4 with the values below:
- // rotateY(theta) = [ cos(theta) 0 sin(theta) 0 ]
- // [ 0 1 0 0 ]
- // [ -sin(theta) 0 cos(theta) 0 ]
- // [ 0 0 0 1 ]
- function rotateY(theta) {
- let result = new mat4();
- result.a1 = Math.cos(theta);
- result.c1 = Math.sin(theta);
- result.b2 = 1;
- result.a3 = -Math.sin(theta);
- result.c3 = Math.cos(theta);
- result.d4 = 1;
- return result;
- }
- // rotateZ(theta) returns a mat4 with the values below:
- // rotateZ(theta) = [ cos(theta) -sin(theta) 0 0 ]
- // [ sin(theta) cos(theta) 0 0 ]
- // [ 0 0 1 0 ]
- // [ 0 0 0 1 ]
- function rotateZ(theta) {
- let result = new mat4();
- result.a1 = Math.cos(theta);
- result.b1 = -Math.sin(theta);
- result.a2 = Math.sin(theta);
- result.b2 = Math.cos(theta);
- result.c3 = 1;
- result.d4 = 1;
- return result;
- }
- // translate(x, y, z) = [ 1 0 0 x ]
- // [ 0 1 0 y ]
- // [ 0 0 1 z ]
- // [ 0 0 0 1 ]
- function translate(x, y, z) {
- let result = new mat4();
- result.a1 = 1;
- result.d1 = x;
- result.b2 = 1;
- result.d2 = y;
- result.c3 = 1;
- result.d3 = z;
- result.d4 = 1;
- return result;
- }
- // Some conversion functions for ease of use
- function degreesToRadians(degs) {
- return degs * Math.PI / 180;
- }
- function radiansToDegrees(rads) {
- return rads * 180 / Math.PI;
- }
- // Now for an example
- function example() {
- // DEFINING MODEL INFORMATION ========================================================
- // An array of vec4s that represent 8 blocks at (0, 0, 0) - (1, 1, 1)
- let blocks = [
- point(0, 0, 0),
- point(0, 0, 1),
- point(0, 1, 0),
- point(0, 1, 1),
- point(1, 0, 0),
- point(1, 0, 1),
- point(1, 1, 0),
- point(1, 1, 1)
- ];
- // You can probably calculate this yourself :P
- center = {
- x: 0,
- y: 0,
- z: 0
- };
- // ===================================================================================
- // DEFINING TRANSFORMATION ===========================================================
- let modelToCenter = translate(-center.x, -center.y, -center.z);
- let rotation = rotateX(degreesToRadians(90));
- let centerToModel = translate(center.x, center.y, center.z);
- // transform translates the model center to the center of the world
- // then performs a 180 degree rotation about the x-axis
- // then performs a 90 degree rotation about the y-axis
- // then translates the world center back to the model center
- let transform = rotateX(degreesToRadians(90));
- // ===================================================================================
- // PERFORMING TRANSFORMATION =========================================================
- let newBlocks = new Array(8);
- for (let i = 0; i < blocks.length; i++) {
- newBlocks[i] = transform.mul(blocks[i]);
- newBlocks[i].x = Math.round(newBlocks[i].x);
- newBlocks[i].y = Math.round(newBlocks[i].y);
- newBlocks[i].z = Math.round(newBlocks[i].z);
- console.log(newBlocks[i].x, newBlocks[i].y, newBlocks[i].z);
- }
- // ===================================================================================
- console.log(newBlocks); // And here are the results
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement