]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
fix verlet integration, add wind
[cloth_sim] / Scripts / cloth.js
index 9389ef60c13cb8e827d6054d92012727ac4129a1..266af16a3f1236417b7efd8c0c8805508d508bca 100644 (file)
@@ -6,9 +6,9 @@
  */\r
 function vectorLength(a, b) {\r
   let v1 = new THREE.Vector3();\r
-  v1.set(a.x, a.y, a.z);\r
+  v1.copy(a);\r
   let v2 = new THREE.Vector3();\r
-  v2.set(b.x, b.y, b.z);\r
+  v2.copy(b);\r
 \r
   return v1.sub(v2).length();\r
 }\r
@@ -65,17 +65,19 @@ export class Spring {
   }\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
+    let direction = new THREE.Vector3();\r
+    direction.copy(vertices[this.index1]);\r
 \r
     direction.sub(vertices[this.index2]);\r
     direction.divideScalar(vectorLength(vertices[this.index1], vertices[this.index2]));\r
 \r
     return direction;\r
   }\r
+\r
+  update(vertices) {\r
+    let length = vectorLength(vertices[this.index1], vertices[this.index2]);\r
+    this.currentLength = length;\r
+  }\r
 }\r
 \r
 /**\r
@@ -94,7 +96,7 @@ export class Cloth {
 \r
   vertexWeights = [];\r
 \r
-\r
+  vertexRigidness = [];\r
 \r
   /**\r
    * creates a rectangular piece of cloth\r
@@ -154,12 +156,12 @@ export class Cloth {
           getVertexIndex(x + 1, y + 1),\r
         );\r
 \r
-        newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y)));\r
-        newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x, y + 1)));\r
-        newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y + 1)));\r
-        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
+        newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y)));         // oben\r
+        newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x, y + 1)));         // links\r
+        newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y + 1)));     // oben links  -> unten rechts diagonal\r
+        newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x, y + 1)));     // oben rechts -> unten links diagonal\r
+        newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x + 1, y + 1))); // rechts\r
+        newFace.springs.push(new Spring(vertices, getVertexIndex(x, y + 1), getVertexIndex(x + 1, y + 1))); // unten\r
 \r
         faces.push(newFace);\r
       }\r
@@ -170,6 +172,12 @@ export class Cloth {
      * with generated vertices and faces\r
      */\r
     this.createExplicit(vertices, faces);\r
+\r
+    /**\r
+     * hand cloth from left and right upper corners\r
+     */\r
+    this.vertexRigidness[0] = true;\r
+    this.vertexRigidness[numPointsWidth-1] = true;\r
   }\r
 \r
   /**\r
@@ -186,9 +194,12 @@ export class Cloth {
      * 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.geometry.vertices.push(vertices[i].clone());\r
+      this.previousPositions.push(vertices[i].clone());\r
+      // this.geometry.vertices.push(vertices[i]);\r
+      // this.previousPositions.push(vertices[i]);\r
       this.vertexWeights.push(0);\r
+      this.vertexRigidness.push(false);\r
     }\r
     /**\r
      * copy faces,\r
@@ -236,6 +247,7 @@ export class Cloth {
      * needed for View Frustum Culling internally\r
      */\r
     this.geometry.computeBoundingSphere();\r
+    this.geometry.computeFaceNormals();\r
   }\r
 \r
   /**\r
@@ -303,25 +315,34 @@ export class Cloth {
   time = 0;\r
   /**\r
    * \r
-   * @param {number} dt \r
+   * @param {number} dt time in seconds since last frame\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
+      //acceleration.clampLength(0, 10);\r
+\r
+      if (Math.abs(acceleration.length()) <= 10e-4) {\r
+        acceleration.set(0, 0, 0);\r
+      }\r
  \r
-      currentPosition = this.verlet(this.geometry.vertices[i], this.previousPositions[i], acceleration, dt/2000);\r
+      let currentPosition = this.verlet(this.geometry.vertices[i].clone(), this.previousPositions[i].clone(), acceleration, dt);\r
+      //let currentPosition = this.euler(this.geometry.vertices[i], acceleration, dt);\r
      \r
-      this.previousPositions[i] = currentPosition;\r
-      this.geometry.vertices[i] = currentPosition;\r
-      \r
+      this.previousPositions[i].copy(this.geometry.vertices[i]);\r
+      this.geometry.vertices[i].copy(currentPosition);\r
     }\r
-    console.log(this.geometry.vertices[0]);\r
+    //console.log(this.getAcceleration(1, dt));\r
+    \r
     this.time += dt;\r
 \r
+    for (let face of this.faces) {\r
+      for (let spring of face.springs) {\r
+        spring.update(this.geometry.vertices);\r
+      }\r
+    }\r
+\r
     /**\r
      * let THREE JS compute bounding sphere around generated mesh\r
      * needed for View Frustum Culling internally\r
@@ -330,6 +351,7 @@ export class Cloth {
     this.geometry.verticesNeedUpdate = true;\r
     this.geometry.elementsNeedUpdate = true;\r
     this.geometry.computeBoundingSphere();\r
+    this.geometry.computeFaceNormals();\r
 \r
   }\r
 \r
@@ -341,20 +363,22 @@ export class Cloth {
  *  @param {number} dt The time passed since last frame\r
  */\r
 getAcceleration(vertexIndex, dt) {\r
+  if (this.vertexRigidness[vertexIndex])\r
+    return new THREE.Vector3(0, 0, 0);\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
+  let g = new THREE.Vector3(0, -9.8, 0);\r
   // stiffness\r
-  let k = 5;\r
+  let k = 1000;\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.cos(vertex.z * this.time),\r
     Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
   );\r
 \r
