Board.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import * as three from "three";
  2. import {CSS2DObject} from "2dRenderer";
  3. /*
  4. * angleData = [
  5. * side ("width" or "height")
  6. * angle in degrees
  7. * bothSides (boolean)
  8. * ]
  9. */
  10. export default class Board{
  11. constructor(scene, width, height, length, boardList, angleData){
  12. this.scene = scene;
  13. this.width = width;
  14. this.height = height;
  15. this.length = length;
  16. this.type = `${width}x${height}`;
  17. this.board = new three.Group();
  18. const vertices = this.vertices(width/2, height/2, length/2, angleData);
  19. const faces = new Float32Array(this.faces(vertices));
  20. const geometry = new three.BufferGeometry();
  21. geometry.setDrawRange(0, Infinity);
  22. geometry.setAttribute("position", new three.BufferAttribute(faces, 3));
  23. const material = new three.MeshBasicMaterial({color: 0x7a4300});
  24. material.side = three.DoubleSide;
  25. this.drawEdges(vertices, this.edges(), this.board);
  26. this.createLabel();
  27. this.board.add(new three.Mesh(geometry, material));
  28. scene.add(this.board);
  29. boardList.push(this.board);
  30. }
  31. translate(x, y, z){
  32. this.board.position.x += x;
  33. this.board.position.y += y;
  34. this.board.position.z += z;
  35. }
  36. rotate(x, y, z){
  37. this.board.rotation.x += this.toRadians(x);
  38. this.board.rotation.y += this.toRadians(y);
  39. this.board.rotation.z += this.toRadians(z);
  40. }
  41. surfaceArea(){
  42. let w = this.width;
  43. let l = this.length;
  44. let h = this.height;
  45. return 2*((w*l)+(l*h)+(h*w));
  46. }
  47. toRadians(n){
  48. return n*(Math.PI/180);
  49. }
  50. createLabel(){
  51. const labelElem = document.createElement("div");
  52. labelElem.classList.add("label");
  53. labelElem.textContent = `${this.type}x${this.length} in.`;
  54. labelElem.style.backgroundColor = "transparent";
  55. const label = new CSS2DObject(labelElem);
  56. label.position.set(0, 0, 0);
  57. label.center.set(0, 1);
  58. label.layers.set(0);
  59. this.board.add(label);
  60. return label;
  61. }
  62. vertices(w, h, l, a){
  63. if(a && a.length === 3){
  64. if(a[0] === "width"){
  65. const shortened = (w * 2) / (Math.tan((a[1] * Math.PI) / 180));
  66. const l1 = l - shortened;
  67. const l2 = a[2] ? l - shortened : l;
  68. return [
  69. [w, h, l1],
  70. [w, -h, l1],
  71. [-w, -h, l],
  72. [-w, h, l],
  73. [w, h, -l2],
  74. [w, -h, -l2],
  75. [-w, -h, -l],
  76. [-w, h, -l],
  77. ];
  78. }else if(a[0] === "height"){
  79. const shortened = (h * 2) / (Math.tan((a[1] * Math.PI) / 180));
  80. const l1 = l - shortened;
  81. const l2 = l - a[2] ? l - shortened : l;
  82. return [
  83. [w, h, l1],
  84. [w, -h, l],
  85. [-w, -h, l],
  86. [-w, h, l1],
  87. [w, h, -l2],
  88. [w, -h, -l],
  89. [-w, -h, -l],
  90. [-w, h, -l2],
  91. ];
  92. }
  93. }
  94. return [
  95. [w, h, l1],
  96. [w, -h, l1],
  97. [-w, -h, l],
  98. [-w, h, l3],
  99. [w, h, -l2],
  100. [w, -h, -l2],
  101. [-w, -h, -l],
  102. [-w, h, -l3],
  103. ];
  104. }
  105. edges(){
  106. return [
  107. [0, 4],
  108. [1, 5],
  109. [2, 6],
  110. [3, 7],
  111. [0, 1],
  112. [3, 2],
  113. [4, 5],
  114. [7, 6],
  115. [0, 3],
  116. [1, 2],
  117. [4, 7],
  118. [5, 6]
  119. ];
  120. }
  121. faces(v){
  122. //Ends
  123. return v[0].concat(v[1]).concat(v[2])
  124. .concat(v[2]).concat(v[3]).concat(v[0])
  125. .concat(v[4]).concat(v[5]).concat(v[6])
  126. .concat(v[6]).concat(v[7]).concat(v[4])
  127. //Sides
  128. .concat(v[0]).concat(v[3]).concat(v[4])
  129. .concat(v[3]).concat(v[7]).concat(v[4])
  130. .concat(v[1]).concat(v[2]).concat(v[5])
  131. .concat(v[2]).concat(v[6]).concat(v[5])
  132. //Largest side
  133. .concat(v[0]).concat(v[1]).concat(v[5])
  134. .concat(v[0]).concat(v[4]).concat(v[5])
  135. .concat(v[2]).concat(v[3]).concat(v[7])
  136. .concat(v[2]).concat(v[6]).concat(v[7]);
  137. }
  138. drawEdges(v, e, b){
  139. for(let i = 0; i < e.length; i++){
  140. let coords = new Float32Array(v[e[i][0]].concat(v[e[i][1]]));
  141. const geometry = new three.BufferGeometry();
  142. geometry.setAttribute("position", new three.BufferAttribute(coords, 3));
  143. const wireframe = new three.WireframeGeometry(geometry);
  144. const line = new three.LineSegments(wireframe);
  145. line.material.depthTest = true;
  146. line.material.opacity = 0.5;
  147. line.material.transparent = true;
  148. b.add(line);
  149. }
  150. const geometry = new three.BufferGeometry();
  151. geometry.setAttribute("position", new three.BufferAttribute())
  152. }
  153. }