]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
comments
[cloth_sim] / Scripts / cloth.js
index 1f1c1e01df19a910b5911c12ef95669b0576173e..712ed63a7250971788ab0424bba30bd9a0490003 100644 (file)
@@ -5,10 +5,6 @@ const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);
 const K = 1;\r
 const MAX_STRETCH = 1.5;\r
 \r
-// Explosion am Anfang\r
-// Intersect mit Velocity?\r
-// Wind/Ziehen\r
-\r
 const options = {\r
   wind: true,\r
 };\r
@@ -21,22 +17,23 @@ class Spring {
   }\r
 \r
   satisfy() {\r
+    /** calculate current spring length */\r
     const diff = this.p2.position.clone().sub(this.p1.position);\r
     const currentDist = diff.length();\r
     if (currentDist == 0) return;\r
     if (currentDist <= this.restDist) return;\r
     //const correction = diff.multiplyScalar(1 - (this.restDist / currentDist));\r
+\r
+    /** calculate necessary correction length and direction */\r
     const correction = diff.multiplyScalar((currentDist - this.restDist) / currentDist);\r
     correction.multiplyScalar(K);\r
-    if (currentDist >= this.restDist * MAX_STRETCH) {\r
-\r
-    }\r
-    //correction.clampLength(0, 1);\r
     const correctionHalf = correction.multiplyScalar(0.5);\r
 \r
     let p1movable = this.p1.movable && this.p1.movableTmp;\r
     let p2movable = this.p2.movable && this.p2.movableTmp;\r
 \r
+    /** apply correction if masses aren't fixed */\r
+    /** divide correction if both are movable */\r
     if (p1movable && p2movable) {\r
       this.p1.position.add(correctionHalf);\r
       this.p2.position.sub(correctionHalf);\r
@@ -67,8 +64,11 @@ class Mass {
     // verlet algorithm\r
     // next position = 2 * current Position - previous position + acceleration * (passed time)^2\r
     // acceleration (dv/dt) = F(net)\r
+    /** calculate velocity */\r
     const nextPosition = this.position.clone().sub(this.previous);\r
+    /** apply drag */\r
     nextPosition.multiplyScalar(DRAG);\r
+    /** add to current position and add acceleration */\r
     nextPosition.add(this.position);\r
     nextPosition.add(this.acceleration.multiplyScalar(dt*dt));\r
 \r
@@ -77,6 +77,7 @@ class Mass {
       this.position = nextPosition;\r
     }\r
 \r
+    /** reset for next frame */\r
     this.acceleration.set(0, 0, 0);\r
   }\r
 }\r
@@ -114,18 +115,16 @@ class Cloth {
       }\r
     }\r
 \r
-    //this.masses[this.getVertexIndex(0, 0)].movable = false;\r
+    /** attach cloth to flag pole */\r
     const n = 3;\r
     for (let i = 0; i < numPointsHeight; i++)\r
       this.masses[this.getVertexIndex(0, i)].movable = false;\r
-    //this.masses[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
-    //this.masses[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;\r
 \r
     const REST_DIST_X = width / (numPointsWidth-1);\r
     const REST_DIST_Y = height / (numPointsHeight-1);\r
 \r
     /**\r
-     * generate springs (springs)\r
+     * generate springs (constraints)\r
      */\r
     this.springs = [];\r
     for (let y = 0; y < numPointsHeight; y++) {\r
@@ -154,6 +153,7 @@ class Cloth {
     const indices = [];\r
     const uvs = [];\r
 \r
+    /** create one vertex and one uv coordinate per mass */\r
     for (let i in this.masses) {\r
       let particle = this.masses[i];\r
       vertices.push(\r
@@ -185,6 +185,7 @@ class Cloth {
       }\r
     }\r
 \r
+    /** set up geometry */\r
     geometry.setIndex(indices);\r
     geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));\r
     geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));\r
