BoxHelper.js 2.3 KB

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