BoxHelper.js 2.6 KB

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