BoxLineGeometry.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute
  4. } from 'three';
  5. /**
  6. * A special type of box geometry intended for {@link LineSegments}.
  7. *
  8. * ```js
  9. * const geometry = new THREE.BoxLineGeometry();
  10. * const material = new THREE.LineBasicMaterial( { color: 0x00ff00 } );
  11. * const lines = new THREE.LineSegments( geometry, material );
  12. * scene.add( lines );
  13. * ```
  14. *
  15. * @augments BufferGeometry
  16. */
  17. class BoxLineGeometry extends BufferGeometry {
  18. /**
  19. * Constructs a new box line geometry.
  20. *
  21. * @param {number} [width=1] - The width. That is, the length of the edges parallel to the X axis.
  22. * @param {number} [height=1] - The height. That is, the length of the edges parallel to the Y axis.
  23. * @param {number} [depth=1] - The depth. That is, the length of the edges parallel to the Z axis.
  24. * @param {number} [widthSegments=1] - Number of segmented rectangular sections along the width of the sides.
  25. * @param {number} [heightSegments=1] - Number of segmented rectangular sections along the height of the sides.
  26. * @param {number} [depthSegments=1] - Number of segmented rectangular sections along the depth of the sides.
  27. */
  28. constructor( width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1 ) {
  29. super();
  30. widthSegments = Math.floor( widthSegments );
  31. heightSegments = Math.floor( heightSegments );
  32. depthSegments = Math.floor( depthSegments );
  33. const widthHalf = width / 2;
  34. const heightHalf = height / 2;
  35. const depthHalf = depth / 2;
  36. const segmentWidth = width / widthSegments;
  37. const segmentHeight = height / heightSegments;
  38. const segmentDepth = depth / depthSegments;
  39. const vertices = [];
  40. let x = - widthHalf;
  41. let y = - heightHalf;
  42. let z = - depthHalf;
  43. for ( let i = 0; i <= widthSegments; i ++ ) {
  44. vertices.push( x, - heightHalf, - depthHalf, x, heightHalf, - depthHalf );
  45. vertices.push( x, heightHalf, - depthHalf, x, heightHalf, depthHalf );
  46. vertices.push( x, heightHalf, depthHalf, x, - heightHalf, depthHalf );
  47. vertices.push( x, - heightHalf, depthHalf, x, - heightHalf, - depthHalf );
  48. x += segmentWidth;
  49. }
  50. for ( let i = 0; i <= heightSegments; i ++ ) {
  51. vertices.push( - widthHalf, y, - depthHalf, widthHalf, y, - depthHalf );
  52. vertices.push( widthHalf, y, - depthHalf, widthHalf, y, depthHalf );
  53. vertices.push( widthHalf, y, depthHalf, - widthHalf, y, depthHalf );
  54. vertices.push( - widthHalf, y, depthHalf, - widthHalf, y, - depthHalf );
  55. y += segmentHeight;
  56. }
  57. for ( let i = 0; i <= depthSegments; i ++ ) {
  58. vertices.push( - widthHalf, - heightHalf, z, - widthHalf, heightHalf, z );
  59. vertices.push( - widthHalf, heightHalf, z, widthHalf, heightHalf, z );
  60. vertices.push( widthHalf, heightHalf, z, widthHalf, - heightHalf, z );
  61. vertices.push( widthHalf, - heightHalf, z, - widthHalf, - heightHalf, z );
  62. z += segmentDepth;
  63. }
  64. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  65. }
  66. }
  67. export { BoxLineGeometry };
粤ICP备19079148号