Browse Source

Merge remote branch 'remotes/upstream/master'

alteredq 14 years ago
parent
commit
15407b01e7

+ 178 - 0
src/extras/primitives/Icosahedron.js

@@ -0,0 +1,178 @@
+/**
+ * @author oosmoxiecode
+ * uvs are messed up in this one, and commented away for now. There is an ugly "seam" by the shared vertices
+ * when it "wraps" around, that needs to be fixed. It´s because they share the first and the last vertices
+ * so it draws the entire texture on the seam-faces, I think...
+ */
+
+var Icosahedron = function (subdivisions) {
+
+	var scope = this;
+	var tempScope = new THREE.Geometry();
+	var tempFaces;
+	this.subdivisions = subdivisions || 0;
+
+	//var temp_uv = [];
+
+	THREE.Geometry.call(this);
+	
+	// create 12 vertices of a Icosahedron
+	var t = (1 + Math.sqrt(5)) / 2;
+
+	v(-1,  t,  0);
+	v( 1,  t,  0);
+	v(-1, -t,  0);
+	v( 1, -t,  0);
+
+	v( 0, -1,  t);
+	v( 0,  1,  t);
+	v( 0, -1, -t);
+	v( 0,  1, -t);
+
+	v( t,  0, -1);
+	v( t,  0,  1);
+	v(-t,  0, -1);
+	v(-t,  0,  1);
+
+	// 5 faces around point 0
+	f3(0, 11, 5, tempScope);
+	f3(0, 5, 1, tempScope);
+	f3(0, 1, 7, tempScope);
+	f3(0, 7, 10, tempScope);
+	f3(0, 10, 11, tempScope);
+
+	// 5 adjacent faces
+	f3(1, 5, 9, tempScope);
+	f3(5, 11, 4, tempScope);
+	f3(11, 10, 2, tempScope);
+	f3(10, 7, 6, tempScope);
+	f3(7, 1, 8, tempScope);
+
+	// 5 faces around point 3
+	f3(3, 9, 4, tempScope);
+	f3(3, 4, 2, tempScope);
+	f3(3, 2, 6, tempScope);
+	f3(3, 6, 8, tempScope);
+	f3(3, 8, 9, tempScope);
+
+	// 5 adjacent faces
+	f3(4, 9, 5, tempScope);
+	f3(2, 4, 11, tempScope);
+	f3(6, 2, 10, tempScope);
+	f3(8, 6, 7, tempScope);
+	f3(9, 8, 1, tempScope);
+
+	// subdivide faces to refine the triangles
+	for (var i=0; i < this.subdivisions; i++) {
+		tempFaces = new THREE.Geometry();
+		for (var tri in tempScope.faces) {
+			// replace each triangle by 4 triangles
+			var a = getMiddlePoint(tempScope.faces[tri].a, tempScope.faces[tri].b);
+			var b = getMiddlePoint(tempScope.faces[tri].b, tempScope.faces[tri].c);
+			var c = getMiddlePoint(tempScope.faces[tri].c, tempScope.faces[tri].a);
+
+			f3(tempScope.faces[tri].a, a, c, tempFaces);
+			f3(tempScope.faces[tri].b, b, a, tempFaces);
+			f3(tempScope.faces[tri].c, c, b, tempFaces);
+			f3(a, b, c, tempFaces);
+		}
+		tempScope.faces = tempFaces.faces;
+		//tempScope.uvs = tempFaces.uvs;
+	}
+
+	scope.faces = tempScope.faces;
+	//scope.uvs = tempScope.uvs;
+
+	delete tempScope;
+	delete tempFaces;
+
+	this.computeCentroids();
+	this.computeFaceNormals();
+	this.computeVertexNormals();
+	this.sortFacesByMaterial();
+
+	function v( x, y, z ) {
+		var length = Math.sqrt(x * x + y * y + z * z);
+		var i = scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x/length, y/length, z/length ) ) );
+
+		//var uv = getUv(x, y, z);
+		//temp_uv.push(uv);
+
+		return i-1;
+	}
+
+	function f3( a, b, c, inscope ) {
+		inscope.faces.push( new THREE.Face3( a, b, c ) );
+		
+		/*inscope.uvs.push( [new THREE.UV( temp_uv[a].u, temp_uv[a].v ),
+						   new THREE.UV( temp_uv[b].u, temp_uv[b].v ),
+						   new THREE.UV( temp_uv[c].u, temp_uv[c].v )
+						  ] );
+		*/
+	}
+
+	function getMiddlePoint(p1,p2) {
+		var pos1 = scope.vertices[p1].position;
+		var pos2 = scope.vertices[p2].position;
+
+		var x = (pos1.x + pos2.x) / 2;
+		var y = (pos1.y + pos2.y) / 2;
+		var z = (pos1.z + pos2.z) / 2;
+		
+		var i = v(x, y, z);
+		return i;
+	}
+
+	/*function getUv(x,y,z) {
+		
+		var u,v;
+		var px,py,pz,d;
+			
+		d = Math.sqrt( x*x+y*y+z*z );
+			
+		px = x/d;
+		py = y/d;
+		pz = z/d;
+
+		var normalisedX = 0;
+		var normalisedZ = -1;
+
+		if (((px * px) + (pz * pz)) > 0) {
+			normalisedX = Math.sqrt((px * px) / ((px * px) + (pz * pz)));
+
+			if (px < 0) {
+				normalisedX = -normalisedX;
+			}
+
+			normalisedZ = Math.sqrt((pz * pz) / ((px * px) + (pz * pz)));
+
+			if (pz < 0)	{
+				normalisedZ = -normalisedZ;
+			}
+		}
+
+		if (normalisedZ == 0) {
+			u = ((normalisedX * Math.PI) / 2);
+		} else {
+			u = Math.atan(normalisedX / normalisedZ);
+
+			if (normalisedZ < 0) {
+				u += Math.PI;
+			}
+		}
+
+		if (u < 0) {
+			u += 2 * Math.PI;
+		}
+
+		u /= 2 * Math.PI;
+		v = (-py + 1) / 2;
+
+		return {u:u,v:v};
+	}*/
+
+}
+
+Icosahedron.prototype = new THREE.Geometry();
+Icosahedron.prototype.constructor = Icosahedron;
+

