Spherical.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import * as MathUtils from './MathUtils.js';
  2. /**
  3. * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system
  4. *
  5. * phi (the polar angle) is measured from the positive y-axis. The positive y-axis is up.
  6. * theta (the azimuthal angle) is measured from the positive z-axis.
  7. */
  8. class Spherical {
  9. constructor( radius = 1, phi = 0, theta = 0 ) {
  10. this.radius = radius;
  11. this.phi = phi; // polar angle
  12. this.theta = theta; // azimuthal angle
  13. return this;
  14. }
  15. set( radius, phi, theta ) {
  16. this.radius = radius;
  17. this.phi = phi;
  18. this.theta = theta;
  19. return this;
  20. }
  21. copy( other ) {
  22. this.radius = other.radius;
  23. this.phi = other.phi;
  24. this.theta = other.theta;
  25. return this;
  26. }
  27. // restrict phi to be between EPS and PI-EPS
  28. makeSafe() {
  29. const EPS = 0.000001;
  30. this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) );
  31. return this;
  32. }
  33. setFromVector3( v ) {
  34. return this.setFromCartesianCoords( v.x, v.y, v.z );
  35. }
  36. setFromCartesianCoords( x, y, z ) {
  37. this.radius = Math.sqrt( x * x + y * y + z * z );
  38. if ( this.radius === 0 ) {
  39. this.theta = 0;
  40. this.phi = 0;
  41. } else {
  42. this.theta = Math.atan2( x, z );
  43. this.phi = Math.acos( MathUtils.clamp( y / this.radius, - 1, 1 ) );
  44. }
  45. return this;
  46. }
  47. clone() {
  48. return new this.constructor().copy( this );
  49. }
  50. }
  51. export { Spherical };
粤ICP备19079148号