UNPKG

3.77 kBJavaScriptView Raw
1/**
2 * @author mrdoob / http://mrdoob.com/
3 * @author benaadams / https://twitter.com/ben_a_adams
4 * @author Mugen87 / https://github.com/Mugen87
5 */
6
7import { Geometry } from '../core/Geometry.js';
8import { BufferGeometry } from '../core/BufferGeometry.js';
9import { Float32BufferAttribute } from '../core/BufferAttribute.js';
10import { Vector3 } from '../math/Vector3.js';
11
12// SphereGeometry
13
14function SphereGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
15
16 Geometry.call( this );
17
18 this.type = 'SphereGeometry';
19
20 this.parameters = {
21 radius: radius,
22 widthSegments: widthSegments,
23 heightSegments: heightSegments,
24 phiStart: phiStart,
25 phiLength: phiLength,
26 thetaStart: thetaStart,
27 thetaLength: thetaLength
28 };
29
30 this.fromBufferGeometry( new SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) );
31 this.mergeVertices();
32
33}
34
35SphereGeometry.prototype = Object.create( Geometry.prototype );
36SphereGeometry.prototype.constructor = SphereGeometry;
37
38// SphereBufferGeometry
39
40function SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
41
42 BufferGeometry.call( this );
43
44 this.type = 'SphereBufferGeometry';
45
46 this.parameters = {
47 radius: radius,
48 widthSegments: widthSegments,
49 heightSegments: heightSegments,
50 phiStart: phiStart,
51 phiLength: phiLength,
52 thetaStart: thetaStart,
53 thetaLength: thetaLength
54 };
55
56 radius = radius || 1;
57
58 widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
59 heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
60
61 phiStart = phiStart !== undefined ? phiStart : 0;
62 phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
63
64 thetaStart = thetaStart !== undefined ? thetaStart : 0;
65 thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
66
67 var thetaEnd = thetaStart + thetaLength;
68
69 var ix, iy;
70
71 var index = 0;
72 var grid = [];
73
74 var vertex = new Vector3();
75 var normal = new Vector3();
76
77 // buffers
78
79 var indices = [];
80 var vertices = [];
81 var normals = [];
82 var uvs = [];
83
84 // generate vertices, normals and uvs
85
86 for ( iy = 0; iy <= heightSegments; iy ++ ) {
87
88 var verticesRow = [];
89
90 var v = iy / heightSegments;
91
92 for ( ix = 0; ix <= widthSegments; ix ++ ) {
93
94 var u = ix / widthSegments;
95
96 // vertex
97
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 );
101
102 vertices.push( vertex.x, vertex.y, vertex.z );
103
104 // normal
105
106 normal.set( vertex.x, vertex.y, vertex.z ).normalize();
107 normals.push( normal.x, normal.y, normal.z );
108
109 // uv
110
111 uvs.push( u, 1 - v );
112
113 verticesRow.push( index ++ );
114
115 }
116
117 grid.push( verticesRow );
118
119 }
120
121 // indices
122
123 for ( iy = 0; iy < heightSegments; iy ++ ) {
124
125 for ( ix = 0; ix < widthSegments; ix ++ ) {
126
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 ];
131
132 if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d );
133 if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d );
134
135 }
136
137 }
138
139 // build geometry
140
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 ) );
145
146}
147
148SphereBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
149SphereBufferGeometry.prototype.constructor = SphereBufferGeometry;
150
151
152export { SphereGeometry, SphereBufferGeometry };