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;
- //console.log(x,y,z,dx,dy,dz,bx,by);
- point2D = new Vector2(bx,by);
- 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.z * Math.sin(angle) + point.x * Math.cos(angle);
- yn = point.y;
- zn = point.y * Math.cos(angle) - point.x * Math.sin(angle);
- return new Vector3(xn,yn,zn);
- }
- function rotateObject(vertices, angleX, angleY, angleZ){
- for (i = 0; i < vertices.length; i++){
- //vertices[i] = rotatex(vertices[i], angleX);
- vertices[i] = rotatey(vertices[i], angleY);
- }
- return vertices;
- }
- let a = 16;
- 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 cam_angle = new Vector3(0,0,0);
- let display = new Vector3(128, 128, 256);
- var cam_pos = new Vector3(0, 0, 64);
- function setup(){
- createCanvas(1100,750);
- frameRate(60);
- }
- let key = false;
- let x_dir = 0;
- let y_dir = 0;
- let z_dir = 0;
- function keyPressed(){
- if (keyCode === ENTER){
- key = !key;
- }
- if (keyCode == LEFT_ARROW){
- x_dir += -1;
- }
- if (keyCode == RIGHT_ARROW){
- x_dir += 1;
- }
- if (keyCode == UP_ARROW){
- y_dir += -1;
- }
- if (keyCode == DOWN_ARROW){
- y_dir += 1;
- }
- if (keyCode == 219){
- z_dir += 1;
- }
- if (keyCode == 221){
- z_dir -=1;
- }
- console.log(keyCode);
- if (keyCode == BACKSPACE){
- x_dir = 0;
- y_dir = 0;
- z_dir = 0;
- }
- }
- function draw(){
- //cam_pos.x += 1;
- if (key){
- background(0);
- //cam_angle.y -= 0.01;
- //cam_pos.x += x_dir*2;
- //cam_pos.y += y_dir*2;
- //cam_pos.z += z_dir*2;
- //console.log(vertices);
- vertices = rotateObject(vertices, 0.01 , 0.1);
- 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);
- 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