@@ -194,6 +195,7 @@ class Cloth {
     return geometry;\r
   }\r
   updateGeometry(geometry) {\r
+    /** update vertex positions in place */\r
     const positions = geometry.attributes.position.array;\r
     for (let i in this.masses) {\r
       let p = this.masses[i];\r
@@ -201,6 +203,7 @@ class Cloth {
       positions[i*3+1] = p.position.y;\r
       positions[i*3+2] = p.position.z;\r
     }\r
+    /** update internally and recalculate bounding volume */\r
     geometry.attributes.position.needsUpdate = true;\r
     geometry.computeBoundingSphere();\r
     geometry.computeVertexNormals();\r
@@ -208,6 +211,10 @@ class Cloth {
   simulate(dt) {\r
     let now = performance.now();\r
     for (let mass of this.masses) {\r
+      /** accumulate acceleration:\r
+       *  - wind\r
+       *  - gravity\r
+       */\r
       let vertex = mass.position;\r
       let fWind = new THREE.Vector3(\r
         this.windFactor.x * (Math.sin(vertex.x * vertex.y * now)+1),\r
@@ -221,14 +228,16 @@ class Cloth {
 \r
       mass.addForce(GRAVITY);\r
 \r
+      /** integrate motion */\r
       mass.verlet(dt);\r
     }\r
 \r
-    \r
+    /** run satisfy step */\r
     for (let constraint of this.springs) {\r
       constraint.satisfy();\r
     }\r
 \r
+    /** prevent self-intersections */\r
     this.intersect();\r
   }\r
 \r
@@ -241,15 +250,21 @@ class Cloth {
         p1.movableTmp = true;\r
         p2.movableTmp = true;\r
 \r
+        /** skip if i == j or if masses are adjacent */\r
         if (i == j || (Math.abs(this.getX(i) - this.getX(j)) == 1 && Math.abs(this.getY(i) - this.getY(j)) == 1))\r
           continue;\r
 \r
+        /** calculate distance of points  */\r
         let dist = p1.position.distanceTo(p2.position);\r
+        /** calculate minimal resting distance (largest distance that should not be fallen below) */\r
         let collisionDistance = Math.min(this.width / this.numPointsWidth, this.height / this.numPointsHeight);\r
         // collisionDistance /= 2;\r
+        /** calculate "sphere intersection" */\r
         if (dist < collisionDistance) {\r
           // p1.movableTmp = false;\r
           // p2.movableTmp = false;\r
+\r
+          /** vectors from p1 to p2 and the other way round */\r
           let diffP2P1 = p1.position.clone().sub(p2.position).normalize();\r
           diffP2P1.multiplyScalar((collisionDistance - dist) * 1.001 / 2);\r
           let diffP1P2 = diffP2P1.clone().multiplyScalar(-1);\r
@@ -260,6 +275,7 @@ class Cloth {
           // let factor1 = (Math.PI - Math.acos(v1.dot(diffP2P1))) / Math.PI * 2;\r
           // let factor2 = (Math.PI - Math.acos(v2.dot(diffP1P2))) / Math.PI * 2;\r
 \r
+          /** move masses apart */\r
           if (p1.movable)\r
             p1.position.add(diffP2P1);\r
             //p1.position.add(diffP2P1.multiplyScalar(factor1));\r
@@ -272,13 +288,17 @@ class Cloth {
   }\r
   blow(camPos, intersects) {\r
     let face = intersects[0].face;\r
+    /** vector from cam to intersection (wind) */\r
     let dir = intersects[0].point.clone().sub(camPos).multiplyScalar(100);\r
+    /** apply to all vertices of affected face */\r
     this.masses[face.a].addForce(dir);\r
     this.masses[face.b].addForce(dir);\r
     this.masses[face.c].addForce(dir);\r
   }\r
   drag(mousePosWorld, index) {\r
+    /** calculate vector from vertex to cursor */\r
     let dir = mousePosWorld.clone().sub(this.masses[index].position).multiplyScalar(200);\r
+    /** apply to grabbed vertex */\r
     this.masses[index].addForce(dir);\r
   }\r
 \r