]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
add flag textures
[cloth_sim] / Scripts / cloth.js
index 0c4f02c64a4c4935c3c41ad4fa4b0a69d135337d..5a7a0f214a7573b5f360215c680aafba4412b570 100644 (file)
@@ -4,7 +4,11 @@ const MASS = 0.1;
 const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);\r
 const K = 1;\r
 \r
-let tmpCorrection;\r
+// Flag Texture\r
+\r
+const options = {\r
+  wind: true,\r
+};\r
 \r
 class Constraint {\r
   constructor(p1, p2, restDist) {\r
@@ -18,16 +22,28 @@ class Constraint {
     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(1 - (this.restDist / currentDist));\r
+    const correction = diff.multiplyScalar((currentDist - this.restDist) / currentDist);\r
     correction.multiplyScalar(K);\r
-    tmpCorrection = correction;\r
+    correction.clampLength(0, 1);\r
     const correctionHalf = correction.multiplyScalar(0.5);\r
-    this.p1.position.add(correctionHalf);\r
-    this.p2.position.sub(correctionHalf);\r
+\r
+    let p1movable = this.p1.movable && this.p1.movableTmp;\r
+    let p2movable = this.p2.movable && this.p2.movableTmp;\r
+\r
+    if (p1movable && p2movable) {\r
+      this.p1.position.add(correctionHalf);\r
+      this.p2.position.sub(correctionHalf);\r
+    } else if (! p1movable && p2movable) {\r
+      this.p2.position.sub(correction);\r
+    } else if (p1movable && ! p2movable) {\r
+      this.p1.position.add(correction);\r
+    }\r
   }\r
 }\r
 \r
 class Particle {\r
+  movableTmp = true;\r
   movable = true;\r
 \r
   constructor(x, y, z, mass) {\r
@@ -50,8 +66,10 @@ class Particle {
     nextPosition.add(this.position);\r
     nextPosition.add(this.acceleration.multiplyScalar(dt*dt));\r
 \r
-    this.previous = this.position;\r
-    this.position = nextPosition;\r
+    if (this.movable && this.movableTmp) {\r
+      this.previous = this.position;\r
+      this.position = nextPosition;\r
+    }\r
 \r
     this.acceleration.set(0, 0, 0);\r
   }\r
@@ -63,7 +81,7 @@ class Cloth {
     this.height = height;\r
     this.numPointsWidth = numPointsWidth;\r
     this.numPointsHeight = numPointsHeight;\r
-    this.windFactor = new THREE.Vector3(0.5, 0.2, 0.2);\r
+    this.windFactor = new THREE.Vector3(3, 2, 2);\r
 \r
     /**\r
      * distance between two vertices horizontally/vertically\r
@@ -90,9 +108,12 @@ class Cloth {
       }\r
     }\r
 \r
-    this.particles[this.getVertexIndex(0, 0)].movable = false;\r
-    this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
-    this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;\r
+    //this.particles[this.getVertexIndex(0, 0)].movable = false;\r
+    const n = 3;\r
+    for (let i = 0; i < numPointsHeight; i++)\r
+      this.particles[this.getVertexIndex(0, i)].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
@@ -124,25 +145,27 @@ class Cloth {
     const geometry = new THREE.BufferGeometry();\r
 \r
     const vertices = [];\r
-    const normals = [];\r
     const indices = [];\r
+    const uvs = [];\r
 \r
-    for (let particle of this.particles) {\r
+    for (let i in this.particles) {\r
+      let particle = this.particles[i];\r
       vertices.push(\r
         particle.position.x,\r
         particle.position.y,\r
         particle.position.z);\r
+      uvs.push(\r
+        this.getX(i) / (this.numPointsWidth-1),\r
+        1 - (this.getY(i) / (this.numPointsHeight-1))\r
+      );\r
     }\r
 \r
-    const numPointsWidth = this.numPointsWidth;\r
-    const numPointsHeight = this.numPointsHeight;\r
-\r
     /**\r
      * generate faces based on 4 vertices\r
      * and 6 springs each\r
      */\r
-    for (let y = 0; y < numPointsHeight - 1; y++) {\r
-      for (let x = 0; x < numPointsWidth - 1; x++) {\r
+    for (let y = 0; y < this.numPointsHeight - 1; y++) {\r
+      for (let x = 0; x < this.numPointsWidth - 1; x++) {\r
         indices.push(\r
           this.getVertexIndex(x, y),\r
           this.getVertexIndex(x+1, y),\r
@@ -158,7 +181,7 @@ class Cloth {
 \r
     geometry.setIndex(indices);\r
     geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));\r
-    //geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));\r
+    geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));\r
     geometry.computeBoundingSphere();\r
     geometry.computeVertexNormals();\r
 \r
@@ -168,13 +191,9 @@ class Cloth {
     const positions = geometry.attributes.position.array;\r
     for (let i in this.particles) {\r
       let p = this.particles[i];\r
-      if (p.movable) {\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
-      } else {\r
-        p.position = p.previous;\r
-      }\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
@@ -190,7 +209,8 @@ class Cloth {
         this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
       );\r
       // normalize then multiply?\r
-      particle.addForce(fWind);\r
+      if (options.wind)\r
+        particle.addForce(fWind);\r
       // calculate wind with normal?\r
 \r
       particle.addForce(GRAVITY);\r
@@ -202,8 +222,38 @@ class Cloth {
     for (let constraint of this.constraints) {\r
       constraint.satisfy();\r
     }\r
-    //console.log(tmpCorrection);\r
+\r
+    this.intersect();\r
   }\r
+\r
+  intersect() {\r
+    for (let i in this.particles) {\r
+      for (let j in this.particles) {  \r
+        let p1 = this.particles[i];\r
+        let p2 = this.particles[j];\r
+\r
+        p1.movableTmp = true;\r
+        p2.movableTmp = true;\r
+\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
+        let dist = p1.position.distanceTo(p2.position);\r
+        const collisionDistance = Math.min(this.width / this.numPointsWidth, this.height / this.numPointsHeight);\r
+        if (dist < collisionDistance) {\r
+          // p1.movableTmp = false;\r
+          // p2.movableTmp = false;\r
+          let diff = p1.position.clone().sub(p2.position).normalize();\r
+          diff.multiplyScalar((collisionDistance - dist) * 1.001 / 2);\r
+          if (p1.movable)\r
+            p1.position.add(diff);\r
+          if (p2.movable)\r
+            p2.position.sub(diff);\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
   /**\r
    * helper function to calculate index of vertex\r
    * in "vertices" array based on its x and y positions\r
@@ -214,4 +264,6 @@ class Cloth {
   getVertexIndex(x, y) {\r
     return y * this.numPointsWidth + x;\r
   }\r
+  getX(i) { return i % this.numPointsWidth; }\r
+  getY(i) { return Math.floor(i / this.numPointsWidth); }\r
 }
\ No newline at end of file