1 | /**
|
---|
2 | * @author mrdoob / http://mrdoob.com/
|
---|
3 | */
|
---|
4 |
|
---|
5 | THREE.CylinderGeometry = function ( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded ) {
|
---|
6 |
|
---|
7 | THREE.Geometry.call( this );
|
---|
8 |
|
---|
9 | this.radiusTop = radiusTop = radiusTop !== undefined ? radiusTop : 20;
|
---|
10 | this.radiusBottom = radiusBottom = radiusBottom !== undefined ? radiusBottom : 20;
|
---|
11 | this.height = height = height !== undefined ? height : 100;
|
---|
12 |
|
---|
13 | this.radialSegments = radialSegments = radialSegments || 8;
|
---|
14 | this.heightSegments = heightSegments = heightSegments || 1;
|
---|
15 |
|
---|
16 | this.openEnded = openEnded = openEnded !== undefined ? openEnded : false;
|
---|
17 |
|
---|
18 | var heightHalf = height / 2;
|
---|
19 |
|
---|
20 | var x, y, vertices = [], uvs = [];
|
---|
21 |
|
---|
22 | for ( y = 0; y <= heightSegments; y ++ ) {
|
---|
23 |
|
---|
24 | var verticesRow = [];
|
---|
25 | var uvsRow = [];
|
---|
26 |
|
---|
27 | var v = y / heightSegments;
|
---|
28 | var radius = v * ( radiusBottom - radiusTop ) + radiusTop;
|
---|
29 |
|
---|
30 | for ( x = 0; x <= radialSegments; x ++ ) {
|
---|
31 |
|
---|
32 | var u = x / radialSegments;
|
---|
33 |
|
---|
34 | var vertex = new THREE.Vector3();
|
---|
35 | vertex.x = radius * Math.sin( u * Math.PI * 2 );
|
---|
36 | vertex.y = - v * height + heightHalf;
|
---|
37 | vertex.z = radius * Math.cos( u * Math.PI * 2 );
|
---|
38 |
|
---|
39 | this.vertices.push( vertex );
|
---|
40 |
|
---|
41 | verticesRow.push( this.vertices.length - 1 );
|
---|
42 | uvsRow.push( new THREE.Vector2( u, 1 - v ) );
|
---|
43 |
|
---|
44 | }
|
---|
45 |
|
---|
46 | vertices.push( verticesRow );
|
---|
47 | uvs.push( uvsRow );
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 | var tanTheta = ( radiusBottom - radiusTop ) / height;
|
---|
52 | var na, nb;
|
---|
53 |
|
---|
54 | for ( x = 0; x < radialSegments; x ++ ) {
|
---|
55 |
|
---|
56 | if ( radiusTop !== 0 ) {
|
---|
57 |
|
---|
58 | na = this.vertices[ vertices[ 0 ][ x ] ].clone();
|
---|
59 | nb = this.vertices[ vertices[ 0 ][ x + 1 ] ].clone();
|
---|
60 |
|
---|
61 | } else {
|
---|
62 |
|
---|
63 | na = this.vertices[ vertices[ 1 ][ x ] ].clone();
|
---|
64 | nb = this.vertices[ vertices[ 1 ][ x + 1 ] ].clone();
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | na.setY( Math.sqrt( na.x * na.x + na.z * na.z ) * tanTheta ).normalize();
|
---|
69 | nb.setY( Math.sqrt( nb.x * nb.x + nb.z * nb.z ) * tanTheta ).normalize();
|
---|
70 |
|
---|
71 | for ( y = 0; y < heightSegments; y ++ ) {
|
---|
72 |
|
---|
73 | var v1 = vertices[ y ][ x ];
|
---|
74 | var v2 = vertices[ y + 1 ][ x ];
|
---|
75 | var v3 = vertices[ y + 1 ][ x + 1 ];
|
---|
76 | var v4 = vertices[ y ][ x + 1 ];
|
---|
77 |
|
---|
78 | var n1 = na.clone();
|
---|
79 | var n2 = na.clone();
|
---|
80 | var n3 = nb.clone();
|
---|
81 | var n4 = nb.clone();
|
---|
82 |
|
---|
83 | var uv1 = uvs[ y ][ x ].clone();
|
---|
84 | var uv2 = uvs[ y + 1 ][ x ].clone();
|
---|
85 | var uv3 = uvs[ y + 1 ][ x + 1 ].clone();
|
---|
86 | var uv4 = uvs[ y ][ x + 1 ].clone();
|
---|
87 |
|
---|
88 | this.faces.push( new THREE.Face3( v1, v2, v4, [ n1, n2, n4 ] ) );
|
---|
89 | this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv4 ] );
|
---|
90 |
|
---|
91 | this.faces.push( new THREE.Face3( v2, v3, v4, [ n2.clone(), n3, n4.clone() ] ) );
|
---|
92 | this.faceVertexUvs[ 0 ].push( [ uv2.clone(), uv3, uv4.clone() ] );
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | // top cap
|
---|
99 |
|
---|
100 | if ( openEnded === false && radiusTop > 0 ) {
|
---|
101 |
|
---|
102 | this.vertices.push( new THREE.Vector3( 0, heightHalf, 0 ) );
|
---|
103 |
|
---|
104 | for ( x = 0; x < radialSegments; x ++ ) {
|
---|
105 |
|
---|
106 | var v1 = vertices[ 0 ][ x ];
|
---|
107 | var v2 = vertices[ 0 ][ x + 1 ];
|
---|
108 | var v3 = this.vertices.length - 1;
|
---|
109 |
|
---|
110 | var n1 = new THREE.Vector3( 0, 1, 0 );
|
---|
111 | var n2 = new THREE.Vector3( 0, 1, 0 );
|
---|
112 | var n3 = new THREE.Vector3( 0, 1, 0 );
|
---|
113 |
|
---|
114 | var uv1 = uvs[ 0 ][ x ].clone();
|
---|
115 | var uv2 = uvs[ 0 ][ x + 1 ].clone();
|
---|
116 | var uv3 = new THREE.Vector2( uv2.x, 0 );
|
---|
117 |
|
---|
118 | this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
|
---|
119 | this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | // bottom cap
|
---|
126 |
|
---|
127 | if ( openEnded === false && radiusBottom > 0 ) {
|
---|
128 |
|
---|
129 | this.vertices.push( new THREE.Vector3( 0, - heightHalf, 0 ) );
|
---|
130 |
|
---|
131 | for ( x = 0; x < radialSegments; x ++ ) {
|
---|
132 |
|
---|
133 | var v1 = vertices[ y ][ x + 1 ];
|
---|
134 | var v2 = vertices[ y ][ x ];
|
---|
135 | var v3 = this.vertices.length - 1;
|
---|
136 |
|
---|
137 | var n1 = new THREE.Vector3( 0, - 1, 0 );
|
---|
138 | var n2 = new THREE.Vector3( 0, - 1, 0 );
|
---|
139 | var n3 = new THREE.Vector3( 0, - 1, 0 );
|
---|
140 |
|
---|
141 | var uv1 = uvs[ y ][ x + 1 ].clone();
|
---|
142 | var uv2 = uvs[ y ][ x ].clone();
|
---|
143 | var uv3 = new THREE.Vector2( uv2.x, 1 );
|
---|
144 |
|
---|
145 | this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
|
---|
146 | this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
|
---|
147 |
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|
151 |
|
---|
152 | this.computeCentroids();
|
---|
153 | this.computeFaceNormals();
|
---|
154 |
|
---|
155 | }
|
---|
156 |
|
---|
157 | THREE.CylinderGeometry.prototype = Object.create( THREE.Geometry.prototype );
|
---|