Spherical.js 1.3 KB

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