Browse Source

CylinderBufferGeometry with groups

CylinderBufferGeometry with 3 different textures when used with

Multimaterial
rfm1201 9 years ago
parent
commit
91b6926536
1 changed files with 34 additions and 7 deletions
  1. 34 7
      src/extras/geometries/CylinderBufferGeometry.js

+ 34 - 7
src/extras/geometries/CylinderBufferGeometry.js

@@ -2,7 +2,7 @@
  * @author Mugen87 / https://github.com/Mugen87
  * @author Mugen87 / https://github.com/Mugen87
  */
  */
 
 
-THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
+THREE.CylinderBufferGeometry = function( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
 
 
 	THREE.BufferGeometry.call( this );
 	THREE.BufferGeometry.call( this );
 
 
@@ -19,11 +19,13 @@ THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radia
 		thetaLength: thetaLength
 		thetaLength: thetaLength
 	};
 	};
 
 
+	var scope = this;
+
 	radiusTop = radiusTop !== undefined ? radiusTop : 20;
 	radiusTop = radiusTop !== undefined ? radiusTop : 20;
 	radiusBottom = radiusBottom !== undefined ? radiusBottom : 20;
 	radiusBottom = radiusBottom !== undefined ? radiusBottom : 20;
 	height = height !== undefined ? height : 100;
 	height = height !== undefined ? height : 100;
 
 
-	radialSegments = Math.floor( radialSegments )  || 8;
+	radialSegments = Math.floor( radialSegments ) || 8;
 	heightSegments = Math.floor( heightSegments ) || 1;
 	heightSegments = Math.floor( heightSegments ) || 1;
 
 
 	openEnded = openEnded !== undefined ? openEnded : false;
 	openEnded = openEnded !== undefined ? openEnded : false;
@@ -46,6 +48,9 @@ THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radia
 
 
 	var index = 0, indexOffset = 0, indexArray = [], halfHeight = height / 2;
 	var index = 0, indexOffset = 0, indexArray = [], halfHeight = height / 2;
 
 
+	// group variables
+	var groupStart = 0;
+
 	// generate geometry
 	// generate geometry
 
 
 	generateTorso();
 	generateTorso();
@@ -66,7 +71,7 @@ THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radia
 
 
 	// helper functions
 	// helper functions
 
 
-	function calculateVertexCount () {
+	function calculateVertexCount() {
 
 
 		var count = ( radialSegments + 1 ) * ( heightSegments + 1 );
 		var count = ( radialSegments + 1 ) * ( heightSegments + 1 );
 
 
@@ -80,7 +85,7 @@ THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radia
 
 
 	}
 	}
 
 
-	function calculateIndexCount () {
+	function calculateIndexCount() {
 
 
 		var count = radialSegments * heightSegments * 2 * 3;
 		var count = radialSegments * heightSegments * 2 * 3;
 
 
@@ -94,12 +99,14 @@ THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radia
 
 
 	}
 	}
 
 
-	function generateTorso () {
+	function generateTorso() {
 
 
 		var x, y;
 		var x, y;
 		var normal = new THREE.Vector3();
 		var normal = new THREE.Vector3();
 		var vertex = new THREE.Vector3();
 		var vertex = new THREE.Vector3();
 
 
+		var groupCount = 0;
+
 		// this will be used to calculate the normal
 		// this will be used to calculate the normal
 		var tanTheta = ( radiusBottom - radiusTop ) / height;
 		var tanTheta = ( radiusBottom - radiusTop ) / height;
 
 
@@ -128,7 +135,7 @@ THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radia
 				normal.copy( vertex );
 				normal.copy( vertex );
 
 
 				// handle special case if radiusTop/radiusBottom is zero
 				// handle special case if radiusTop/radiusBottom is zero
-				if ( ( radiusTop === 0  && y === 0 ) || ( radiusBottom === 0  && y === heightSegments ) ) {
+				if ( ( radiusTop === 0 && y === 0 ) || ( radiusBottom === 0 && y === heightSegments ) ) {
 
 
 					normal.x = Math.sin( u * thetaLength + thetaStart );
 					normal.x = Math.sin( u * thetaLength + thetaStart );
 					normal.z = Math.cos( u * thetaLength + thetaStart );
 					normal.z = Math.cos( u * thetaLength + thetaStart );
@@ -176,18 +183,29 @@ THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radia
 				indices.setX( indexOffset, i3 ); indexOffset ++;
 				indices.setX( indexOffset, i3 ); indexOffset ++;
 				indices.setX( indexOffset, i4 ); indexOffset ++;
 				indices.setX( indexOffset, i4 ); indexOffset ++;
 
 
+				// update counters
+				groupCount += 6;
+
 			}
 			}
 
 
 		}
 		}
 
 
+		// add a group to the geometry. this will ensure multi material support
+		scope.addGroup( groupStart, groupCount, 0 );
+
+		// calculate new start value for groups
+		groupStart += groupCount;
+
 	}
 	}
 
 
-	function generateCap ( top ) {
+	function generateCap( top ) {
 
 
 		var x, centerIndexStart, centerIndexEnd;
 		var x, centerIndexStart, centerIndexEnd;
 		var uv = new THREE.Vector2();
 		var uv = new THREE.Vector2();
 		var vertex = new THREE.Vector3();
 		var vertex = new THREE.Vector3();
 
 
+		var groupCount = 0;
+
 		var radius = ( top === true ) ? radiusTop : radiusBottom;
 		var radius = ( top === true ) ? radiusTop : radiusBottom;
 		var sign = ( top === true ) ? 1 : - 1;
 		var sign = ( top === true ) ? 1 : - 1;
 
 
@@ -275,8 +293,17 @@ THREE.CylinderBufferGeometry = function ( radiusTop, radiusBottom, height, radia
 
 
 			}
 			}
 
 
+			// update counters
+			groupCount += 3;
+
 		}
 		}
 
 
+		// add a group to the geometry. this will ensure multi material support
+		scope.addGroup( groupStart, groupCount, top === true ? 1 : 2 );
+
+		// calculate new start value for groups
+		groupStart += groupCount;
+
 	}
 	}
 
 
 };
 };