SHARE
TWEET

Untitled

a guest Dec 9th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Vector3{
  2.   constructor(x,y,z){
  3.     this.x = x;
  4.     this.y = y;
  5.     this.z = z;
  6.   }
  7. }
  8.  
  9. class Vector2{
  10.   constructor(x,y){
  11.     this.x = x;
  12.     this.y = y;
  13.   }
  14. }
  15.  
  16. function transformation(point, cam_pos, cam_angle, display){
  17.   x = point.x - cam_pos.x;
  18.   y = point.y - cam_pos.y;
  19.   z = point.z - cam_pos.z;
  20.   dx = Math.cos(cam_angle.y) * (Math.sin(cam_angle.z) * y + Math.cos(cam_angle.z) * x) - Math.sin(cam_angle.y) * z;
  21.   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);
  22.   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);
  23.  
  24.   bx = (display.z/dz) * dx + display.x;
  25.   by = (display.z/dz) * dy + display.y;
  26.   //console.log(x,y,z,dx,dy,dz,bx,by);
  27.   point2D = new Vector2(bx,by);
  28.   return point2D;
  29. }
  30.  
  31. function rotatex(point, angle){
  32.   xn = point.x;
  33.   yn = point.y * Math.cos(angle) - point.z * Math.sin(angle);
  34.   zn = point.y * Math.sin(angle) + point.z * Math.cos(angle);
  35.   return new Vector3(xn,yn,zn);
  36. }
  37.  
  38. function rotatey(point, angle){
  39.   xn = point.z * Math.sin(angle) + point.x * Math.cos(angle);
  40.   yn = point.y;
  41.   zn = point.y * Math.cos(angle) - point.x * Math.sin(angle);
  42.   return new Vector3(xn,yn,zn);
  43. }
  44.  
  45. function rotateObject(vertices, angleX, angleY, angleZ){
  46.   for (i = 0; i < vertices.length; i++){
  47.     //vertices[i] = rotatex(vertices[i], angleX);
  48.     vertices[i] = rotatey(vertices[i], angleY);
  49.   }
  50.   return vertices;
  51. }
  52.  
  53. let a = 16;
  54. 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) ];
  55. 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)];
  56.  
  57. let cam_angle = new Vector3(0,0,0);
  58. let display   = new Vector3(128, 128, 256);
  59. var cam_pos   = new Vector3(0, 0, 64);
  60.  
  61. function setup(){
  62.   createCanvas(1100,750);
  63.   frameRate(60);
  64. }
  65.  
  66. let key = false;
  67. let x_dir = 0;
  68. let y_dir = 0;
  69. let z_dir = 0;
  70.  
  71. function keyPressed(){
  72.   if (keyCode === ENTER){
  73.     key = !key;
  74.   }
  75.   if (keyCode == LEFT_ARROW){
  76.     x_dir += -1;
  77.   }
  78.   if (keyCode == RIGHT_ARROW){
  79.     x_dir += 1;
  80.   }
  81.   if (keyCode == UP_ARROW){
  82.     y_dir += -1;
  83.   }
  84.   if (keyCode == DOWN_ARROW){
  85.     y_dir += 1;
  86.   }
  87.   if (keyCode == 219){
  88.     z_dir += 1;
  89.   }
  90.   if (keyCode == 221){
  91.     z_dir -=1;
  92.   }
  93.  
  94.   console.log(keyCode);
  95.   if (keyCode == BACKSPACE){
  96.     x_dir = 0;
  97.     y_dir = 0;
  98.     z_dir = 0;
  99.   }
  100. }
  101.  
  102. function draw(){
  103.  
  104.  
  105.   //cam_pos.x += 1;
  106.   if (key){
  107.     background(0);
  108.     //cam_angle.y -= 0.01;
  109.     //cam_pos.x += x_dir*2;
  110.     //cam_pos.y += y_dir*2;
  111.     //cam_pos.z += z_dir*2;
  112.     //console.log(vertices);
  113.     vertices = rotateObject(vertices, 0.01 , 0.1);
  114.     for (i = 0; i < edges.length; i++){
  115.         var cur_edge = edges[i];
  116.         var pixel1 = transformation(vertices[cur_edge.x - 1], cam_pos, cam_angle, display);
  117.         var pixel2 = transformation(vertices[cur_edge.y - 1], cam_pos, cam_angle, display);
  118.         stroke('green');
  119.         line(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  120.         console.log(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  121.     }
  122.   }
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top