2 * @author mrdoob / http://mrdoob.com/
3 * @author benaadams / https://twitter.com/ben_a_adams
4 * @author Mugen87 / https://github.com/Mugen87
7 import { Geometry } from '../core/Geometry';
8 import { BufferGeometry } from '../core/BufferGeometry';
9 import { Float32BufferAttribute } from '../core/BufferAttribute';
10 import { Vector3 } from '../math/Vector3';
14 function SphereGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
16 Geometry.call( this );
18 this.type = 'SphereGeometry';
22 widthSegments: widthSegments,
23 heightSegments: heightSegments,
26 thetaStart: thetaStart,
27 thetaLength: thetaLength
30 this.fromBufferGeometry( new SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) );
35 SphereGeometry.prototype = Object.create( Geometry.prototype );
36 SphereGeometry.prototype.constructor = SphereGeometry;
38 // SphereBufferGeometry
40 function SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
42 BufferGeometry.call( this );
44 this.type = 'SphereBufferGeometry';
48 widthSegments: widthSegments,
49 heightSegments: heightSegments,
52 thetaStart: thetaStart,
53 thetaLength: thetaLength
56 radius = radius || 50;
58 widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
59 heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
61 phiStart = phiStart !== undefined ? phiStart : 0;
62 phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
64 thetaStart = thetaStart !== undefined ? thetaStart : 0;
65 thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
67 var thetaEnd = thetaStart + thetaLength;
74 var vertex = new Vector3();
75 var normal = new Vector3();
84 // generate vertices, normals and uvs
86 for ( iy = 0; iy <= heightSegments; iy ++ ) {
90 var v = iy / heightSegments;
92 for ( ix = 0; ix <= widthSegments; ix ++ ) {
94 var u = ix / widthSegments;
98 vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
99 vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
100 vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
102 vertices.push( vertex.x, vertex.y, vertex.z );
106 normal.set( vertex.x, vertex.y, vertex.z ).normalize();
107 normals.push( normal.x, normal.y, normal.z );
111 uvs.push( u, 1 - v );
113 verticesRow.push( index ++ );
117 grid.push( verticesRow );
123 for ( iy = 0; iy < heightSegments; iy ++ ) {
125 for ( ix = 0; ix < widthSegments; ix ++ ) {
127 var a = grid[ iy ][ ix + 1 ];
128 var b = grid[ iy ][ ix ];
129 var c = grid[ iy + 1 ][ ix ];
130 var d = grid[ iy + 1 ][ ix + 1 ];
132 if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d );
133 if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d );
141 this.setIndex( indices );
142 this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
143 this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
144 this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
148 SphereBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
149 SphereBufferGeometry.prototype.constructor = SphereBufferGeometry;
152 export { SphereGeometry, SphereBufferGeometry };