+ 68 - 0
src/extras/primitives/Torus.js

@@ -0,0 +1,68 @@
+/**
+ * @author oosmoxiecode
+ * based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3DLite/src/away3dlite/primitives/Torus.as?r=2888
+ */
+
+var Torus = function (radius, tube, segmentsR, segmentsT) {
+
+	var scope = this;
+
+	this.radius = radius || 100;
+	this.tube = tube || 40;
+	this.segmentsR = segmentsR || 8;
+	this.segmentsT = segmentsT || 6;
+
+	var temp_uv = [];
+
+	THREE.Geometry.call(this);
+
+	for (var j = 0; j <= this.segmentsR; ++j) {
+		for (var i = 0; i <= this.segmentsT; ++i) {
+			var u = i / this.segmentsT * 2 * Math.PI;
+			var v = j / this.segmentsR * 2 * Math.PI;
+			var x = (this.radius + this.tube*Math.cos(v))*Math.cos(u);
+			var y = (this.radius + this.tube*Math.cos(v))*Math.sin(u);
+			var z = this.tube*Math.sin(v);
+			
+			vert(x, y, z);
+			
+			temp_uv.push([i/this.segmentsT, 1 - j/this.segmentsR]);
+		}
+	}
+
+	
+	for (var j = 1; j <= this.segmentsR; ++j) {
+		for (var i = 1; i <= this.segmentsT; ++i) {
+			var a = (this.segmentsT + 1)*j + i;
+			var b = (this.segmentsT + 1)*j + i - 1;
+			var c = (this.segmentsT + 1)*(j - 1) + i - 1;
+			var d = (this.segmentsT + 1)*(j - 1) + i;
+			
+			f4(a,b,c,d);
+
+			this.uvs.push( [new THREE.UV( temp_uv[a][0], temp_uv[a][1] ),
+							new THREE.UV( temp_uv[b][0], temp_uv[b][1] ),
+							new THREE.UV( temp_uv[c][0], temp_uv[c][1] ),
+							new THREE.UV( temp_uv[d][0], temp_uv[d][1] )
+							] );
+		}
+	}
+
+	delete temp_uv;
+	this.computeCentroids();
+	this.computeFaceNormals();
+	this.computeVertexNormals();
+	this.sortFacesByMaterial();
+
+	function vert( 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 ) );
+	}
+
+}
+
+Torus.prototype = new THREE.Geometry();
+Torus.prototype.constructor = Torus;

