twoByFour.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import * as three from "three";
  2. export default class TwoByFour {
  3. constructor(scene, length){
  4. this.scene = scene;
  5. this.dimensions = [2, 4, length];
  6. const loader = new three.TextureLoader();
  7. const texture = loader.load("/wood-material.jpg", (t)=>{
  8. t.wrapS = t.wrapT = three.RepeatWrapping;
  9. t.offset.set(0, 0);
  10. t.repeat.set(length / 24, 3);
  11. });
  12. texture.colorSpace = three.SRGBColorSpace;
  13. const geometry = new three.BoxGeometry(2, 4, length);
  14. const edges = new three.EdgesGeometry(geometry);
  15. const lineMaterial = new three.LineBasicMaterial({color: 0xffffff});
  16. const line = new three.LineSegments(edges, lineMaterial);
  17. const material = new three.MeshBasicMaterial({map: texture});
  18. const board = new three.Mesh(geometry, material);
  19. this.board = new three.Group();
  20. this.board.add(board);
  21. this.board.add(line);
  22. scene.add(this.board);
  23. }
  24. translate(x, y, z){
  25. this.board.position.x += x;
  26. this.board.position.y += y;
  27. this.board.position.z += z;
  28. }
  29. rotate(x, y, z){
  30. let toRadians = (n)=>n*(Math.PI/180);
  31. this.board.rotation.x += toRadians(x);
  32. this.board.rotation.y += toRadians(y);
  33. this.board.rotation.z += toRadians(z);
  34. }
  35. clone(){
  36. let newBoard = new TwoByFour(this.scene, this.dimensions[2]);
  37. newBoard.board.position.x = this.board.position.x;
  38. newBoard.board.position.y = this.board.position.y;
  39. newBoard.board.position.z = this.board.position.z;
  40. newBoard.board.rotation.x = this.board.rotation.x;
  41. newBoard.board.rotation.y = this.board.rotation.y;
  42. newBoard.board.rotation.z = this.board.rotation.z;
  43. return newBoard;
  44. }
  45. surfaceArea(){
  46. let w = this.dimensions[0];
  47. let l = this.dimensions[1];
  48. let h = this.dimentsions[2];
  49. return 2 * ((w * l) + (l * h) + (h * w));
  50. }
  51. }