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 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 angleX = 0;
- let angleY = 0.01;
- let angleZ = 0;
- 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 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(128, 128, 256);
- var cam_pos = new Vector3(0, 0, 64);
- 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;
- function keyPressed(){
- if (keyCode === ENTER){
- key = !key;
- }
- if (keyCode === 38){
- ydir += 1;
- }
- if (keyCode === 40){
- ydir -= 1;
- }
- if (keyCode === 37){
- xdir += 1;
- }
- if (keyCode === 39){
- xdir -= 1;
- }
- if (keyCode === 8){
- ydir = 0;
- xdir = 0;
- }
- }
- function draw(){
- if (key){
- background(0);
- translateObject(vertices, xdir, ydir, 0);
- 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);
- }
- /*
- 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