Box3Helper.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { LineSegments } from '../objects/LineSegments.js';
  2. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  3. import { BufferAttribute } from '../core/BufferAttribute.js';
  4. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  5. import { BufferGeometry } from '../core/BufferGeometry.js';
  6. class Box3Helper extends LineSegments {
  7. constructor( box, color = 0xffff00 ) {
  8. 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 ] );
  9. const positions = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 1, - 1, 1, - 1, - 1 ];
  10. const geometry = new BufferGeometry();
  11. geometry.setIndex( new BufferAttribute( indices, 1 ) );
  12. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  13. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  14. this.box = box;
  15. this.type = 'Box3Helper';
  16. this.geometry.computeBoundingSphere();
  17. }
  18. updateMatrixWorld( force ) {
  19. const box = this.box;
  20. if ( box.isEmpty() ) return;
  21. box.getCenter( this.position );
  22. box.getSize( this.scale );
  23. this.scale.multiplyScalar( 0.5 );
  24. super.updateMatrixWorld( force );
  25. }
  26. }
  27. export { Box3Helper };