Spherical.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { _Math } from './Math';
  2. /**
  3. * @author bhouston / http://clara.io
  4. * @author WestLangley / http://github.com/WestLangley
  5. *
  6. * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system
  7. *
  8. * The poles (phi) are at the positive and negative y axis.
  9. * The equator starts at positive z.
  10. */
  11. function Spherical( radius, phi, theta ) {
  12. this.radius = ( radius !== undefined ) ? radius : 1.0;
  13. this.phi = ( phi !== undefined ) ? phi : 0; // up / down towards top and bottom pole
  14. this.theta = ( theta !== undefined ) ? theta : 0; // around the equator of the sphere
  15. return this;
  16. }
  17. Object.assign( Spherical.prototype, {
  18. set: function ( radius, phi, theta ) {
  19. this.radius = radius;
  20. this.phi = phi;
  21. this.theta = theta;
  22. return this;
  23. },
  24. clone: function () {
  25. return new this.constructor().copy( this );
  26. },
  27. copy: function ( other ) {
  28. this.radius = other.radius;
  29. this.phi = other.phi;
  30. this.theta = other.theta;
  31. return this;
  32. },
  33. // restrict phi to be betwee EPS and PI-EPS
  34. makeSafe: function() {
  35. var EPS = 0.000001;
  36. this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) );
  37. return this;
  38. },
  39. setFromVector3: function( vec3 ) {
  40. this.radius = vec3.length();
  41. if ( this.radius === 0 ) {
  42. this.theta = 0;
  43. this.phi = 0;
  44. } else {
  45. this.theta = Math.atan2( vec3.x, vec3.z ); // equator angle around y-up axis
  46. this.phi = Math.acos( _Math.clamp( vec3.y / this.radius, - 1, 1 ) ); // polar angle
  47. }
  48. return this;
  49. }
  50. } );
  51. export { Spherical };