Box3Helper.js 1.3 KB

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