TorusGeometry.js 1.8 KB
Newer Older
1 2 3 4 5
/**
 * @author oosmoxiecode
 * based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3DLite/src/away3dlite/primitives/Torus.as?r=2888
 */

6
THREE.TorusGeometry = function ( radius, tube, segmentsR, segmentsT, arc ) {
7 8

	THREE.Geometry.call( this );
9 10 11 12 13 14 15

	var scope = this;

	this.radius = radius || 100;
	this.tube = tube || 40;
	this.segmentsR = segmentsR || 8;
	this.segmentsT = segmentsT || 6;
16
	this.arc = arc || (2 * Math.PI);
17 18 19

	var temp_uv = [];

20 21 22
	for ( var j = 0; j <= this.segmentsR; ++j ) {

		for ( var i = 0; i <= this.segmentsT; ++i ) {
23 24
		
			var u = i / this.segmentsT * this.arc;
25 26 27 28
			var v = j / this.segmentsR * 2 * Math.PI;
			var x = (this.radius + this.tube*Math.cos(v))*Math.cos(u);
			var y = (this.radius + this.tube*Math.cos(v))*Math.sin(u);
			var z = this.tube*Math.sin(v);
29

30 31 32
			vert( x, y, z );

			temp_uv.push( [i/this.segmentsT, 1 - j/this.segmentsR] );
33

34 35 36
		}
	}

37

38 39 40 41
	for ( var j = 1; j <= this.segmentsR; ++j ) {

		for ( var i = 1; i <= this.segmentsT; ++i ) {

42 43 44 45
			var a = (this.segmentsT + 1)*j + i;
			var b = (this.segmentsT + 1)*j + i - 1;
			var c = (this.segmentsT + 1)*(j - 1) + i - 1;
			var d = (this.segmentsT + 1)*(j - 1) + i;
46

47
			f4( a, b, c,d );
48

49
			this.faceVertexUvs[ 0 ].push( [new THREE.UV( temp_uv[a][0], temp_uv[a][1] ),
50 51 52 53 54
							new THREE.UV( temp_uv[b][0], temp_uv[b][1] ),
							new THREE.UV( temp_uv[c][0], temp_uv[c][1] ),
							new THREE.UV( temp_uv[d][0], temp_uv[d][1] )
							] );
		}
55

56 57 58 59 60 61 62
	}

	this.computeCentroids();
	this.computeFaceNormals();
	this.computeVertexNormals();

	function vert( x, y, z ) {
63

64
		scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
65 66

	};
67 68

	function f4( a, b, c, d ) {
69

70 71
		scope.faces.push( new THREE.Face4( a, b, c, d ) );

72 73 74
	};

};
75

M
Mr.doob 已提交
76 77
THREE.TorusGeometry.prototype = new THREE.Geometry();
THREE.TorusGeometry.prototype.constructor = THREE.TorusGeometry;