board.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import * as three from "three";
  2. import {CSS2DObject} from "2dRenderer";
  3. export default class Board{
  4. constructor(scene, width, height, length){
  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. }
  22. translate(x, y, z){
  23. this.board.position.x += x;
  24. this.board.position.y += y;
  25. this.board.position.z += z;
  26. }
  27. rotate(x, y, z){
  28. this.board.rotation.x += this.toRadians(x);
  29. this.board.rotation.y += this.toRadians(y);
  30. this.board.rotation.z += this.toRadians(z);
  31. }
  32. surfaceArea(){
  33. let w = this.width;
  34. let l = this.length;
  35. let h = this.height;
  36. return 2*((w*l)+(l*h)+(h*w));
  37. }
  38. toRadians(n){
  39. return n*(Math.PI/180);
  40. }
  41. createLabel(board){
  42. const labelElem = document.createElement("div");
  43. labelElem.classList.add("label");
  44. labelElem.textContent = `${this.type}x${this.length} in.`;
  45. labelElem.style.backgroundColor = "transparent";
  46. const label = new CSS2DObject(labelElem);
  47. label.position.set(0, 0, 0);
  48. label.center.set(0, 1);
  49. board.add(label);
  50. label.layers.set(0);
  51. return label;
  52. }
  53. }