From ea9492b882316ec261069a6700e52c4543672f2b Mon Sep 17 00:00:00 2001 From: astrodud Date: Fri, 7 Jan 2011 21:46:45 -0800 Subject: [PATCH] added LathedObject which creates a geometry by rotating a set of Vertices around the z axis. UV mapping is supported. See http://astrodud.isgreat.org/asteroids for an example. --- src/extras/primitives/LathedObject.js | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/extras/primitives/LathedObject.js diff --git a/src/extras/primitives/LathedObject.js b/src/extras/primitives/LathedObject.js new file mode 100644 index 00000000..4f6debf4 --- /dev/null +++ b/src/extras/primitives/LathedObject.js @@ -0,0 +1,45 @@ +/** + * @author astrodud / http://astrodud.isgreat.org/ + */ + +function LathedObject( verts, nsteps, latheAngle ) { + THREE.Geometry.call( this ); + nsteps = nsteps || 12; + latheAngle = latheAngle || 2 * Math.PI; + var stepSize = latheAngle / nsteps; + var newV = [], oldInds = [], newInds = [], startInds = []; + for ( var j = 0; j < verts.length; j ++ ) { + this.vertices.push( new THREE.Vertex( verts[ j ] ) ); + oldInds[ j ] = this.vertices.length - 1; + newV[ j ] = new THREE.Vector3( verts[ j ].x, verts[ j ].y, verts[ j ].z ); + } + var m = THREE.Matrix4.rotationZMatrix( stepSize ); + for ( var r = 0; r <= latheAngle + 0.001; r += stepSize ) { // need the +0.001 to it go up to latheAngle + for ( var j = 0; j < newV.length; j ++ ) { + if ( r < latheAngle ) { + newV[ j ] = transformVector3( newV[ j ], m ); + this.vertices.push( new THREE.Vertex( newV[ j ] ) ); + newInds[ j ] = this.vertices.length - 1; + } else { + newInds = startInds; // wrap it up! + } + } + if ( r == 0 ) startInds = oldInds; + for ( var j = 0; j < oldInds.length - 1; j ++ ) { + this.faces.push( new THREE.Face4( newInds[ j ], newInds[ j + 1 ], oldInds[ j + 1 ], oldInds[ j ] ) ); + this.uvs.push( [ new THREE.UV( r / latheAngle, j / verts.length ), + new THREE.UV( r / latheAngle, ( j + 1 ) / verts.length ), + new THREE.UV( ( r - stepSize ) / latheAngle, ( j + 1 ) / verts.length ), + new THREE.UV( ( r - stepSize ) / latheAngle, j / verts.length ) ] ); + } + oldInds = newInds; + newInds = []; + } + this.computeCentroids(); + this.computeFaceNormals(); + this.computeVertexNormals(); + this.sortFacesByMaterial(); +}; + +LathedObject.prototype = new THREE.Geometry(); +LathedObject.prototype.constructor = LathedObject;