Sphere.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @author bhouston / http://clara.io
  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. intersectsBox: function ( box ) {
  55. return box.intersectsSphere( this );
  56. },
  57. intersectsPlane: function ( plane ) {
  58. // We use the following equation to compute the signed distance from
  59. // the center of the sphere to the plane.
  60. //
  61. // distance = q * n - d
  62. //
  63. // If this distance is greater than the radius of the sphere,
  64. // then there is no intersection.
  65. return Math.abs( this.center.dot( plane.normal ) - plane.constant ) <= this.radius;
  66. },
  67. clampPoint: function ( point, optionalTarget ) {
  68. var deltaLengthSq = this.center.distanceToSquared( point );
  69. var result = optionalTarget || new THREE.Vector3();
  70. result.copy( point );
  71. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  72. result.sub( this.center ).normalize();
  73. result.multiplyScalar( this.radius ).add( this.center );
  74. }
  75. return result;
  76. },
  77. getBoundingBox: function ( optionalTarget ) {
  78. var box = optionalTarget || new THREE.Box3();
  79. box.set( this.center, this.center );
  80. box.expandByScalar( this.radius );
  81. return box;
  82. },
  83. applyMatrix4: function ( matrix ) {
  84. this.center.applyMatrix4( matrix );
  85. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  86. return this;
  87. },
  88. translate: function ( offset ) {
  89. this.center.add( offset );
  90. return this;
  91. },
  92. equals: function ( sphere ) {
  93. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  94. }
  95. };
粤ICP备19079148号