[28897] | 1 | /**
|
---|
| 2 | * @author oosmoxiecode
|
---|
| 3 | * @author mrdoob / http://mrdoob.com/
|
---|
| 4 | * based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3DLite/src/away3dlite/primitives/Torus.as?r=2888
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | THREE.TorusGeometry = function ( radius, tube, radialSegments, tubularSegments, arc ) {
|
---|
| 8 |
|
---|
| 9 | THREE.Geometry.call( this );
|
---|
| 10 |
|
---|
| 11 | var scope = this;
|
---|
| 12 |
|
---|
| 13 | this.radius = radius || 100;
|
---|
| 14 | this.tube = tube || 40;
|
---|
| 15 | this.radialSegments = radialSegments || 8;
|
---|
| 16 | this.tubularSegments = tubularSegments || 6;
|
---|
| 17 | this.arc = arc || Math.PI * 2;
|
---|
| 18 |
|
---|
| 19 | var center = new THREE.Vector3(), uvs = [], normals = [];
|
---|
| 20 |
|
---|
| 21 | for ( var j = 0; j <= this.radialSegments; j ++ ) {
|
---|
| 22 |
|
---|
| 23 | for ( var i = 0; i <= this.tubularSegments; i ++ ) {
|
---|
| 24 |
|
---|
| 25 | var u = i / this.tubularSegments * this.arc;
|
---|
| 26 | var v = j / this.radialSegments * Math.PI * 2;
|
---|
| 27 |
|
---|
| 28 | center.x = this.radius * Math.cos( u );
|
---|
| 29 | center.y = this.radius * Math.sin( u );
|
---|
| 30 |
|
---|
| 31 | var vertex = new THREE.Vector3();
|
---|
| 32 | vertex.x = ( this.radius + this.tube * Math.cos( v ) ) * Math.cos( u );
|
---|
| 33 | vertex.y = ( this.radius + this.tube * Math.cos( v ) ) * Math.sin( u );
|
---|
| 34 | vertex.z = this.tube * Math.sin( v );
|
---|
| 35 |
|
---|
| 36 | this.vertices.push( vertex );
|
---|
| 37 |
|
---|
| 38 | uvs.push( new THREE.Vector2( i / this.tubularSegments, j / this.radialSegments ) );
|
---|
| 39 | normals.push( vertex.clone().sub( center ).normalize() );
|
---|
| 40 |
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | for ( var j = 1; j <= this.radialSegments; j ++ ) {
|
---|
| 47 |
|
---|
| 48 | for ( var i = 1; i <= this.tubularSegments; i ++ ) {
|
---|
| 49 |
|
---|
| 50 | var a = ( this.tubularSegments + 1 ) * j + i - 1;
|
---|
| 51 | var b = ( this.tubularSegments + 1 ) * ( j - 1 ) + i - 1;
|
---|
| 52 | var c = ( this.tubularSegments + 1 ) * ( j - 1 ) + i;
|
---|
| 53 | var d = ( this.tubularSegments + 1 ) * j + i;
|
---|
| 54 |
|
---|
| 55 | var face = new THREE.Face3( a, b, d, [ normals[ a ].clone(), normals[ b ].clone(), normals[ d ].clone() ] );
|
---|
| 56 | this.faces.push( face );
|
---|
| 57 | this.faceVertexUvs[ 0 ].push( [ uvs[ a ].clone(), uvs[ b ].clone(), uvs[ d ].clone() ] );
|
---|
| 58 |
|
---|
| 59 | face = new THREE.Face3( b, c, d, [ normals[ b ].clone(), normals[ c ].clone(), normals[ d ].clone() ] );
|
---|
| 60 | this.faces.push( face );
|
---|
| 61 | this.faceVertexUvs[ 0 ].push( [ uvs[ b ].clone(), uvs[ c ].clone(), uvs[ d ].clone() ] );
|
---|
| 62 |
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | this.computeCentroids();
|
---|
| 68 | this.computeFaceNormals();
|
---|
| 69 |
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | THREE.TorusGeometry.prototype = Object.create( THREE.Geometry.prototype );
|
---|