BoxLineGeometry.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute
  4. } from 'three';
  5. class BoxLineGeometry extends BufferGeometry {
  6. constructor( width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1 ) {
  7. super();
  8. widthSegments = Math.floor( widthSegments );
  9. heightSegments = Math.floor( heightSegments );
  10. depthSegments = Math.floor( depthSegments );
  11. const widthHalf = width / 2;
  12. const heightHalf = height / 2;
  13. const depthHalf = depth / 2;
  14. const segmentWidth = width / widthSegments;
  15. const segmentHeight = height / heightSegments;
  16. const segmentDepth = depth / depthSegments;
  17. const vertices = [];
  18. let x = - widthHalf;
  19. let y = - heightHalf;
  20. let z = - depthHalf;
  21. for ( let i = 0; i <= widthSegments; i ++ ) {
  22. vertices.push( x, - heightHalf, - depthHalf, x, heightHalf, - depthHalf );
  23. vertices.push( x, heightHalf, - depthHalf, x, heightHalf, depthHalf );
  24. vertices.push( x, heightHalf, depthHalf, x, - heightHalf, depthHalf );
  25. vertices.push( x, - heightHalf, depthHalf, x, - heightHalf, - depthHalf );
  26. x += segmentWidth;
  27. }
  28. for ( let i = 0; i <= heightSegments; i ++ ) {
  29. vertices.push( - widthHalf, y, - depthHalf, widthHalf, y, - depthHalf );
  30. vertices.push( widthHalf, y, - depthHalf, widthHalf, y, depthHalf );
  31. vertices.push( widthHalf, y, depthHalf, - widthHalf, y, depthHalf );
  32. vertices.push( - widthHalf, y, depthHalf, - widthHalf, y, - depthHalf );
  33. y += segmentHeight;
  34. }
  35. for ( let i = 0; i <= depthSegments; i ++ ) {
  36. vertices.push( - widthHalf, - heightHalf, z, - widthHalf, heightHalf, z );
  37. vertices.push( - widthHalf, heightHalf, z, widthHalf, heightHalf, z );
  38. vertices.push( widthHalf, heightHalf, z, widthHalf, - heightHalf, z );
  39. vertices.push( widthHalf, - heightHalf, z, - widthHalf, - heightHalf, z );
  40. z += segmentDepth;
  41. }
  42. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  43. }
  44. }
  45. export { BoxLineGeometry };