TorusGeometry.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. THREE.TorusGeometry = function ( radius, tube, radialSegments, tubularSegments, arc ) {
  7. THREE.Geometry.call( this );
  8. this.type = 'TorusGeometry';
  9. this.parameters = {
  10. radius: radius,
  11. tube: tube,
  12. radialSegments: radialSegments,
  13. tubularSegments: tubularSegments,
  14. arc: arc
  15. };
  16. radius = radius || 100;
  17. tube = tube || 40;
  18. radialSegments = radialSegments || 8;
  19. tubularSegments = tubularSegments || 6;
  20. arc = arc || Math.PI * 2;
  21. var center = new THREE.Vector3(), uvs = [], normals = [];
  22. for ( var j = 0; j <= radialSegments; j ++ ) {
  23. for ( var i = 0; i <= tubularSegments; i ++ ) {
  24. var u = i / tubularSegments * arc;
  25. var v = j / radialSegments * Math.PI * 2;
  26. center.x = radius * Math.cos( u );
  27. center.y = radius * Math.sin( u );
  28. var vertex = new THREE.Vector3();
  29. vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
  30. vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
  31. vertex.z = tube * Math.sin( v );
  32. this.vertices.push( vertex );
  33. uvs.push( new THREE.Vector2( i / tubularSegments, j / radialSegments ) );
  34. normals.push( vertex.clone().sub( center ).normalize() );
  35. }
  36. }
  37. for ( var j = 1; j <= radialSegments; j ++ ) {
  38. for ( var i = 1; i <= tubularSegments; i ++ ) {
  39. var a = ( tubularSegments + 1 ) * j + i - 1;
  40. var b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
  41. var c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
  42. var d = ( tubularSegments + 1 ) * j + i;
  43. var face = new THREE.Face3( a, b, d, [ normals[ a ].clone(), normals[ b ].clone(), normals[ d ].clone() ] );
  44. this.faces.push( face );
  45. this.faceVertexUvs[ 0 ].push( [ uvs[ a ].clone(), uvs[ b ].clone(), uvs[ d ].clone() ] );
  46. face = new THREE.Face3( b, c, d, [ normals[ b ].clone(), normals[ c ].clone(), normals[ d ].clone() ] );
  47. this.faces.push( face );
  48. this.faceVertexUvs[ 0 ].push( [ uvs[ b ].clone(), uvs[ c ].clone(), uvs[ d ].clone() ] );
  49. }
  50. }
  51. this.computeFaceNormals();
  52. };
  53. THREE.TorusGeometry.prototype = Object.create( THREE.Geometry.prototype );
  54. THREE.TorusGeometry.prototype.constructor = THREE.TorusGeometry;
  55. THREE.TorusGeometry.prototype.clone = function () {
  56. var geometry = new THREE.TorusGeometry(
  57. this.parameters.radius,
  58. this.parameters.tube,
  59. this.parameters.radialSegments,
  60. this.parameters.tubularSegments,
  61. this.parameters.arc
  62. );
  63. return geometry;
  64. };