BoxHelper.js 2.4 KB

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