forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSphereGeometry.js
96 lines (61 loc) · 2.69 KB
/
SphereGeometry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @author mr.doob / http://mrdoob.com/
*/
THREE.SphereGeometry = function ( radius, segmentsWidth, segmentsHeight, phiStart, phiLength, thetaStart, thetaLength ) {
THREE.Geometry.call( this );
radius = radius || 50;
phiStart = phiStart !== undefined ? phiStart : 0;
phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
thetaStart = thetaStart !== undefined ? thetaStart : 0;
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
var segmentsX = Math.max( 3, Math.floor( segmentsWidth ) || 8 );
var segmentsY = Math.max( 2, Math.floor( segmentsHeight ) || 6 );
var x, y, vertices = [], uvs = [];
for ( y = 0; y <= segmentsY; y ++ ) {
var verticesRow = [];
var uvsRow = [];
for ( x = 0; x <= segmentsX; x ++ ) {
var u = x / segmentsX;
var v = y / segmentsY;
var xpos = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
var ypos = radius * Math.cos( thetaStart + v * thetaLength );
var zpos = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
this.vertices.push( new THREE.Vertex( new THREE.Vector3( xpos, ypos, zpos ) ) );
verticesRow.push( this.vertices.length - 1 );
uvsRow.push( new THREE.UV( u, v ) );
}
vertices.push( verticesRow );
uvs.push( uvsRow );
}
for ( y = 0; y < segmentsY; y ++ ) {
for ( x = 0; x < segmentsX; x ++ ) {
var v1 = vertices[ y ][ x + 1 ];
var v2 = vertices[ y ][ x ];
var v3 = vertices[ y + 1 ][ x ];
var v4 = vertices[ y + 1 ][ x + 1 ];
var n1 = this.vertices[ v1 ].position.clone().normalize();
var n2 = this.vertices[ v2 ].position.clone().normalize();
var n3 = this.vertices[ v3 ].position.clone().normalize();
var n4 = this.vertices[ v4 ].position.clone().normalize();
var uv1 = uvs[ y ][ x + 1 ].clone();
var uv2 = uvs[ y ][ x ].clone();
var uv3 = uvs[ y + 1 ][ x ].clone();
var uv4 = uvs[ y + 1 ][ x + 1 ].clone();
if ( Math.abs( this.vertices[ v1 ].position.y ) == radius ) {
this.faces.push( new THREE.Face3( v1, v3, v4, [ n1, n3, n4 ] ) );
this.faceVertexUvs[ 0 ].push( [ uv1, uv3, uv4 ] );
} else if ( Math.abs( this.vertices[ v3 ].position.y ) == radius ) {
this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
} else {
this.faces.push( new THREE.Face4( v1, v2, v3, v4, [ n1, n2, n3, n4 ] ) );
this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3, uv4 ] );
}
}
}
this.computeCentroids();
this.computeFaceNormals();
this.boundingSphere = { radius: radius };
};
THREE.SphereGeometry.prototype = new THREE.Geometry();
THREE.SphereGeometry.prototype.constructor = THREE.SphereGeometry;