BoxHelper.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author Mugen87 / http://github.com/Mugen87
  4. */
  5. import { Box3 } from '../math/Box3';
  6. import { LineSegments } from '../objects/LineSegments';
  7. import { LineBasicMaterial } from '../materials/LineBasicMaterial';
  8. import { BufferAttribute } from '../core/BufferAttribute';
  9. import { BufferGeometry } from '../core/BufferGeometry';
  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. if ( this.object !== undefined ) {
  31. box.setFromObject( this.object );
  32. }
  33. if ( box.isEmpty() ) return;
  34. var min = box.min;
  35. var max = box.max;
  36. /*
  37. 5____4
  38. 1/___0/|
  39. | 6__|_7
  40. 2/___3/
  41. 0: max.x, max.y, max.z
  42. 1: min.x, max.y, max.z
  43. 2: min.x, min.y, max.z
  44. 3: max.x, min.y, max.z
  45. 4: max.x, max.y, min.z
  46. 5: min.x, max.y, min.z
  47. 6: min.x, min.y, min.z
  48. 7: max.x, min.y, min.z
  49. */
  50. var position = this.geometry.attributes.position;
  51. var array = position.array;
  52. array[ 0 ] = max.x; array[ 1 ] = max.y; array[ 2 ] = max.z;
  53. array[ 3 ] = min.x; array[ 4 ] = max.y; array[ 5 ] = max.z;
  54. array[ 6 ] = min.x; array[ 7 ] = min.y; array[ 8 ] = max.z;
  55. array[ 9 ] = max.x; array[ 10 ] = min.y; array[ 11 ] = max.z;
  56. array[ 12 ] = max.x; array[ 13 ] = max.y; array[ 14 ] = min.z;
  57. array[ 15 ] = min.x; array[ 16 ] = max.y; array[ 17 ] = min.z;
  58. array[ 18 ] = min.x; array[ 19 ] = min.y; array[ 20 ] = min.z;
  59. array[ 21 ] = max.x; array[ 22 ] = min.y; array[ 23 ] = min.z;
  60. position.needsUpdate = true;
  61. this.geometry.computeBoundingSphere();
  62. };
  63. } )();
  64. BoxHelper.prototype.setFromObject = function ( object ) {
  65. this.object = object;
  66. this.update();
  67. return this;
  68. };
  69. export { BoxHelper };