]> gitweb.ps.run Git - cloth_sim/commitdiff
WIP Cloth Simulation
authorRamtin Naraghi <ramtin.naraghi@hotmail.com>
Mon, 18 Jan 2021 12:10:26 +0000 (13:10 +0100)
committerRamtin Naraghi <ramtin.naraghi@hotmail.com>
Mon, 18 Jan 2021 12:10:26 +0000 (13:10 +0100)
Scripts/cloth.js
Scripts/main.js

index e84facc28dc840447f4848723d33ef722bb9f85f..9389ef60c13cb8e827d6054d92012727ac4129a1 100644 (file)
@@ -46,12 +46,12 @@ export class Spring {
   index1;\r
   index2;\r
 \r
\r
+\r
   /**\r
    * set vertex indices\r
    * and calculate inital length based on the\r
    * vertex positions\r
-   * @param {Array of Vector3} vertices \r
+   * @param {Array<Vector3>} vertices \r
    * @param {number} index1 \r
    * @param {number} index2 \r
    */\r
@@ -63,6 +63,19 @@ export class Spring {
     this.restLength = length;\r
     this.currentLength = length;\r
   }\r
+\r
+  getDirection(vertices) {\r
+    let direction = new THREE.Vector3(\r
+      vertices[this.index1].x,\r
+      vertices[this.index1].y,\r
+      vertices[this.index1].z\r
+    );\r
+\r
+    direction.sub(vertices[this.index2]);\r
+    direction.divideScalar(vectorLength(vertices[this.index1], vertices[this.index2]));\r
+\r
+    return direction;\r
+  }\r
 }\r
 \r
 /**\r
@@ -81,7 +94,8 @@ export class Cloth {
 \r
   vertexWeights = [];\r
 \r
-  \r
+\r
+\r
   /**\r
    * creates a rectangular piece of cloth\r
    * takes the size of the cloth\r
@@ -126,7 +140,7 @@ export class Cloth {
     function getVertexIndex(x, y) {\r
       return y * numPointsWidth + x;\r
     }\r
-    \r
+\r
     /**\r
      * generate faces based on 4 vertices\r
      * and 6 springs each\r
@@ -146,7 +160,7 @@ export class Cloth {
         newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x, y + 1)));\r
         newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x + 1, y + 1)));\r
         newFace.springs.push(new Spring(vertices, getVertexIndex(x, y + 1), getVertexIndex(x + 1, y + 1)));\r
-        \r
+\r
         faces.push(newFace);\r
       }\r
     }\r
@@ -163,15 +177,17 @@ export class Cloth {
    * (list of vertices and list of indices representing triangles)\r
    * and calculate the weight of each face and split it between\r
    * surrounding vertices\r
-   * @param {Array of Vector3} vertices \r
-   * @param {Array of Face} faces \r
+   * @param {Array<Vector3>} vertices \r
+   * @param {Array<Face>} faces \r
    */\r
   createExplicit(vertices, faces) {\r
+\r
     /**\r
      * Copy vertices and initialize vertex weights to 0\r
      */\r
     for (let i in vertices) {\r
       this.geometry.vertices.push(vertices[i]);\r
+      this.previousPositions.push(vertices[i]);\r
       this.vertexWeights.push(0);\r
     }\r
     /**\r
@@ -193,7 +209,7 @@ export class Cloth {
       this.geometry.faces.push(new THREE.Face3(\r
         face.c, face.b, face.d\r
       ));\r
-      \r
+\r
       /**\r
        * calculate area of face as combined area of\r
        * its two composing triangles\r
@@ -240,7 +256,7 @@ export class Cloth {
       let geometry = new THREE.Geometry();\r
       geometry.vertices.push(from);\r
       geometry.vertices.push(to);\r
-      let material = new THREE.LineBasicMaterial( { color: color, linewidth: 10 } );\r
+      let material = new THREE.LineBasicMaterial({ color: color, linewidth: 10 });\r
       let line = new THREE.Line(geometry, material);\r
       line.renderOrder = 1;\r
       scene.add(line);\r
@@ -252,11 +268,11 @@ export class Cloth {
      * @param {number} color \r
      */\r
     function addPoint(point, color) {\r
-      const geometry = new THREE.SphereGeometry( 0.05, 32, 32 );\r
-      const material = new THREE.MeshBasicMaterial( { color: color } );\r
-      const sphere = new THREE.Mesh( geometry, material );\r
+      const geometry = new THREE.SphereGeometry(0.05, 32, 32);\r
+      const material = new THREE.MeshBasicMaterial({ color: color });\r
+      const sphere = new THREE.Mesh(geometry, material);\r
       sphere.position.set(point.x, point.y, point.z);\r
-      scene.add( sphere );\r
+      scene.add(sphere);\r
     }\r
 \r
     let lineColor = 0x000000;\r
@@ -282,4 +298,138 @@ export class Cloth {
       addPoint(this.geometry.vertices[face.d], pointColor);\r
     }\r
   }\r
