-
Notifications
You must be signed in to change notification settings - Fork 0
/
TorusGeometry.js
72 lines (48 loc) · 2.12 KB
/
TorusGeometry.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
/**
* @author oosmoxiecode
* @author mrdoob / http://mrdoob.com/
* based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3DLite/src/away3dlite/primitives/Torus.as?r=2888
*/
THREE.TorusGeometry = function ( radius, tube, radialSegments, tubularSegments, arc ) {
THREE.Geometry.call( this );
var scope = this;
this.radius = radius || 100;
this.tube = tube || 40;
this.radialSegments = radialSegments || 8;
this.tubularSegments = tubularSegments || 6;
this.arc = arc || Math.PI * 2;
var center = new THREE.Vector3(), uvs = [], normals = [];
for ( var j = 0; j <= this.radialSegments; j ++ ) {
for ( var i = 0; i <= this.tubularSegments; i ++ ) {
var u = i / this.tubularSegments * this.arc;
var v = j / this.radialSegments * Math.PI * 2;
center.x = this.radius * Math.cos( u );
center.y = this.radius * Math.sin( u );
var vertex = new THREE.Vector3();
vertex.x = ( this.radius + this.tube * Math.cos( v ) ) * Math.cos( u );
vertex.y = ( this.radius + this.tube * Math.cos( v ) ) * Math.sin( u );
vertex.z = this.tube * Math.sin( v );
this.vertices.push( vertex );
uvs.push( new THREE.Vector2( i / this.tubularSegments, j / this.radialSegments ) );
normals.push( vertex.clone().subSelf( center ).normalize() );
}
}
for ( var j = 1; j <= this.radialSegments; j ++ ) {
for ( var i = 1; i <= this.tubularSegments; i ++ ) {
var a = ( this.tubularSegments + 1 ) * j + i - 1;
var b = ( this.tubularSegments + 1 ) * ( j - 1 ) + i - 1;
var c = ( this.tubularSegments + 1 ) * ( j - 1 ) + i;
var d = ( this.tubularSegments + 1 ) * j + i;
var face = new THREE.Face4( a, b, c, d, [ normals[ a ], normals[ b ], normals[ c ], normals[ d ] ] );
face.normal.addSelf( normals[ a ] );
face.normal.addSelf( normals[ b ] );
face.normal.addSelf( normals[ c ] );
face.normal.addSelf( normals[ d ] );
face.normal.normalize();
this.faces.push( face );
this.faceVertexUvs[ 0 ].push( [ uvs[ a ].clone(), uvs[ b ].clone(), uvs[ c ].clone(), uvs[ d ].clone() ] );
}
}
this.computeCentroids();
};
THREE.TorusGeometry.prototype = Object.create( THREE.Geometry.prototype );