Cylinder.js 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/**
 * @author kile / http://kile.stravaganza.org/
 */

var Cylinder = function (numSegs, topRad, botRad, height, topOffset, botOffset) {

	THREE.Geometry.call(this);

	var scope = this, i;

	// VERTICES

	// Top circle vertices
	for (i = 0; i < numSegs; i++) {

		v( 
			Math.sin(2 * 3.1415 * i / numSegs)*topRad,
			Math.cos(2 * 3.1415 * i / numSegs)*topRad,
			0);
	}

	// Bottom circle vertices
	for (i = 0; i < numSegs; i++) {

		v(  
			Math.sin(2 * 3.1415 * i / numSegs)*botRad,
			Math.cos(2 * 3.1415 * i / numSegs)*botRad,
			height);
	}


	// FACES

	// Body	
	for (i = 0; i < numSegs; i++) {

		f4(i, i + numSegs, numSegs + (i + 1) % numSegs, (i + 1) % numSegs, '#ff0000');
	}

	// Bottom circle
	if (botRad != 0) {

		v(0, 0, -topOffset);

		for (i = numSegs; i < numSegs + (numSegs / 2); i++) {

			f4(2 * numSegs,
			(2 * i - 2 * numSegs) % numSegs,
			(2 * i - 2 * numSegs + 1) % numSegs,
			(2 * i - 2 * numSegs + 2) % numSegs);
		}
	}

	// Top circle
	if (topRad != 0) {

		v(0, 0, height + topOffset);

		for (i = numSegs + (numSegs / 2); i < 2 * numSegs; i++) {

			f4(	(2 * i - 2 * numSegs + 2) % numSegs + numSegs,
				(2 * i - 2 * numSegs + 1) % numSegs + numSegs,
				(2 * i - 2 * numSegs) % numSegs+numSegs, 
				2 * numSegs + 1);
		}
	}

	this.computeCentroids();
	this.computeNormals();

	function v(x, y, z) {

		scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
	}

	function f4(a, b, c, d) {

		scope.faces.push( new THREE.Face4( a, b, c, d ) );
	}
}

Cylinder.prototype = new THREE.Geometry();
Cylinder.prototype.constructor = Cylinder;