-}
\ No newline at end of file
+\r
+  previousPositions = [];\r
+  time = 0;\r
+  /**\r
+   * \r
+   * @param {number} dt \r
+   */\r
+  simulate(dt) {\r
+\r
+\r
+\r
+    for (let i in this.geometry.vertices) {\r
+      let currentPosition;\r
+      let acceleration = this.getAcceleration(i, dt);\r
\r
+      currentPosition = this.verlet(this.geometry.vertices[i], this.previousPositions[i], acceleration, dt/2000);\r
+     \r
+      this.previousPositions[i] = currentPosition;\r
+      this.geometry.vertices[i] = currentPosition;\r
+      \r
+    }\r
+    console.log(this.geometry.vertices[0]);\r
+    this.time += dt;\r
+\r
+    /**\r
+     * let THREE JS compute bounding sphere around generated mesh\r
+     * needed for View Frustum Culling internally\r
+     */\r
+\r
+    this.geometry.verticesNeedUpdate = true;\r
+    this.geometry.elementsNeedUpdate = true;\r
+    this.geometry.computeBoundingSphere();\r
+\r
+  }\r
+\r
+\r
+\r
+/**\r
+ * Equation of motion for each vertex which represents the acceleration \r
+ * @param {number} vertexIndex The index of the current vertex whose acceleration should be calculated\r
+ *  @param {number} dt The time passed since last frame\r
+ */\r
+getAcceleration(vertexIndex, dt) {\r
+\r
+  let vertex = this.geometry.vertices[vertexIndex];\r
+\r
+  // Mass of vertex\r
+  let M = this.vertexWeights[vertexIndex];\r
+  // constant gravity\r
+  let g = new THREE.Vector3(0, -1.8, 0);\r
+  // stiffness\r
+  let k = 5;\r
+\r
+  // Wind vector\r
+  let fWind = new THREE.Vector3(\r
+    Math.sin(vertex.x * vertex.y * this.time),\r
+    Math.cos(vertex.z* this.time),\r
+    Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
+  );\r
+\r
+  /**\r
+   * constant determined by the properties of the surrounding fluids (air)\r
+   * achievement of cloth effects through try out\r
+   * */\r
+  let a = 1;\r
+\r
+  let velocity = new THREE.Vector3(\r
+    (vertex.x - this.previousPositions[vertexIndex].x) / dt,\r
+    (vertex.y - this.previousPositions[vertexIndex].y) / dt,\r
+    (vertex.z - this.previousPositions[vertexIndex].z) / dt\r
+  );\r
+\r
+\r
+  let fAirResistance = velocity.multiplyScalar(-a);\r
+\r
+  let springSum = new THREE.Vector3(0, 0, 0);\r
+\r
+  // Get the bounding springs and add them to the needed springs\r
+  for (let i in this.faces) {\r
+    if (this.faces[i].a == vertexIndex || this.faces[i].b == vertexIndex || this.faces[i].c == vertexIndex || this.faces[i].d == vertexIndex) {\r
+      for (let j in this.faces[i].springs) {\r
+        if (this.faces[i].springs[j].index1 == vertexIndex || this.faces[i].springs[j].index2 == vertexIndex) {\r
+\r
+          let spring = this.faces[i].springs[j];\r
+          let springDirection = spring.getDirection(this.geometry.vertices);\r
+\r
+\r
+          if (this.faces[i].springs[j].index1 == vertexIndex)\r
+            springDirection.multiplyScalar(-1);\r
+\r
+          springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength)));\r
+\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  \r
+  let result = new THREE.Vector3(1, 1, 1);\r
+\r
+  \r
+  result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).sub(springSum);\r
+  \r
+\r
+  return result;\r
+\r
+\r
+}\r
+\r
+/**\r
+ * The Verlet algorithm as an integrator \r
+ * to get the next position of a vertex  \r
+ * @param {Vector3} currentPosition \r
+ * @param {Vector3} previousPosition \r
+ * @param {Vector3} acceleration \r
+ * @param {number} passedTime The delta time since last frame\r
+ */\r
+verlet(currentPosition, previousPosition, acceleration, passedTime) {\r
+  // verlet algorithm\r
+  // next position = 2 * current Position - previous position + acceleration * (passed time)^2\r
+  // acceleration (dv/dt) = F(net)\r
+  // Dependency for one vertex: gravity, fluids/air, springs\r
+\r
+  let nextPosition = new THREE.Vector3(\r
+    2 * currentPosition.x - previousPosition.x + acceleration.x * (passedTime * passedTime),\r
+    2 * currentPosition.y - previousPosition.y + acceleration.y * (passedTime * passedTime),\r
+    2 * currentPosition.z - previousPosition.z + acceleration.z * (passedTime * passedTime),\r
+  );\r
+\r
+  return nextPosition;\r
+}\r
+\r
+\r
+}\r
+\r
index a0387ac5943e08126bf531f98936ba15248d8e6a..5ed1866027c92343b1cde743c6b527dedbeb2f27 100644 (file)
@@ -57,7 +57,8 @@ document.body.onload = init;
 \r
 function init() {\r
   let mousePos = new Point();\r
-\r
+  let previousClothSimulation;\r
+  \r
   /**\r
    * Space left empty under canvas\r
    * for UI elements\r
@@ -74,6 +75,11 @@ function init() {
 \r
   const material = new THREE.MeshBasicMaterial({ color: 0x0000ff });\r
   const mesh = new THREE.Mesh(cloth.geometry, material);\r
+  //const mesh = new THREE.WireframeGeometry(cloth.geometry);\r
+  //const line = new THREE.LineSegments(mesh);\r
+  //line.material.depthTest = false;\r
+  //line.material.opacity = 0.25;\r
+  //line.material.transparent = true;\r
   scene.add(mesh);\r
 \r
   /**\r
@@ -81,7 +87,12 @@ function init() {
    * @param {number} dt - time passed since last frame\r
    */\r
   function animate(dt) {\r
-    requestAnimationFrame(animate);\r
+    \r
+    cloth.simulate(dt);\r
+\r
+    setTimeout(() => {\r
+      animate(2000);\r
+    }, 2000);\r
     renderer.render(scene, camera);\r
   }\r
 \r