BoxHelper.js 2.5 KB

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