Advertisement
Genesis332

cube.js

Dec 10th, 2019
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. class Vector3{
  3.   constructor(x,y,z){
  4.     this.x = x;
  5.     this.y = y;
  6.     this.z = z;
  7.   }
  8. }
  9.  
  10. class Vector2{
  11.   constructor(x,y){
  12.     this.x = x;
  13.     this.y = y;
  14.   }
  15. }
  16.  
  17. function transformation(point, cam_pos, cam_angle, display){
  18.   x = point.x - cam_pos.x;
  19.   y = point.y - cam_pos.y;
  20.   z = point.z - cam_pos.z;
  21.   dx = Math.cos(cam_angle.y) * (Math.sin(cam_angle.z) * y + Math.cos(cam_angle.z) * x) - Math.sin(cam_angle.y) * z;
  22.   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);
  23.   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);
  24.  
  25.   bx = (display.z/dz) * dx + display.x;
  26.   by = (display.z/dz) * dy + display.y;
  27.   point2D = new Vector2(bx,by);
  28.   return point2D;
  29. }
  30.  
  31. function multiply(a, b) {
  32.   var aNumRows = a.length, aNumCols = a[0].length,
  33.       bNumRows = b.length, bNumCols = b[0].length,
  34.       m = new Array(aNumRows);  // initialize array of rows
  35.   for (var r = 0; r < aNumRows; ++r) {
  36.     m[r] = new Array(bNumCols); // initialize the current row
  37.     for (var c = 0; c < bNumCols; ++c) {
  38.       m[r][c] = 0;             // initialize the current cell
  39.       for (var i = 0; i < aNumCols; ++i) {
  40.         m[r][c] += a[r][i] * b[i][c];
  41.       }
  42.     }
  43.   }
  44.   return m;
  45. }
  46.  
  47. function transformation1(point, cam_pos, cam_angle, res, sensor, f, s){
  48.   var sinX = Math.sin(cam_angle.x);
  49.   var sinY = Math.sin(cam_angle.y);
  50.   var sinZ = Math.sin(cam_angle.z);
  51.  
  52.   var cosX = Math.cos(cam_angle.x);
  53.   var cosY = Math.cos(cam_angle.y);
  54.   var cosZ = Math.cos(cam_angle.z);
  55.  
  56.   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]];
  57.   let mat2 = [[1, 0, 0, 0], [0, cosX, -sinX, 0], [0, sinX, cosX, 0], [0, 0, 0, 1]];
  58.   let mat3 = [[cosY, sinY, 0, 0], [0, 1, 0, 0], [-sinY, cosY, 1, 0], [0, 0, 0, 1]];
  59.   let mat4 = [[cosZ, -sinZ, 0, 0], [sinZ, cosZ, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
  60.   let mat5 = [[1, 0, 0, -cam_pos.x], [0, 1, 0, -cam_pos.y], [0, 0, 1, -cam_pos.z], [0, 0, 0, 1]];
  61.   let mat6 = [[point.x], [point.y], [point.z], [1]];
  62.  
  63.   let tmp1 = multiply(mat1, mat2);
  64.   let tmp2 = multiply(tmp1, mat3);
  65.   let tmp3 = multiply(tmp2, mat4);
  66.   let tmp4 = multiply(tmp3, mat5);
  67.   let result = multiply(tmp4, mat6);
  68.   let z = result[2][0];
  69.   let mat7 = [[1/z, 0, 0, 0], [0, 1/z, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
  70.  
  71.   let result1 = multiply(mat7, result);
  72.   //console.log(z);
  73.   point2D = new Vector2(result[0][0] / z, result[1][0] / z);
  74.   //point2D = new Vector2(result1[0][0], result1[1][0])
  75.   //console.log(point2D);
  76.   return point2D;
  77. }
  78.  
  79.  
  80. function rotatex(point, angle){
  81.   xn = point.x;
  82.   yn = point.y * Math.cos(angle) - point.z * Math.sin(angle);
  83.   zn = point.y * Math.sin(angle) + point.z * Math.cos(angle);
  84.   return new Vector3(xn,yn,zn);
  85. }
  86.  
  87. function rotatey(point, angle){
  88.   xn = point.x * Math.cos(angle) - point.z * Math.sin(angle);
  89.   yn = point.y;
  90.   zn = point.z * Math.cos(angle) + point.x * Math.sin(angle);
  91.   return new Vector3(xn,yn,zn);
  92. }
  93.  
  94. function rotatez(point, angle){
  95.   xn = point.x * Math.cos(angle) - point.y * Math.sin(angle);
  96.   yn = point.x * Math.sin(angle) + point.y * Math.cos(angle);
  97.   zn = point.z;
  98.   return new Vector3(xn,yn,zn);
  99. }
  100.  
  101. function translateXYZ(point, x, y, z){
  102.   xn = point.x + x;
  103.   yn = point.y + y;
  104.   zn = point.z + z;
  105.   return new Vector3(xn,yn,zn);
  106. }
  107.  
  108. function translateObject(vertices, x, y, z){
  109.   for (i = 0; i < vertices.length; i++){
  110.     vertices[i] = translateXYZ(vertices[i], x, y, z);
  111.   }
  112.   return vertices;
  113. }
  114.  
  115. function rotateObjectX(vertices, angleX){
  116.   for (i = 0; i < vertices.length; i++){
  117.     vertices[i] = rotatex(vertices[i], angleX);
  118.   }
  119.   return vertices;
  120. }
  121.  
  122. function rotateObjectY(vertices, angleY){
  123.   for (i = 0; i < vertices.length; i++){
  124.     vertices[i] = rotatey(vertices[i], angleY);
  125.   }
  126.   return vertices;
  127. }
  128.  
  129.  
  130. let a = 64;
  131. 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) ];
  132. 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)];
  133.  
  134. let axes = [new Vector3(0,0,0), new Vector3(-600,0,0), new Vector3(0,-600,0), new Vector3(0,0,-600)];
  135. let axes_conn = [new Vector2(1,2), new Vector2(1,3), new Vector2(1,4)];
  136.  
  137. let cam_angle = new Vector3(0,0,0);
  138. let display   = new Vector3(550, 375, 256);
  139. var cam_pos   = new Vector3(256, 256, 2048);
  140.  
  141. function setup(){
  142.   createCanvas(1100,750);
  143.   frameRate(60);
  144. }
  145.  
  146.  
  147. function mouseDragged(){
  148.   vertices = rotateObjectX(vertices, (mouseY - pmouseY)*0.005);
  149.   vertices = rotateObjectY(vertices, (mouseX - pmouseX)*0.005);
  150. }
  151.  
  152. let key  = false;
  153. let xdir = 0;
  154. let ydir = 0;
  155. let zdir = 0;
  156. let rotate = 0;
  157.  
  158. function keyPressed(){
  159.   if (keyCode === ENTER){
  160.     key = !key;
  161.   }
  162.   if (keyCode === 82){
  163.     rotate++;
  164.   }
  165.   if (keyCode === 38){
  166.     ydir -= 1;
  167.   }
  168.   if (keyCode === 40){
  169.     ydir += 1;
  170.   }
  171.   if (keyCode === 37){
  172.     xdir -= 1;
  173.   }
  174.   if (keyCode === 39){
  175.     xdir += 1;
  176.   }
  177.   if (keyCode === 187){
  178.     zdir -= 1;
  179.   }
  180.   if (keyCode === 189){
  181.     zdir += 1;
  182.   }
  183.   if (keyCode === 8){
  184.     ydir = 0;
  185.     xdir = 0;
  186.     zidr = 0;
  187.   }
  188. }
  189.  
  190. function draw(){
  191.   if (key){
  192.     background(0);
  193.     if (rotate%3 == 1){
  194.       rotateObjectX(vertices, 0.01);
  195.     }
  196.     if (rotate%3 == 2){
  197.       rotateObjectY(vertices, 0.01);
  198.     }
  199.     //translateObject(vertices, xdir, ydir, 0);
  200.     cam_pos.x += xdir;
  201.     cam_pos.y += ydir;
  202.     cam_pos.z += zdir;
  203.     //cam_angle.y += 0.01;
  204.     for (i = 0; i < edges.length; i++){
  205.         var cur_edge = edges[i];
  206.         //var pixel1 = transformation(vertices[cur_edge.x - 1], cam_pos, cam_angle, display);
  207.         //var pixel2 = transformation(vertices[cur_edge.y - 1], cam_pos, cam_angle, display);
  208.         var pixel1 = transformation1(vertices[cur_edge.x - 1], cam_pos, cam_angle, new Vector2(700,700), new Vector2(0.1,0.1), 1, 1);
  209.         var pixel2 = transformation1(vertices[cur_edge.y - 1], cam_pos, cam_angle, new Vector2(700,700), new Vector2(0.1,0.1), 1, 1);
  210.         stroke('green');
  211.         line(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  212.     }
  213.     /*
  214.     for (i = 0; i < axes_conn.length; i++){
  215.         var cur_edge = axes_conn[i];
  216.         var pixel1 = transformation(axes[cur_edge.x - 1], cam_pos, cam_angle, display);
  217.         var pixel2 = transformation(axes[cur_edge.y - 1], cam_pos, cam_angle, display);
  218.         stroke('green');
  219.         line(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  220.         //console.log(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  221.     }
  222.     */
  223.  
  224.   }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement