1 | import { _Math } from './Math.js';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function Spherical( radius, phi, theta ) {
|
14 |
|
15 | this.radius = ( radius !== undefined ) ? radius : 1.0;
|
16 | this.phi = ( phi !== undefined ) ? phi : 0;
|
17 | this.theta = ( theta !== undefined ) ? theta : 0;
|
18 |
|
19 | return this;
|
20 |
|
21 | }
|
22 |
|
23 | Object.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 |
|
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 );
|
73 | this.phi = Math.acos( _Math.clamp( vec3.y / this.radius, - 1, 1 ) );
|
74 |
|
75 | }
|
76 |
|
77 | return this;
|
78 |
|
79 | }
|
80 |
|
81 | } );
|
82 |
|
83 |
|
84 | export { Spherical };
|