Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class vec4 {
  2.     constructor(x, y, z, w) {
  3.         this.x = x;
  4.         this.y = y;
  5.         this.z = z;
  6.         this.w = w;
  7.     }
  8. }
  9.  
  10. function point(x, y, z) {
  11.     return new vec4(x, y, z, 1);
  12. }
  13.  
  14. function direction(x, y, z) {
  15.     return new vec4(x, y, z, 0);
  16. }
  17.  
  18. // 4D-Matrix representation
  19. // [ a1 b1 c1 d1 ]
  20. // [ a2 b2 c2 d2 ]
  21. // [ a3 b3 c3 d3 ]
  22. // [ a4 b4 c4 d4 ]
  23.  
  24. class mat4 {
  25.     constructor() {
  26.         this.a1 = 0;
  27.         this.a2 = 0;
  28.         this.a3 = 0;
  29.         this.a4 = 0;
  30.         this.b1 = 0;
  31.         this.b2 = 0;
  32.         this.b3 = 0;
  33.         this.b4 = 0;
  34.         this.c1 = 0;
  35.         this.c2 = 0;
  36.         this.c3 = 0;
  37.         this.c4 = 0;
  38.         this.d1 = 0;
  39.         this.d2 = 0;
  40.         this.d3 = 0;
  41.         this.d4 = 0;
  42.     }
  43.  
  44.     mul(other) {
  45.         if (other instanceof mat4) {
  46.             let result = new mat4();
  47.             result.a1 = this.a1 * other.a1 + this.b1 * other.a2 + this.c1 * other.a3 + this.d1 * other.a4;
  48.             result.a2 = this.a2 * other.a1 + this.b2 * other.a2 + this.c2 * other.a3 + this.d2 * other.a4;
  49.             result.a3 = this.a3 * other.a1 + this.b3 * other.a2 + this.c3 * other.a3 + this.d3 * other.a4;
  50.             result.a4 = this.a4 * other.a1 + this.b4 * other.a2 + this.c4 * other.a3 + this.d4 * other.a4;
  51.             result.b1 = this.a1 * other.b1 + this.b1 * other.b2 + this.c1 * other.b3 + this.d1 * other.b4;
  52.             result.b2 = this.a2 * other.b1 + this.b2 * other.b2 + this.c2 * other.b3 + this.d2 * other.b4;
  53.             result.b3 = this.a3 * other.b1 + this.b3 * other.b2 + this.c3 * other.b3 + this.d3 * other.b4;
  54.             result.b4 = this.a4 * other.b1 + this.b4 * other.b2 + this.c4 * other.b3 + this.d4 * other.b4;
  55.             result.c1 = this.a1 * other.c1 + this.b1 * other.c2 + this.c1 * other.c3 + this.d1 * other.c4;
  56.             result.c2 = this.a2 * other.c1 + this.b2 * other.c2 + this.c2 * other.c3 + this.d2 * other.c4;
  57.             result.c3 = this.a3 * other.c1 + this.b3 * other.c2 + this.c3 * other.c3 + this.d3 * other.c4;
  58.             result.c4 = this.a4 * other.c1 + this.b4 * other.c2 + this.c4 * other.c3 + this.d4 * other.c4;
  59.             result.d1 = this.a1 * other.d1 + this.b1 * other.d2 + this.c1 * other.d3 + this.d1 * other.d4;
  60.             result.d2 = this.a2 * other.d1 + this.b2 * other.d2 + this.c2 * other.d3 + this.d2 * other.d4;
  61.             result.d3 = this.a3 * other.d1 + this.b3 * other.d2 + this.c3 * other.d3 + this.d3 * other.d4;
  62.             result.d4 = this.a4 * other.d1 + this.b4 * other.d2 + this.c4 * other.d3 + this.d4 * other.d4;
  63.             return result;
  64.         }
  65.  
  66.         if (other instanceof vec4) {
  67.             return new vec4(
  68.                 other.x * this.a1 + other.y * this.b1 + other.z * this.c1 + other.w * this.d1,
  69.                 other.x * this.a2 + other.y * this.b2 + other.z * this.c2 + other.w * this.d2,
  70.                 other.x * this.a3 + other.y * this.b3 + other.z * this.c3 + other.w * this.d3,
  71.                 other.x * this.a4 + other.y * this.b4 + other.z * this.c4 + other.w * this.d4
  72.             );
  73.         }
  74.     }
  75. }
  76.  
  77. // In all rotate functions, theta is in radians
  78.  
  79. // rotateX(theta) returns a mat4 with the values below:
  80. // rotateX(theta) = [ 1           0           0           0 ]
  81. //                  [ 0           cos(theta)  -sin(theta) 0 ]
  82. //                  [ 0           sin(theta)  costheta)  0  ]
  83. //                  [ 0           0           0           1 ]
  84. function rotateX(theta) {
  85.     let result = new mat4();
  86.     result.a1 =  1;
  87.     result.b2 =  Math.cos(theta);
  88.     result.c2 = -Math.sin(theta);
  89.     result.b3 =  Math.sin(theta);
  90.     result.c3 =  Math.cos(theta);
  91.     result.d4 =  1;
  92.     return result;
  93. }
  94.  
  95. // rotateY(theta) returns a mat4 with the values below:
  96. // rotateY(theta) = [ cos(theta)  0           sin(theta)  0 ]
  97. //                  [ 0           1           0           0 ]
  98. //                  [ -sin(theta) 0           cos(theta)  0 ]
  99. //                  [ 0           0           0           1 ]
  100. function rotateY(theta) {
  101.     let result = new mat4();
  102.     result.a1 =  Math.cos(theta);
  103.     result.c1 =  Math.sin(theta);
  104.     result.b2 =  1;
  105.     result.a3 = -Math.sin(theta);
  106.     result.c3 =  Math.cos(theta);
  107.     result.d4 =  1;
  108.     return result;
  109. }
  110.  
  111. // rotateZ(theta) returns a mat4 with the values below:
  112. // rotateZ(theta) = [ cos(theta)  -sin(theta) 0           0 ]
  113. //                  [ sin(theta)  cos(theta)  0           0 ]
  114. //                  [ 0           0           1           0 ]
  115. //                  [ 0           0           0           1 ]
  116. function rotateZ(theta) {
  117.     let result = new mat4();
  118.     result.a1 =  Math.cos(theta);
  119.     result.b1 = -Math.sin(theta);
  120.     result.a2 =  Math.sin(theta);
  121.     result.b2 =  Math.cos(theta);
  122.     result.c3 =  1;
  123.     result.d4 =  1;
  124.     return result;
  125. }
  126.  
  127. // translate(x, y, z) = [ 1 0 0 x ]
  128. //                      [ 0 1 0 y ]
  129. //                      [ 0 0 1 z ]
  130. //                      [ 0 0 0 1 ]
  131. function translate(x, y, z) {
  132.     let result = new mat4();
  133.     result.a1 = 1;
  134.     result.d1 = x;
  135.     result.b2 = 1;
  136.     result.d2 = y;
  137.     result.c3 = 1;
  138.     result.d3 = z;
  139.     result.d4 = 1;
  140.     return result;
  141. }
  142.  
  143. // Some conversion functions for ease of use
  144. function degreesToRadians(degs) {
  145.     return degs * Math.PI / 180;
  146. }
  147. function radiansToDegrees(rads) {
  148.     return rads * 180 / Math.PI;
  149. }
  150.  
  151. // Now for an example
  152. function example() {
  153.     // DEFINING MODEL INFORMATION ========================================================
  154.     // An array of vec4s that represent 8 blocks at (0, 0, 0) - (1, 1, 1)
  155.     let blocks = [
  156.         point(0, 0, 0),
  157.         point(0, 0, 1),
  158.         point(0, 1, 0),
  159.         point(0, 1, 1),
  160.         point(1, 0, 0),
  161.         point(1, 0, 1),
  162.         point(1, 1, 0),
  163.         point(1, 1, 1)
  164.     ];
  165.     // You can probably calculate this yourself :P
  166.     center = {
  167.         x: 0,
  168.         y: 0,
  169.         z: 0
  170.     };
  171.     // ===================================================================================
  172.  
  173.     // DEFINING TRANSFORMATION ===========================================================
  174.     let modelToCenter = translate(-center.x, -center.y, -center.z);
  175.     let rotation = rotateX(degreesToRadians(90));
  176.     let centerToModel = translate(center.x, center.y, center.z);
  177.  
  178.     // transform translates the model center to the center of the world
  179.     // then performs a 180 degree rotation about the x-axis
  180.     // then performs a 90 degree rotation about the y-axis
  181.     // then translates the world center back to the model center
  182.     let transform = rotateX(degreesToRadians(90));
  183.     // ===================================================================================
  184.  
  185.     // PERFORMING TRANSFORMATION =========================================================
  186.     let newBlocks = new Array(8);
  187.     for (let i = 0; i < blocks.length; i++) {
  188.         newBlocks[i] = transform.mul(blocks[i]);
  189.         newBlocks[i].x = Math.round(newBlocks[i].x);
  190.         newBlocks[i].y = Math.round(newBlocks[i].y);
  191.         newBlocks[i].z = Math.round(newBlocks[i].z);
  192.         console.log(newBlocks[i].x, newBlocks[i].y, newBlocks[i].z);
  193.     }
  194.     // ===================================================================================
  195.  
  196.     console.log(newBlocks); // And here are the results
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement