Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vector3{
- constructor(x,y,z){
- this.x = x;
- this.y = y;
- this.z = z;
- }
- }
- class Vector2{
- constructor(x,y){
- this.x = x;
- this.y = y;
- }
- }
- function transformation(point, cam_pos, cam_angle, display){
- x = point.x - cam_pos.x;
- y = point.y - cam_pos.y;
- z = point.z - cam_pos.z;
- dx = Math.cos(cam_angle.y) * (Math.sin(cam_angle.z) * y + Math.cos(cam_angle.z) * x) - Math.sin(cam_angle.y) * z;
- dy = Math.sin(cam_angle.x) * (Math.cos(cam_angle.y) * z + Math.sin(cam_angle.y) * (Math.sin(cam_angle.z) * y + Math.cos(cam_angle.z) * x)) + Math.cos(cam_angle.x) * (Math.cos(cam_angle.z) * y - Math.sin(cam_angle.z) * x);
- dz = Math.cos(cam_angle.x) * (Math.cos(cam_angle.y) * z + Math.sin(cam_angle.y) * (Math.sin(cam_angle.z) * y + Math.cos(cam_angle.z) * x)) - Math.sin(cam_angle.x) * (Math.cos(cam_angle.z) * y - Math.sin(cam_angle.z) * x);
- bx = (display.z/dz) * dx + display.x;
- by = (display.z/dz) * dy + display.y;
- point2D = new Vector2(bx,by);
- return point2D;
- }
- function multiply(a, b) {
- var aNumRows = a.length, aNumCols = a[0].length,
- bNumRows = b.length, bNumCols = b[0].length,
- m = new Array(aNumRows); // initialize array of rows
- for (var r = 0; r < aNumRows; ++r) {
- m[r] = new Array(bNumCols); // initialize the current row
- for (var c = 0; c < bNumCols; ++c) {
- m[r][c] = 0; // initialize the current cell
- for (var i = 0; i < aNumCols; ++i) {
- m[r][c] += a[r][i] * b[i][c];
- }
- }
- }
- return m;
- }
- function transformation1(point, cam_pos, cam_angle, res, sensor, f, s){
- var sinX = Math.sin(cam_angle.x);
- var sinY = Math.sin(cam_angle.y);
- var sinZ = Math.sin(cam_angle.z);
- var cosX = Math.cos(cam_angle.x);
- var cosY = Math.cos(cam_angle.y);
- var cosZ = Math.cos(cam_angle.z);
- let mat1 = [[(f * res.x)/(2 * sensor.x), s, 0, 0],[0, (f * res.y)/(2 * sensor.y), 0, 0], [0, -1, 1, 0], [0, 0, 0, 1]];
- let mat2 = [[1, 0, 0, 0], [0, cosX, -sinX, 0], [0, sinX, cosX, 0], [0, 0, 0, 1]];
- let mat3 = [[cosY, sinY, 0, 0], [0, 1, 0, 0], [-sinY, cosY, 1, 0], [0, 0, 0, 1]];
- let mat4 = [[cosZ, -sinZ, 0, 0], [sinZ, cosZ, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
- let mat5 = [[1, 0, 0, -cam_pos.x], [0, 1, 0, -cam_pos.y], [0, 0, 1, -cam_pos.z], [0, 0, 0, 1]];
- let mat6 = [[point.x], [point.y], [point.z], [1]];
- let tmp1 = multiply(mat1, mat2);
- let tmp2 = multiply(tmp1, mat3);
- let tmp3 = multiply(tmp2, mat4);
- let tmp4 = multiply(tmp3, mat5);
- let result = multiply(tmp4, mat6);
- let z = result[2][0];
- let mat7 = [[1/z, 0, 0, 0], [0, 1/z, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
- let result1 = multiply(mat7, result);
- //console.log(z);
- point2D = new Vector2(result[0][0] / z, result[1][0] / z);
- //point2D = new Vector2(result1[0][0], result1[1][0])
- //console.log(point2D);
- return point2D;
- }
- function rotatex(point, angle){
- xn = point.x;
- yn = point.y * Math.cos(angle) - point.z * Math.sin(angle);
- zn = point.y * Math.sin(angle) + point.z * Math.cos(angle);
- return new Vector3(xn,yn,zn);
- }
- function rotatey(point, angle){
- xn = point.x * Math.cos(angle) - point.z * Math.sin(angle);
- yn = point.y;
- zn = point.z * Math.cos(angle) + point.x * Math.sin(angle);
- return new Vector3(xn,yn,zn);
- }
- function rotatez(point, angle){
- xn = point.x * Math.cos(angle) - point.y * Math.sin(angle);
- yn = point.x * Math.sin(angle) + point.y * Math.cos(angle);
- zn = point.z;
- return new Vector3(xn,yn,zn);
- }
- function translateXYZ(point, x, y, z){
- xn = point.x + x;
- yn = point.y + y;
- zn = point.z + z;
- return new Vector3(xn,yn,zn);
- }
- function translateObject(vertices, x, y, z){
- for (i = 0; i < vertices.length; i++){
- vertices[i] = translateXYZ(vertices[i], x, y, z);
- }
- return vertices;
- }
- function rotateObjectX(vertices, angleX){
- for (i = 0; i < vertices.length; i++){
- vertices[i] = rotatex(vertices[i], angleX);
- }
- return vertices;
- }
- function rotateObjectY(vertices, angleY){
- for (i = 0; i < vertices.length; i++){
- vertices[i] = rotatey(vertices[i], angleY);
- }
- return vertices;
- }
- let a = 64;
- let vertices = [new Vector3(a,a,a), new Vector3(a,-a,a), new Vector3(-a, -a, a), new Vector3(-a,a,a), new Vector3(a,a,-a), new Vector3(a,-a,-a), new Vector3(-a,-a,-a), new Vector3(-a,a,-a) ];
- let edges = [new Vector2(1,2), new Vector2(2,3), new Vector2(3,4), new Vector2(4,1), new Vector2(5,6), new Vector2(6,7), new Vector2(7,8), new Vector2(8,5), new Vector2(1,5), new Vector2(2,6), new Vector2(3,7), new Vector2(4,8)];
- let axes = [new Vector3(0,0,0), new Vector3(-600,0,0), new Vector3(0,-600,0), new Vector3(0,0,-600)];
- let axes_conn = [new Vector2(1,2), new Vector2(1,3), new Vector2(1,4)];
- let cam_angle = new Vector3(0,0,0);
- let display = new Vector3(550, 375, 256);
- var cam_pos = new Vector3(256, 256, 2048);
- function setup(){
- createCanvas(1100,750);
- frameRate(60);
- }
- function mouseDragged(){
- vertices = rotateObjectX(vertices, (mouseY - pmouseY)*0.005);
- vertices = rotateObjectY(vertices, (mouseX - pmouseX)*0.005);
- }
- let key = false;
- let xdir = 0;
- let ydir = 0;
- let zdir = 0;
- let rotate = 0;
- function keyPressed(){
- if (keyCode === ENTER){
- key = !key;
- }
- if (keyCode === 82){
- rotate++;
- }
- if (keyCode === 38){
- ydir -= 1;
- }
- if (keyCode === 40){
- ydir += 1;
- }
- if (keyCode === 37){
- xdir -= 1;
- }
- if (keyCode === 39){
- xdir += 1;
- }
- if (keyCode === 187){
- zdir -= 1;
- }
- if (keyCode === 189){
- zdir += 1;
- }
- if (keyCode === 8){
- ydir = 0;
- xdir = 0;
- zidr = 0;
- }
- }
- function draw(){
- if (key){
- background(0);
- if (rotate%3 == 1){
- rotateObjectX(vertices, 0.01);
- }
- if (rotate%3 == 2){
- rotateObjectY(vertices, 0.01);
- }
- //translateObject(vertices, xdir, ydir, 0);
- cam_pos.x += xdir;
- cam_pos.y += ydir;
- cam_pos.z += zdir;
- //cam_angle.y += 0.01;
- for (i = 0; i < edges.length; i++){
- var cur_edge = edges[i];
- //var pixel1 = transformation(vertices[cur_edge.x - 1], cam_pos, cam_angle, display);
- //var pixel2 = transformation(vertices[cur_edge.y - 1], cam_pos, cam_angle, display);
- var pixel1 = transformation1(vertices[cur_edge.x - 1], cam_pos, cam_angle, new Vector2(700,700), new Vector2(0.1,0.1), 1, 1);
- var pixel2 = transformation1(vertices[cur_edge.y - 1], cam_pos, cam_angle, new Vector2(700,700), new Vector2(0.1,0.1), 1, 1);
- stroke('green');
- line(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
- }
- /*
- for (i = 0; i < axes_conn.length; i++){
- var cur_edge = axes_conn[i];
- var pixel1 = transformation(axes[cur_edge.x - 1], cam_pos, cam_angle, display);
- var pixel2 = transformation(axes[cur_edge.y - 1], cam_pos, cam_angle, display);
- stroke('green');
- line(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
- //console.log(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
- }
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement