UNPKG

1.53 kBJavaScriptView Raw
1import { _Math } from './Math.js';
2
3/**
4 * @author bhouston / http://clara.io
5 * @author WestLangley / http://github.com/WestLangley
6 *
7 * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system
8 *
9 * The poles (phi) are at the positive and negative y axis.
10 * The equator starts at positive z.
11 */
12
13function Spherical( radius, phi, theta ) {
14
15 this.radius = ( radius !== undefined ) ? radius : 1.0;
16 this.phi = ( phi !== undefined ) ? phi : 0; // up / down towards top and bottom pole
17 this.theta = ( theta !== undefined ) ? theta : 0; // around the equator of the sphere
18
19 return this;
20
21}
22
23Object.assign( Spherical.prototype, {
24
25 set: function ( radius, phi, theta ) {
26
27 this.radius = radius;
28 this.phi = phi;
29 this.theta = theta;
30
31 return this;
32
33 },
34
35 clone: function () {
36
37 return new this.constructor().copy( this );
38
39 },
40
41 copy: function ( other ) {
42
43 this.radius = other.radius;
44 this.phi = other.phi;
45 this.theta = other.theta;
46
47 return this;
48
49 },
50
51 // restrict phi to be betwee EPS and PI-EPS
52 makeSafe: function () {
53
54 var EPS = 0.000001;
55 this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) );
56
57 return this;
58
59 },
60
61 setFromVector3: function ( vec3 ) {
62
63 this.radius = vec3.length();
64
65 if ( this.radius === 0 ) {
66
67 this.theta = 0;
68 this.phi = 0;
69
70 } else {
71
72 this.theta = Math.atan2( vec3.x, vec3.z ); // equator angle around y-up axis
73 this.phi = Math.acos( _Math.clamp( vec3.y / this.radius, - 1, 1 ) ); // polar angle
74
75 }
76
77 return this;
78
79 }
80
81} );
82
83
84export { Spherical };