+ 100 - 0
src/extras/primitives/Torusknot.js

@@ -0,0 +1,100 @@
+/**
+ * @author oosmoxiecode
+ * based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
+ */
+
+var Torusknot = function (radius, tube, segmentsR, segmentsT, p, q, heightScale) {
+	var scope = this;
+	THREE.Geometry.call(this);
+
+	this.radius = radius || 200;
+	this.tube = tube || 40;
+	this.segmentsR = segmentsR || 64;
+	this.segmentsT = segmentsT || 8;
+	this.p = p || 2;
+    this.q = q || 3;
+	this.heightScale = heightScale || 1;
+	this.grid = new Array(this.segmentsR);
+
+	var tang = new THREE.Vector3();
+	var n = new THREE.Vector3();
+	var bitan = new THREE.Vector3();
+
+	for (var i=0; i<this.segmentsR; ++i) {
+		this.grid[i] = new Array(this.segmentsT);
+		for (var j=0; j<this.segmentsT; ++j) {
+			var u = i / this.segmentsR * 2 * this.p * Math.PI;
+			var v = j / this.segmentsT * 2 * Math.PI;
+			var p = getPos(u, v, this.q, this.p, this.radius, this.heightScale);
+			var p2 = getPos(u+.01, v, this.q, this.p, this.radius, this.heightScale);
+			var cx, cy;
+
+			tang.x = p2.x - p.x; tang.y = p2.y - p.y; tang.z = p2.z - p.z;
+			n.x = p2.x + p.x; n.y = p2.y + p.y; n.z = p2.z + p.z; 
+			bitan.cross(tang, n);
+			n.cross(bitan, tang);
+			bitan.normalize();
+			n.normalize();
+
+			cx = this.tube*Math.cos(v); cy = this.tube*Math.sin(v);
+			p.x += cx * n.x + cy * bitan.x;
+			p.y += cx * n.y + cy * bitan.y;
+			p.z += cx * n.z + cy * bitan.z;
+
+			this.grid[i][j] = vert(p.x, p.y, p.z);
+		}
+	}
+
+	for (var i=0; i<this.segmentsR; ++i) {
+		for (var j=0; j<this.segmentsT; ++j) {
+			var ip = (i+1) % this.segmentsR;
+			var jp = (j+1) % this.segmentsT;
+			var a = this.grid[i][j]; 
+			var b = this.grid[ip][j];
+			var c = this.grid[i][jp]; 
+			var d = this.grid[ip][jp];
+
+			var uva = new THREE.UV(i     / this.segmentsR, j     / this.segmentsT);
+			var uvb = new THREE.UV((i+1) / this.segmentsR, j     / this.segmentsT);
+			var uvc = new THREE.UV(i     / this.segmentsR, (j+1) / this.segmentsT);
+			var uvd = new THREE.UV((i+1) / this.segmentsR, (j+1) / this.segmentsT);
+
+			f3(a, b, c);
+			this.uvs.push( [uva,uvb,uvc] );
+			f3(d, c, b);
+			this.uvs.push( [uvd,uvc,uvb] );
+		}
+	}
+
+	this.computeCentroids();
+	this.computeFaceNormals();
+	this.computeVertexNormals();
+	this.sortFacesByMaterial();
+
+	function vert( x, y, z ) {
+		var i = scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
+		return i-1;
+	}
+
+	function f3( a, b, c ) {
+		scope.faces.push( new THREE.Face3( a, b, c ) );
+	}
+
+	function getPos(u, v, in_q, in_p, radius, heightScale ) {
+		var cu = Math.cos(u);
+		var cv = Math.cos(v);
+		var su = Math.sin(u);
+		var quOverP = in_q/in_p*u;
+		var cs = Math.cos(quOverP);
+
+		var tx = radius*(2+cs)*.5 * cu;
+		var ty = radius*(2+cs)*su*.5;
+		var tz = heightScale*radius*Math.sin(quOverP)*.5;
+		
+		return new THREE.Vector3(tx,ty,tz);
+	}
+
+}
+
+Torusknot.prototype = new THREE.Geometry();
+Torusknot.prototype.constructor = Torusknot;