board.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import * as three from "three";
  2. import {CSS2DObject} from "2dRenderer";
  3. export default class Board{
  4. constructor(scene, width, height, length, boardList){
  5. this.scene = scene;
  6. this.width = width;
  7. this.height = height;
  8. this.length = length;
  9. this.type = `${width}x${height}`;
  10. const geometry = new three.BoxGeometry(width, height, length);
  11. const edges = new three.EdgesGeometry(geometry);
  12. const lineMaterial = new three.LineBasicMaterial({color: 0xffffff});
  13. const line = new three.LineSegments(edges, lineMaterial);
  14. const material = new three.MeshBasicMaterial({color: 0x7a4300});
  15. const board = new three.Mesh(geometry, material);
  16. this.board = new three.Group();
  17. this.board.add(board);
  18. this.board.add(line);
  19. this.label = this.createLabel(board);
  20. scene.add(this.board);
  21. if(boardList) boardList.push(this);
  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(board){
  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. board.add(label);
  51. label.layers.set(0);
  52. return label;
  53. }
  54. }