Просмотр исходного кода

adding THREE.ParametricGeometry

zz85 13 лет назад
Родитель
Сommit
07de9d958b
2 измененных файлов с 64 добавлено и 0 удалено
  1. 63 0
      src/extras/geometries/ParametricGeometry.js
  2. 1 0
      utils/build.py

+ 63 - 0
src/extras/geometries/ParametricGeometry.js

@@ -0,0 +1,63 @@
+/*
+ * @author zz85 / https://github.com/zz85
+ * Parametric Surfaces Geometry
+ * based on the brilliant article by @prideout http://prideout.net/blog/?p=44
+ */
+
+THREE.ParametricGeometry = function(slices, stacks, func) {
+
+    THREE.Geometry.call(this);
+
+    var verts = this.vertices,
+        faces = this.faces,
+        uvs = this.faceVertexUvs[0];
+    var pi = Math.PI;
+
+    var i, il, theta, j, phi, p;
+
+    for (i = 0; i <= slices; i++) {
+        theta = i * pi / slices;
+
+        for (j = 0; j < stacks; j++) {
+            phi = j * 2 * pi / stacks;
+
+            p = func(theta, phi);
+            verts.push(new THREE.Vertex(p));
+
+        }
+    }
+
+    var v = 0,
+        next;
+
+    // Some UV / Face orientation work needs to be done here...
+    for (i = 0; i < slices; i++) {
+        for (j = 0; j < stacks; j++) {
+            next = (j + 1) % stacks;
+
+            faces.push(new THREE.Face3(v + j, v + next, v + j + stacks));
+            faces.push(new THREE.Face3(v + next, v + next + stacks, v + j + stacks));
+
+            uvs.push([
+                new THREE.UV(i / slices, j / stacks),
+                new THREE.UV(i / slices, (j + 1) / stacks),
+                new THREE.UV((i + 1) / slices, j / stacks)
+                ]);
+            uvs.push([
+                new THREE.UV(i / slices, (j + 1) / stacks),
+                new THREE.UV((i + 1) / slices, (j + 1) / stacks),
+                new THREE.UV((i + 1) / slices, j / stacks)
+                ]);
+        }
+        v += stacks;
+    }
+
+
+    this.computeCentroids();
+    this.computeFaceNormals();
+    this.computeVertexNormals();
+
+};
+
+THREE.ParametricGeometry.prototype = new THREE.Geometry();
+THREE.ParametricGeometry.prototype.constructor = THREE.ParametricGeometry;

+ 1 - 0
utils/build.py

@@ -121,6 +121,7 @@ EXTRAS_FILES = [
 'extras/geometries/IcosahedronGeometry.js',
 'extras/geometries/OctahedronGeometry.js',
 'extras/geometries/TetrahedronGeometry.js',
+'extras/geometries/ParametricGeometry.js',
 'extras/helpers/AxisHelper.js',
 'extras/helpers/ArrowHelper.js',
 'extras/helpers/CameraHelper.js',