Sphere.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Sphere = function ( center, radius ) {
  5. this.center = center || new THREE.Vector3();
  6. this.radius = radius || 0;
  7. };
  8. THREE.Sphere.prototype = {
  9. constructor: THREE.Sphere,
  10. set: function ( center, radius ) {
  11. this.center = center;
  12. this.radius = radius;
  13. return this;
  14. },
  15. setFromCenterAndPoints: function ( center, points ) {
  16. var maxRadiusSq = 0;
  17. for ( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {
  18. var radiusSq = center.distanceToSquared( points[i] );
  19. maxRadiusSq = Math.max( maxRadiusSq, radiusSq );
  20. }
  21. this.center = center;
  22. this.radius = Math.sqrt( maxRadiusSq );
  23. return this;
  24. },
  25. copy: function ( sphere ) {
  26. this.center.copy( sphere.center );
  27. this.radius = sphere.radius;
  28. return this;
  29. },
  30. empty: function () {
  31. return ( this.radius <= 0 );
  32. },
  33. volume: function () {
  34. return Math.PI * 4 / 3 * ( this.radius * this.radius * this.radius );
  35. },
  36. containsPoint: function ( point ) {
  37. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  38. },
  39. distanceToPoint: function ( point ) {
  40. return ( point.distanceTo( this.center ) - this.radius );
  41. },
  42. clampPoint: function ( point ) {
  43. var deltaLengthSq = this.center.distanceToSquared( point );
  44. var result = new THREE.Vector3().copy( point );
  45. if( deltaLengthSq > ( this.radius * this.radius ) ) {
  46. result.subSelf( center ).normalize();
  47. result.multiplyScalar( this.radius ).addSelf( this.center );
  48. }
  49. return result;
  50. },
  51. bounds: function () {
  52. var box = new THREE.Box3( this.center, this.center );
  53. box.expandByScalar( this.radius );
  54. return box;
  55. },
  56. translate: function ( offset ) {
  57. this.center.addSelf( this.offset );
  58. return this;
  59. },
  60. scale: function ( factor ) {
  61. this.radius *= factor;
  62. return this;
  63. },
  64. equals: function ( sphere ) {
  65. return sphere.center.equals( this.center ) && ( sphere.radius == this.radius );
  66. },
  67. clone: function () {
  68. return new THREE.Sphere3().copy( this );
  69. }
  70. };
粤ICP备19079148号