CylinderGeometry.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.CylinderGeometry = function ( radiusTop, radiusBottom, height, segmentsRadius, segmentsHeight, openEnded ) {
  5. THREE.Geometry.call( this );
  6. radiusTop = radiusTop !== undefined ? radiusTop : 20;
  7. radiusBottom = radiusBottom !== undefined ? radiusBottom : 20;
  8. height = height !== undefined ? height : 100;
  9. var heightHalf = height / 2;
  10. var segmentsX = segmentsRadius || 8;
  11. var segmentsY = segmentsHeight || 1;
  12. var x, y, vertices = [], uvs = [];
  13. for ( y = 0; y <= segmentsY; y ++ ) {
  14. var verticesRow = [];
  15. var uvsRow = [];
  16. var v = y / segmentsY;
  17. var radius = v * ( radiusBottom - radiusTop ) + radiusTop;
  18. for ( x = 0; x <= segmentsX; x ++ ) {
  19. var u = x / segmentsX;
  20. var xpos = radius * Math.sin( u * Math.PI * 2 );
  21. var ypos = - v * height + heightHalf;
  22. var zpos = radius * Math.cos( u * Math.PI * 2 );
  23. this.vertices.push( new THREE.Vertex( xpos, ypos, zpos ) );
  24. verticesRow.push( this.vertices.length - 1 );
  25. uvsRow.push( new THREE.UV( u, v ) );
  26. }
  27. vertices.push( verticesRow );
  28. uvs.push( uvsRow );
  29. }
  30. for ( y = 0; y < segmentsY; y ++ ) {
  31. for ( x = 0; x < segmentsX; x ++ ) {
  32. var v1 = vertices[ y ][ x ];
  33. var v2 = vertices[ y + 1 ][ x ];
  34. var v3 = vertices[ y + 1 ][ x + 1 ];
  35. var v4 = vertices[ y ][ x + 1 ];
  36. // FIXME: These normals aren't right for cones.
  37. var n1 = this.vertices[ v1 ].clone().setY( 0 ).normalize();
  38. var n2 = this.vertices[ v2 ].clone().setY( 0 ).normalize();
  39. var n3 = this.vertices[ v3 ].clone().setY( 0 ).normalize();
  40. var n4 = this.vertices[ v4 ].clone().setY( 0 ).normalize();
  41. var uv1 = uvs[ y ][ x ].clone();
  42. var uv2 = uvs[ y + 1 ][ x ].clone();
  43. var uv3 = uvs[ y + 1 ][ x + 1 ].clone();
  44. var uv4 = uvs[ y ][ x + 1 ].clone();
  45. this.faces.push( new THREE.Face4( v1, v2, v3, v4, [ n1, n2, n3, n4 ] ) );
  46. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3, uv4 ] );
  47. }
  48. }
  49. // top cap
  50. if ( !openEnded && radiusTop > 0 ) {
  51. this.vertices.push( new THREE.Vertex( 0, heightHalf, 0 ) );
  52. for ( x = 0; x < segmentsX; x ++ ) {
  53. var v1 = vertices[ 0 ][ x ];
  54. var v2 = vertices[ 0 ][ x + 1 ];
  55. var v3 = this.vertices.length - 1;
  56. var n1 = new THREE.Vector3( 0, 1, 0 );
  57. var n2 = new THREE.Vector3( 0, 1, 0 );
  58. var n3 = new THREE.Vector3( 0, 1, 0 );
  59. var uv1 = uvs[ 0 ][ x ].clone();
  60. var uv2 = uvs[ 0 ][ x + 1 ].clone();
  61. var uv3 = new THREE.UV( uv2.u, 0 );
  62. this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
  63. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
  64. }
  65. }
  66. // bottom cap
  67. if ( !openEnded && radiusBottom > 0 ) {
  68. this.vertices.push( new THREE.Vertex( 0, - heightHalf, 0 ) );
  69. for ( x = 0; x < segmentsX; x ++ ) {
  70. var v1 = vertices[ y ][ x + 1 ];
  71. var v2 = vertices[ y ][ x ];
  72. var v3 = this.vertices.length - 1;
  73. var n1 = new THREE.Vector3( 0, - 1, 0 );
  74. var n2 = new THREE.Vector3( 0, - 1, 0 );
  75. var n3 = new THREE.Vector3( 0, - 1, 0 );
  76. var uv1 = uvs[ y ][ x + 1 ].clone();
  77. var uv2 = uvs[ y ][ x ].clone();
  78. var uv3 = new THREE.UV( uv2.u, 1 );
  79. this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
  80. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
  81. }
  82. }
  83. this.computeCentroids();
  84. this.computeFaceNormals();
  85. }
  86. THREE.CylinderGeometry.prototype = new THREE.Geometry();
  87. THREE.CylinderGeometry.prototype.constructor = THREE.CylinderGeometry;