@@ -362,49 +386,94 @@ getAcceleration(vertexIndex, dt) {
    * 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 a = 0.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
+    (this.previousPositions[vertexIndex].x - vertex.x) / dt,\r
+    (this.previousPositions[vertexIndex].y - vertex.y) / dt,\r
+    (this.previousPositions[vertexIndex].z - vertex.z) / dt\r
   );\r
 \r
+  //console.log(velocity, vertex, this.previousPositions[vertexIndex]);\r
 \r
-  let fAirResistance = velocity.multiplyScalar(-a);\r
-\r
+  let fAirResistance = velocity.multiply(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
+  // TODO: optimize\r
 \r
-          let spring = this.faces[i].springs[j];\r
-          let springDirection = spring.getDirection(this.geometry.vertices);\r
+  const numPointsX = 10;\r
+  const numPointsY = 10;\r
+  const numFacesX = numPointsX - 1;\r
+  const numFacesY = numPointsY - 1;\r
 \r
+  function getFaceIndex(x, y) {\r
+    return y * numFacesX + x;\r
+  }\r
+\r
+  let indexX = vertexIndex % numPointsX;\r
+  let indexY = Math.floor(vertexIndex / numPointsX);\r
+\r
+  let springs = [];\r
+\r
+  // 0  oben\r
+  // 1  links\r
+  // 2  oben links  -> unten rechts diagonal\r
+  // 3  oben rechts -> unten links diagonal\r
+  // 4  rechts\r
+  // 5  unten\r
+\r
+  let ul = indexX > 0 && indexY < numPointsY - 1;\r
+  let ur = indexX < numPointsX - 1 && indexY < numPointsY - 1;\r
+  let ol = indexX > 0 && indexY > 0;\r
+  let or = indexX < numPointsX - 1 && indexY > 0;\r
+\r
+  if (ul) {\r
+    let faceUL = this.faces[getFaceIndex(indexX - 1, indexY)];\r
+    springs.push(faceUL.springs[3]);\r
+    if (!ol)\r
+      springs.push(faceUL.springs[0]);\r
+    springs.push(faceUL.springs[4]);\r
+  }\r
+  if (ur) {\r
+    let faceUR = this.faces[getFaceIndex(indexX, indexY)];\r
+    springs.push(faceUR.springs[2]);\r
+    if (!or)\r
+      springs.push(faceUR.springs[0]);\r
+    if (!ul)\r
+      springs.push(faceUR.springs[1]);\r
+  }\r
+  if (ol) {\r
+    let faceOL = this.faces[getFaceIndex(indexX - 1, indexY - 1)];\r
+    springs.push(faceOL.springs[2]);\r
+    springs.push(faceOL.springs[4]);\r
+    springs.push(faceOL.springs[5]);\r
+  }\r
+  if (or) {\r
+    let faceOR = this.faces[getFaceIndex(indexX , indexY - 1)];\r
+    springs.push(faceOR.springs[3]);\r
+    if (!ol)\r
+      springs.push(faceOR.springs[1]);\r
+    springs.push(faceOR.springs[5]);\r
+  }\r
 \r
-          if (this.faces[i].springs[j].index1 == vertexIndex)\r
-            springDirection.multiplyScalar(-1);\r
+  for (let spring of springs) {\r
+    let springDirection = spring.getDirection(this.geometry.vertices);\r
 \r
-          springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength)));\r
+    if (spring.index1 == vertexIndex)\r
+      springDirection.multiplyScalar(-1);\r
 \r
-        }\r
-      }\r
-    }\r
+    springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength)));\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
+  let temp = result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).clone();\r
+  result.sub(springSum);\r
+  document.getElementById("Output").innerText = "SpringSum: " + Math.floor(springSum.y) + "\nTemp: " + Math.floor(temp.y);\r
 \r
   return result;\r
-\r
-\r
 }\r
 \r
 /**\r
@@ -420,16 +489,38 @@ verlet(currentPosition, previousPosition, acceleration, passedTime) {
   // 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
+  const DRAG = 0.97;\r
+  let nextPosition = new THREE.Vector3(\r
+    (currentPosition.x - previousPosition.x) * DRAG + currentPosition.x + acceleration.x * (passedTime * passedTime),\r
+    (currentPosition.y - previousPosition.y) * DRAG + currentPosition.y + acceleration.y * (passedTime * passedTime),\r
+    (currentPosition.z - previousPosition.z) * DRAG + currentPosition.z + acceleration.z * (passedTime * passedTime),\r
+  );\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
+euler(currentPosition, acceleration, passedTime) {\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
+    currentPosition.x + acceleration.x * passedTime,\r
+    currentPosition.y + acceleration.y * passedTime,\r
+    currentPosition.z + acceleration.z * passedTime,\r
   );\r
 \r
   return nextPosition;\r
 }\r
 \r
+wind(intersects) {\r
+  let intersect = intersects[0];\r
+  this.geometry.vertices[intersect.face.a].z -= 0.05;\r
+  this.geometry.vertices[intersect.face.b].z -= 0.05;\r
+  this.geometry.vertices[intersect.face.c].z -= 0.05;\r
+}\r
 \r
 }\r
 \r