Board.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import * as three from "three";
  2. import {CSS2DObject} from "2dRenderer";
  3. export default class Board{
  4. constructor(scene, width, height, length, boardList, degree=90, angleSide="width"){
  5. this.scene = scene;
  6. this.width = width;
  7. this.height = height;
  8. this.length = length;
  9. this.type = `${width}x${height}`;
  10. this.board = new three.Group();
  11. const faces = new Float32Array(this.faces(this.vertices(width/2, height/2, length/2)));
  12. const geometry = new three.BufferGeometry();
  13. geometry.setDrawRange(0, Infinity);
  14. geometry.setAttribute("position", new three.BufferAttribute(faces, 3));
  15. const material = new three.MeshBasicMaterial({color: 0x7a4300});
  16. material.side = three.DoubleSide;
  17. this.drawEdges(this.vertices(width/2, height/2, length/2), this.edges(), this.board);
  18. this.createLabel();
  19. this.board.add(new three.Mesh(geometry, material));
  20. scene.add(this.board);
  21. boardList.push(this.board);
  22. }
  23. translate(x, y, z){
  24. this.board.position.x += x;
  25. this.board.position.y += y;
  26. this.board.position.z += z;
  27. }
  28. rotate(x, y, z){
  29. this.board.rotation.x += this.toRadians(x);
  30. this.board.rotation.y += this.toRadians(y);
  31. this.board.rotation.z += this.toRadians(z);
  32. }
  33. surfaceArea(){
  34. let w = this.width;
  35. let l = this.length;
  36. let h = this.height;
  37. return 2*((w*l)+(l*h)+(h*w));
  38. }
  39. toRadians(n){
  40. return n*(Math.PI/180);
  41. }
  42. createLabel(){
  43. const labelElem = document.createElement("div");
  44. labelElem.classList.add("label");
  45. labelElem.textContent = `${this.type}x${this.length} in.`;
  46. labelElem.style.backgroundColor = "transparent";
  47. const label = new CSS2DObject(labelElem);
  48. label.position.set(0, 0, 0);
  49. label.center.set(0, 1);
  50. label.layers.set(0);
  51. this.board.add(label);
  52. return label;
  53. }
  54. vertices(w, h, l){
  55. return [
  56. [w, h, l],
  57. [w, -h, l],
  58. [-w, -h, l],
  59. [-w, h, l],
  60. [w, h, -l],
  61. [w, -h, -l],
  62. [-w, -h, -l],
  63. [-w, h, -l],
  64. ];
  65. }
  66. edges(){
  67. return [
  68. [0, 4],
  69. [1, 5],
  70. [2, 6],
  71. [3, 7],
  72. [0, 1],
  73. [3, 2],
  74. [4, 5],
  75. [7, 6],
  76. [0, 3],
  77. [1, 2],
  78. [4, 7],
  79. [5, 6]
  80. ];
  81. }
  82. faces(v){
  83. //Ends
  84. return v[0].concat(v[1]).concat(v[2])
  85. .concat(v[2]).concat(v[3]).concat(v[0])
  86. .concat(v[4]).concat(v[5]).concat(v[6])
  87. .concat(v[6]).concat(v[7]).concat(v[4])
  88. //Sides
  89. .concat(v[0]).concat(v[3]).concat(v[4])
  90. .concat(v[3]).concat(v[7]).concat(v[4])
  91. .concat(v[1]).concat(v[2]).concat(v[5])
  92. .concat(v[2]).concat(v[6]).concat(v[5])
  93. //Largest side
  94. .concat(v[0]).concat(v[1]).concat(v[5])
  95. .concat(v[0]).concat(v[4]).concat(v[5])
  96. .concat(v[2]).concat(v[3]).concat(v[7])
  97. .concat(v[2]).concat(v[6]).concat(v[7]);
  98. }
  99. drawEdges(v, e, b){
  100. for(let i = 0; i < e.length; i++){
  101. let coords = new Float32Array(v[e[i][0]].concat(v[e[i][1]]));
  102. const geometry = new three.BufferGeometry();
  103. geometry.setAttribute("position", new three.BufferAttribute(coords, 3));
  104. const wireframe = new three.WireframeGeometry(geometry);
  105. const line = new three.LineSegments(wireframe);
  106. line.material.depthTest = true;
  107. line.material.opacity = 0.5;
  108. line.material.transparent = true;
  109. b.add(line);
  110. }
  111. const geometry = new three.BufferGeometry();
  112. geometry.setAttribute("position", new three.BufferAttribute())
  113. }
  114. }