Skip to content

Commit fdf8022

Browse files
committed
Sphere*Geometry clean up.
1 parent 1f54435 commit fdf8022

2 files changed

Lines changed: 24 additions & 36 deletions

File tree

src/extras/geometries/SphereBufferGeometry.js

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ THREE.SphereBufferGeometry = function ( radius, widthSegments, heightSegments, p
1616
phiStart: phiStart,
1717
phiLength: phiLength,
1818
thetaStart: thetaStart,
19-
thetaLength: thetaLength
19+
thetaLength: thetaLength
2020
};
2121

2222
radius = radius || 50;
2323

24-
widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
24+
widthSegments = Math.max( 2, Math.floor( widthSegments ) || 8 );
2525
heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
2626

2727
phiStart = phiStart !== undefined ? phiStart : 0;
@@ -36,25 +36,21 @@ THREE.SphereBufferGeometry = function ( radius, widthSegments, heightSegments, p
3636
var normals = new THREE.BufferAttribute( new Float32Array( vertexCount * 3 ), 3);
3737
var uvs = new THREE.BufferAttribute( new Float32Array( vertexCount * 2 ), 2 );
3838

39-
this.addAttribute( 'position', positions );
40-
this.addAttribute( 'normal', normals );
41-
this.addAttribute( 'uv', uvs );
42-
43-
var x, y, u, v, px, py, pz, index = 0, vertices = [], normal = new THREE.Vector3();
39+
var index = 0, vertices = [], normal = new THREE.Vector3();
4440

45-
for ( y = 0; y <= heightSegments; y ++ ) {
41+
for ( var y = 0; y <= heightSegments; y ++ ) {
4642

4743
var verticesRow = [];
4844

49-
v = y / heightSegments;
45+
var v = y / heightSegments;
5046

51-
for ( x = 0; x <= widthSegments; x ++ ) {
47+
for ( var x = 0; x <= widthSegments; x ++ ) {
5248

53-
u = x / widthSegments;
49+
var u = x / widthSegments;
5450

55-
px = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
56-
py = radius * Math.cos( thetaStart + v * thetaLength );
57-
pz = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
51+
var px = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
52+
var py = radius * Math.cos( thetaStart + v * thetaLength );
53+
var pz = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
5854

5955
normal.set( px, py, pz ).normalize();
6056

@@ -73,39 +69,31 @@ THREE.SphereBufferGeometry = function ( radius, widthSegments, heightSegments, p
7369
}
7470

7571
var indices = [];
76-
var ul;
77-
for ( y = 0, ul = heightSegments - 1; y < ul; y++ ) {
7872

79-
for ( x = 0; x < widthSegments; x++ ) {
73+
for ( var y = 0; y < heightSegments; y ++ ) {
8074

81-
var v1 = vertices[y][x + 1];
82-
var v2 = vertices[y][x];
83-
var v3 = vertices[y + 1][x];
84-
var v4 = vertices[y + 1][x + 1];
75+
for ( var x = 0; x < widthSegments; x ++ ) {
76+
77+
var v1 = vertices[ y ][ x + 1 ];
78+
var v2 = vertices[ y ][ x ];
79+
var v3 = vertices[ y + 1 ][ x ];
80+
var v4 = vertices[ y + 1 ][ x + 1 ];
8581

8682
if ( y !== 0 ) indices.push( v1, v2, v4 );
87-
indices.push( v2, v3, v4 );
83+
if ( y !== heightSegments - 1 ) indices.push( v2, v3, v4 );
8884

8985
}
90-
}
91-
92-
y = heightSegments;
93-
94-
for ( x = 0; x < widthSegments; x++ ) {
95-
96-
var v2 = vertices[y][x];
97-
var v3 = vertices[y - 1][x];
98-
var v4 = vertices[y - 1][x + 1];
99-
100-
indices.push( v2, v4, v3 );
10186

10287
}
10388

10489
this.addAttribute( 'index', new THREE.BufferAttribute( new Uint16Array( indices ), 1 ) );
90+
this.addAttribute( 'position', positions );
91+
this.addAttribute( 'normal', normals );
92+
this.addAttribute( 'uv', uvs );
10593

10694
this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );
10795

10896
};
10997

11098
THREE.SphereBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
111-
THREE.SphereBufferGeometry.prototype.constructor = THREE.SphereBufferGeometry;
99+
THREE.SphereBufferGeometry.prototype.constructor = THREE.SphereBufferGeometry;

src/extras/geometries/SphereGeometry.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ THREE.SphereGeometry = function ( radius, widthSegments, heightSegments, phiStar
1717
phiStart: phiStart,
1818
phiLength: phiLength,
1919
thetaStart: thetaStart,
20-
thetaLength: thetaLength
20+
thetaLength: thetaLength
2121
};
2222

2323
radius = radius || 50;
2424

25-
widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
25+
widthSegments = Math.max( 2, Math.floor( widthSegments ) || 8 );
2626
heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
2727

2828
phiStart = phiStart !== undefined ? phiStart : 0;

0 commit comments

Comments
 (0)