1 | /**
|
---|
2 | * @author mrdoob / http://mrdoob.com/
|
---|
3 | */
|
---|
4 |
|
---|
5 | THREE.SphereGeometry = function ( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
|
---|
6 |
|
---|
7 | THREE.Geometry.call( this );
|
---|
8 |
|
---|
9 | this.radius = radius = radius || 50;
|
---|
10 |
|
---|
11 | this.widthSegments = widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
|
---|
12 | this.heightSegments = heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
|
---|
13 |
|
---|
14 | this.phiStart = phiStart = phiStart !== undefined ? phiStart : 0;
|
---|
15 | this.phiLength = phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
|
---|
16 |
|
---|
17 | this.thetaStart = thetaStart = thetaStart !== undefined ? thetaStart : 0;
|
---|
18 | this.thetaLength = thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
|
---|
19 |
|
---|
20 | var x, y, vertices = [], uvs = [];
|
---|
21 |
|
---|
22 | for ( y = 0; y <= heightSegments; y ++ ) {
|
---|
23 |
|
---|
24 | var verticesRow = [];
|
---|
25 | var uvsRow = [];
|
---|
26 |
|
---|
27 | for ( x = 0; x <= widthSegments; x ++ ) {
|
---|
28 |
|
---|
29 | var u = x / widthSegments;
|
---|
30 | var v = y / heightSegments;
|
---|
31 |
|
---|
32 | var vertex = new THREE.Vector3();
|
---|
33 | vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
|
---|
34 | vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
|
---|
35 | vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
|
---|
36 |
|
---|
37 | this.vertices.push( vertex );
|
---|
38 |
|
---|
39 | verticesRow.push( this.vertices.length - 1 );
|
---|
40 | uvsRow.push( new THREE.Vector2( u, 1 - v ) );
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | vertices.push( verticesRow );
|
---|
45 | uvs.push( uvsRow );
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | for ( y = 0; y < this.heightSegments; y ++ ) {
|
---|
50 |
|
---|
51 | for ( x = 0; x < this.widthSegments; x ++ ) {
|
---|
52 |
|
---|
53 | var v1 = vertices[ y ][ x + 1 ];
|
---|
54 | var v2 = vertices[ y ][ x ];
|
---|
55 | var v3 = vertices[ y + 1 ][ x ];
|
---|
56 | var v4 = vertices[ y + 1 ][ x + 1 ];
|
---|
57 |
|
---|
58 | var n1 = this.vertices[ v1 ].clone().normalize();
|
---|
59 | var n2 = this.vertices[ v2 ].clone().normalize();
|
---|
60 | var n3 = this.vertices[ v3 ].clone().normalize();
|
---|
61 | var n4 = this.vertices[ v4 ].clone().normalize();
|
---|
62 |
|
---|
63 | var uv1 = uvs[ y ][ x + 1 ].clone();
|
---|
64 | var uv2 = uvs[ y ][ x ].clone();
|
---|
65 | var uv3 = uvs[ y + 1 ][ x ].clone();
|
---|
66 | var uv4 = uvs[ y + 1 ][ x + 1 ].clone();
|
---|
67 |
|
---|
68 | if ( Math.abs( this.vertices[ v1 ].y ) === this.radius ) {
|
---|
69 |
|
---|
70 | uv1.x = ( uv1.x + uv2.x ) / 2;
|
---|
71 | this.faces.push( new THREE.Face3( v1, v3, v4, [ n1, n3, n4 ] ) );
|
---|
72 | this.faceVertexUvs[ 0 ].push( [ uv1, uv3, uv4 ] );
|
---|
73 |
|
---|
74 | } else if ( Math.abs( this.vertices[ v3 ].y ) === this.radius ) {
|
---|
75 |
|
---|
76 | uv3.x = ( uv3.x + uv4.x ) / 2;
|
---|
77 | this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
|
---|
78 | this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
|
---|
79 |
|
---|
80 | } else {
|
---|
81 |
|
---|
82 | this.faces.push( new THREE.Face3( v1, v2, v4, [ n1, n2, n4 ] ) );
|
---|
83 | this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv4 ] );
|
---|
84 |
|
---|
85 | this.faces.push( new THREE.Face3( v2, v3, v4, [ n2.clone(), n3, n4.clone() ] ) );
|
---|
86 | this.faceVertexUvs[ 0 ].push( [ uv2.clone(), uv3, uv4.clone() ] );
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | this.computeCentroids();
|
---|
95 | this.computeFaceNormals();
|
---|
96 |
|
---|
97 | this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );
|
---|
98 |
|
---|
99 | };
|
---|
100 |
|
---|
101 | THREE.SphereGeometry.prototype = Object.create( THREE.Geometry.prototype );
|
---|