BoxHelper.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Box3 } from '../math/Box3.js';
  2. import { LineSegments } from '../objects/LineSegments.js';
  3. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  4. import { BufferAttribute } from '../core/BufferAttribute.js';
  5. import { BufferGeometry } from '../core/BufferGeometry.js';
  6. const _box = /*@__PURE__*/ new Box3();
  7. class BoxHelper extends LineSegments {
  8. constructor( object, color = 0xffff00 ) {
  9. const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
  10. const positions = new Float32Array( 8 * 3 );
  11. const geometry = new BufferGeometry();
  12. geometry.setIndex( new BufferAttribute( indices, 1 ) );
  13. geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) );
  14. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  15. this.object = object;
  16. this.type = 'BoxHelper';
  17. this.matrixAutoUpdate = false;
  18. this.update();
  19. }
  20. update( object ) {
  21. if ( object !== undefined ) {
  22. console.warn( 'THREE.BoxHelper: .update() has no longer arguments.' );
  23. }
  24. if ( this.object !== undefined ) {
  25. _box.setFromObject( this.object );
  26. }
  27. if ( _box.isEmpty() ) return;
  28. const min = _box.min;
  29. const max = _box.max;
  30. /*
  31. 5____4
  32. 1/___0/|
  33. | 6__|_7
  34. 2/___3/
  35. 0: max.x, max.y, max.z
  36. 1: min.x, max.y, max.z
  37. 2: min.x, min.y, max.z
  38. 3: max.x, min.y, max.z
  39. 4: max.x, max.y, min.z
  40. 5: min.x, max.y, min.z
  41. 6: min.x, min.y, min.z
  42. 7: max.x, min.y, min.z
  43. */
  44. const position = this.geometry.attributes.position;
  45. const array = position.array;
  46. array[ 0 ] = max.x; array[ 1 ] = max.y; array[ 2 ] = max.z;
  47. array[ 3 ] = min.x; array[ 4 ] = max.y; array[ 5 ] = max.z;
  48. array[ 6 ] = min.x; array[ 7 ] = min.y; array[ 8 ] = max.z;
  49. array[ 9 ] = max.x; array[ 10 ] = min.y; array[ 11 ] = max.z;
  50. array[ 12 ] = max.x; array[ 13 ] = max.y; array[ 14 ] = min.z;
  51. array[ 15 ] = min.x; array[ 16 ] = max.y; array[ 17 ] = min.z;
  52. array[ 18 ] = min.x; array[ 19 ] = min.y; array[ 20 ] = min.z;
  53. array[ 21 ] = max.x; array[ 22 ] = min.y; array[ 23 ] = min.z;
  54. position.needsUpdate = true;
  55. this.geometry.computeBoundingSphere();
  56. }
  57. setFromObject( object ) {
  58. this.object = object;
  59. this.update();
  60. return this;
  61. }
  62. copy( source ) {
  63. LineSegments.prototype.copy.call( this, source );
  64. this.object = source.object;
  65. return this;
  66. }
  67. }
  68. export { BoxHelper };