]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
tweaking parameters
[cloth_sim] / Scripts / cloth.js
index 05ff6f1f30eb54da8985b99c786d3596662f214e..aaa9d249a25a9702d3abd95d46a1d317f5a95f76 100644 (file)
@@ -1,10 +1,44 @@
-// cloth rendering\r
-// simulate\r
-// setup scene\r
+const DAMPING = 0.03;\r
+const DRAG = 1 - DAMPING;\r
+const MASS = 0.35;\r
+const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);\r
+const K = 1;\r
 \r
-const MASS = 0.1;\r
+const options = {\r
+  wind: true,\r
+};\r
+\r
+class Constraint {\r
+  constructor(p1, p2, restDist) {\r
+    this.p1 = p1;\r
+    this.p2 = p2;\r
+    this.restDist = restDist;\r
+  }\r
+\r
+  satisfy() {\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
+    const correction = diff.multiplyScalar((currentDist - this.restDist) / currentDist);\r
+    correction.multiplyScalar(K);\r
+    correction.clampLength(0, 1);\r
+    const correctionHalf = correction.multiplyScalar(0.5);\r
+    if (this.p1.movable && this.p2.movable) {\r
+      this.p1.position.add(correctionHalf);\r
+      this.p2.position.sub(correctionHalf);\r
+    } else if (! this.p1.movable && this.p2.movable) {\r
+      this.p2.position.sub(correction);\r
+    } else if (this.p1.movable && ! this.p2.movable) {\r
+      this.p1.position.add(correction);\r
+    }\r
+  }\r
+}\r
 \r
 class Particle {\r
+  movable = true;\r
+\r
   constructor(x, y, z, mass) {\r
     this.position = new THREE.Vector3(x, y, z);\r
     this.previous = new THREE.Vector3(x, y, z);\r
@@ -12,10 +46,25 @@ class Particle {
     this.mass = mass;\r
   }\r
   addForce(force) {\r
-\r
+    this.acceleration.add(\r
+      force.clone().multiplyScalar(1/this.mass)\r
+    );\r
   }\r
   verlet(dt) {\r
+    // verlet algorithm\r
+    // next position = 2 * current Position - previous position + acceleration * (passed time)^2\r
+    // acceleration (dv/dt) = F(net)\r
+    const nextPosition = this.position.clone().sub(this.previous);\r
+    nextPosition.multiplyScalar(DRAG);\r
+    nextPosition.add(this.position);\r
+    nextPosition.add(this.acceleration.multiplyScalar(dt*dt));\r
 \r
+    if (this.movable) {\r
+      this.previous = this.position;\r
+      this.position = nextPosition;\r
+    }\r
+\r
+    this.acceleration.set(0, 0, 0);\r
   }\r
 }\r
 \r
@@ -25,6 +74,7 @@ class Cloth {
     this.height = height;\r
     this.numPointsWidth = numPointsWidth;\r
     this.numPointsHeight = numPointsHeight;\r
+    this.windFactor = new THREE.Vector3(5, 2, 2);\r
 \r
     /**\r
      * distance between two vertices horizontally/vertically\r
@@ -50,6 +100,39 @@ class Cloth {
         );\r
       }\r
     }\r
+\r
+    //this.particles[this.getVertexIndex(0, 0)].movable = false;\r
+    const n = 3;\r
+    for (let i = 0; i <= n; i++)\r
+      this.particles[this.getVertexIndex(0, Math.floor((numPointsHeight-1)*(i/n)))].movable = false;\r
+    //this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
+    //this.particles[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 constraints (springs)\r
+     */\r
+    this.constraints = [];\r
+    for (let y = 0; y < numPointsHeight; y++) {\r
+      for (let x = 0; x < numPointsWidth; x++) {\r
+        if (x < numPointsWidth-1) {\r
+          this.constraints.push(new Constraint(\r
+            this.particles[this.getVertexIndex(x, y)],\r
+            this.particles[this.getVertexIndex(x+1, y)],\r
+            REST_DIST_X\r
+          ));\r
+        }\r
+        if (y < numPointsHeight-1) {\r
+          this.constraints.push(new Constraint(\r
+            this.particles[this.getVertexIndex(x, y)],\r
+            this.particles[this.getVertexIndex(x, y+1)],\r
+            REST_DIST_Y\r
+          ));\r
+        }\r
+      }\r
+    }\r
   }\r
   generateGeometry() {\r
     const geometry = new THREE.BufferGeometry();\r
@@ -68,17 +151,6 @@ class Cloth {
     const numPointsWidth = this.numPointsWidth;\r
     const numPointsHeight = this.numPointsHeight;\r
 \r
-    /**\r
-     * helper function to calculate index of vertex\r
-     * in "vertices" array based on its x and y positions\r
-     * in the mesh\r
-     * @param {number} x - x index of vertex\r
-     * @param {number} y - y index of vertex\r
-     */\r
-    function getVertexIndex(x, y) {\r
-      return y * numPointsWidth + x;\r
-    }\r
-\r
     /**\r
      * generate faces based on 4 vertices\r
      * and 6 springs each\r
@@ -86,14 +158,14 @@ class Cloth {
     for (let y = 0; y < numPointsHeight - 1; y++) {\r
       for (let x = 0; x < numPointsWidth - 1; x++) {\r
         indices.push(\r
-          getVertexIndex(x, y),\r
-          getVertexIndex(x+1, y),\r
-          getVertexIndex(x+1, y+1)\r
+          this.getVertexIndex(x, y),\r
+          this.getVertexIndex(x+1, y),\r
+          this.getVertexIndex(x+1, y+1)\r
         );\r
         indices.push(\r
-          getVertexIndex(x, y),\r
-          getVertexIndex(x+1, y+1),\r
-          getVertexIndex(x, y+1)\r
+          this.getVertexIndex(x, y),\r
+          this.getVertexIndex(x+1, y+1),\r
+          this.getVertexIndex(x, y+1)\r
         );\r
       }\r
     }\r
@@ -107,6 +179,50 @@ class Cloth {
     return geometry;\r
   }\r
   updateGeometry(geometry) {\r
+    const positions = geometry.attributes.position.array;\r
+    for (let i in this.particles) {\r
+      let p = this.particles[i];\r
+      positions[i*3+0] = p.position.x;\r
+      positions[i*3+1] = p.position.y;\r
+      positions[i*3+2] = p.position.z;\r
+    }\r
+    geometry.attributes.position.needsUpdate = true;\r
+    geometry.computeBoundingSphere();\r
+    geometry.computeVertexNormals();\r
+  }\r
+  simulate(dt) {\r
+    let now = performance.now();\r
+    for (let particle of this.particles) {\r
+      let vertex = particle.position;\r
+      let fWind = new THREE.Vector3(\r
+        this.windFactor.x * (Math.sin(vertex.x * vertex.y * now)+1),\r
+        this.windFactor.y * Math.cos(vertex.z * now),\r
+        this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
+      );\r
+      // normalize then multiply?\r
+      if (options.wind)\r
+        particle.addForce(fWind);\r
+      // calculate wind with normal?\r
+\r
+      particle.addForce(GRAVITY);\r
 \r
+      particle.verlet(dt);\r
+    }\r
+\r
+    \r
+    for (let constraint of this.constraints) {\r
+      constraint.satisfy();\r
+    }\r
+    //console.log(tmpCorrection);\r
+  }\r
+  /**\r
+   * helper function to calculate index of vertex\r
+   * in "vertices" array based on its x and y positions\r
+   * in the mesh\r
+   * @param {number} x - x index of vertex\r
+   * @param {number} y - y index of vertex\r
+   */\r
+  getVertexIndex(x, y) {\r
+    return y * this.numPointsWidth + x;\r
   }\r
 }
\ No newline at end of file