2
0

CylinderGeometry.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.CylinderGeometry = function ( radiusTop, radiusBottom, height, segmentsRadius, segmentsHeight ) {
  5. THREE.Geometry.call( this );
  6. var radiusTop = radiusTop != null ? radiusTop : 20;
  7. var radiusBottom = radiusBottom != null ? radiusBottom : 20;
  8. var height = 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( new THREE.Vector3( 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. var n1 = this.vertices[ v1 ].position.clone().setY( 0 ).normalize();
  37. var n2 = this.vertices[ v2 ].position.clone().setY( 0 ).normalize();
  38. var n3 = this.vertices[ v3 ].position.clone().setY( 0 ).normalize();
  39. var n4 = this.vertices[ v4 ].position.clone().setY( 0 ).normalize();
  40. var uv1 = uvs[ y ][ x ].clone();
  41. var uv2 = uvs[ y + 1 ][ x ].clone();
  42. var uv3 = uvs[ y + 1 ][ x + 1 ].clone();
  43. var uv4 = uvs[ y ][ x + 1 ].clone();
  44. this.faces.push( new THREE.Face4( v1, v2, v3, v4, [ n1, n2, n3, n4 ] ) );
  45. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3, uv4 ] );
  46. }
  47. }
  48. // top cap
  49. if ( radiusTop > 0 ) {
  50. this.vertices.push( new THREE.Vertex( new THREE.Vector3( 0, heightHalf, 0 ) ) );
  51. for ( x = 0; x < segmentsX; x ++ ) {
  52. var v1 = vertices[ 0 ][ x ];
  53. var v2 = vertices[ 0 ][ x + 1 ];
  54. var v3 = this.vertices.length - 1;
  55. var n1 = new THREE.Vector3( 0, 1, 0 );
  56. var n2 = new THREE.Vector3( 0, 1, 0 );
  57. var n3 = new THREE.Vector3( 0, 1, 0 );
  58. var uv1 = uvs[ 0 ][ x ].clone();
  59. var uv2 = uvs[ 0 ][ x + 1 ].clone();
  60. var uv3 = new THREE.UV( uv2.u, 0 );
  61. this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
  62. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
  63. }
  64. }
  65. // bottom cap
  66. if ( radiusBottom > 0 ) {
  67. this.vertices.push( new THREE.Vertex( new THREE.Vector3( 0, - heightHalf, 0 ) ) );
  68. for ( x = 0; x < segmentsX; x ++ ) {
  69. var v1 = vertices[ y ][ x + 1 ];
  70. var v2 = vertices[ y ][ x ];
  71. var v3 = this.vertices.length - 1;
  72. var n1 = new THREE.Vector3( 0, - 1, 0 );
  73. var n2 = new THREE.Vector3( 0, - 1, 0 );
  74. var n3 = new THREE.Vector3( 0, - 1, 0 );
  75. var uv1 = uvs[ y ][ x + 1 ].clone();
  76. var uv2 = uvs[ y ][ x ].clone();
  77. var uv3 = new THREE.UV( uv2.u, 1 );
  78. this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
  79. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
  80. }
  81. }
  82. this.computeCentroids();
  83. this.computeFaceNormals();
  84. }
  85. THREE.CylinderGeometry.prototype = new THREE.Geometry();
  86. THREE.CylinderGeometry.prototype.constructor = THREE.CylinderGeometry;