Spherical.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. constructor: Spherical,
  19. set: function ( radius, phi, theta ) {
  20. this.radius = radius;
  21. this.phi = phi;
  22. this.theta = theta;
  23. return this;
  24. },
  25. clone: function () {
  26. return new this.constructor().copy( this );
  27. },
  28. copy: function ( other ) {
  29. this.radius = other.radius;
  30. this.phi = other.phi;
  31. this.theta = other.theta;
  32. return this;
  33. },
  34. // restrict phi to be betwee EPS and PI-EPS
  35. makeSafe: function() {
  36. var EPS = 0.000001;
  37. this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) );
  38. return this;
  39. },
  40. setFromVector3: function( vec3 ) {
  41. this.radius = vec3.length();
  42. if ( this.radius === 0 ) {
  43. this.theta = 0;
  44. this.phi = 0;
  45. } else {
  46. this.theta = Math.atan2( vec3.x, vec3.z ); // equator angle around y-up axis
  47. this.phi = Math.acos( _Math.clamp( vec3.y / this.radius, - 1, 1 ) ); // polar angle
  48. }
  49. return this;
  50. }
  51. } );
  52. export { Spherical };