Sphere.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.Sphere = function ( center, radius ) {
  6. this.center = ( center !== undefined ) ? center : new THREE.Vector3();
  7. this.radius = ( radius !== undefined ) ? radius : 0;
  8. };
  9. THREE.Sphere.prototype = {
  10. constructor: THREE.Sphere,
  11. set: function ( center, radius ) {
  12. this.center.copy( center );
  13. this.radius = radius;
  14. return this;
  15. },
  16. setFromPoints: function () {
  17. var box = new THREE.Box3();
  18. return function ( points, optionalCenter ) {
  19. var center = this.center;
  20. if ( optionalCenter !== undefined ) {
  21. center.copy( optionalCenter );
  22. } else {
  23. box.setFromPoints( points ).center( center );
  24. }
  25. var maxRadiusSq = 0;
  26. for ( var i = 0, il = points.length; i < il; i ++ ) {
  27. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
  28. }
  29. this.radius = Math.sqrt( maxRadiusSq );
  30. return this;
  31. };
  32. }(),
  33. clone: function () {
  34. return new this.constructor().copy( this );
  35. },
  36. copy: function ( sphere ) {
  37. this.center.copy( sphere.center );
  38. this.radius = sphere.radius;
  39. return this;
  40. },
  41. empty: function () {
  42. return ( this.radius <= 0 );
  43. },
  44. containsPoint: function ( point ) {
  45. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  46. },
  47. distanceToPoint: function ( point ) {
  48. return ( point.distanceTo( this.center ) - this.radius );
  49. },
  50. intersectsSphere: function ( sphere ) {
  51. var radiusSum = this.radius + sphere.radius;
  52. return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
  53. },
  54. clampPoint: function ( point, optionalTarget ) {
  55. var deltaLengthSq = this.center.distanceToSquared( point );
  56. var result = optionalTarget || new THREE.Vector3();
  57. result.copy( point );
  58. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  59. result.sub( this.center ).normalize();
  60. result.multiplyScalar( this.radius ).add( this.center );
  61. }
  62. return result;
  63. },
  64. getBoundingBox: function ( optionalTarget ) {
  65. var box = optionalTarget || new THREE.Box3();
  66. box.set( this.center, this.center );
  67. box.expandByScalar( this.radius );
  68. return box;
  69. },
  70. applyMatrix4: function ( matrix ) {
  71. this.center.applyMatrix4( matrix );
  72. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  73. return this;
  74. },
  75. translate: function ( offset ) {
  76. this.center.add( offset );
  77. return this;
  78. },
  79. equals: function ( sphere ) {
  80. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  81. }
  82. };
粤ICP备19079148号