]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
Distinguish between right and left mouse click for wind and dragging
[cloth_sim] / Scripts / cloth.js
index 14975839294f6f45356f3e2e2802d94c3c7cb456..1ec79d5bec9bdff2f11b4bed1b67a5c4417658c3 100644 (file)
@@ -98,6 +98,9 @@ export class Cloth {
 \r
   vertexRigidness = [];\r
 \r
+  externalForces = [];\r
+  windForce = 100;\r
+\r
   /**\r
    * creates a rectangular piece of cloth\r
    * takes the size of the cloth\r
@@ -112,6 +115,9 @@ export class Cloth {
     let vertices = [];\r
     let faces = [];\r
 \r
+    this.numPointsWidth = numPointsWidth;\r
+    this.numPointsHeight = numPointsHeight;\r
+\r
     /**\r
      * distance between two vertices horizontally/vertically\r
      * divide by the number of points minus one\r
@@ -156,12 +162,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
@@ -200,6 +206,7 @@ export class Cloth {
       // this.previousPositions.push(vertices[i]);\r
       this.vertexWeights.push(0);\r
       this.vertexRigidness.push(false);\r
+      this.externalForces.push(new THREE.Vector3(0,0,0));\r
     }\r
     /**\r
      * copy faces,\r
@@ -247,6 +254,8 @@ export class Cloth {
      * needed for View Frustum Culling internally\r
      */\r
     this.geometry.computeBoundingSphere();\r
+    this.geometry.computeFaceNormals();\r
+    this.geometry.computeVertexNormals();\r
   }\r
 \r
   /**\r
@@ -350,6 +359,8 @@ export class Cloth {
     this.geometry.verticesNeedUpdate = true;\r
     this.geometry.elementsNeedUpdate = true;\r
     this.geometry.computeBoundingSphere();\r
+    this.geometry.computeFaceNormals();\r
+    this.geometry.computeVertexNormals();\r
 \r
   }\r
 \r
@@ -364,7 +375,8 @@ getAcceleration(vertexIndex, dt) {
   if (this.vertexRigidness[vertexIndex])\r
     return new THREE.Vector3(0, 0, 0);\r
 \r
-  let vertex = this.geometry.vertices[vertexIndex];\r
+  let externalForce = this.externalForces[vertexIndex];\r
+  let vertex = this.geometry.vertices[vertexIndex];//.add(externalForce);\r
 \r
   // Mass of vertex\r
   let M = this.vertexWeights[vertexIndex];\r
@@ -379,14 +391,13 @@ getAcceleration(vertexIndex, dt) {
     Math.cos(vertex.z * this.time),\r
     Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
   );\r
-  fWind.set(0, 0, 0);\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 = 0.01;\r
-\r
+  let a = 0.1;\r
+  \r
   let velocity = new THREE.Vector3(\r
     (this.previousPositions[vertexIndex].x - vertex.x) / dt,\r
     (this.previousPositions[vertexIndex].y - vertex.y) / dt,\r
@@ -395,34 +406,104 @@ getAcceleration(vertexIndex, dt) {
 \r
   //console.log(velocity, vertex, this.previousPositions[vertexIndex]);\r
 \r
-  let fAirResistance = velocity.cross(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
   // TODO: optimize\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
+  const numPointsX = this.numPointsWidth;\r
+  const numPointsY = this.numPointsHeight;\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
+  for (let spring of springs) {\r
+    let springDirection = spring.getDirection(this.geometry.vertices);\r
 \r
-          if (this.faces[i].springs[j].index1 == vertexIndex)\r
-            springDirection.multiplyScalar(-1);\r
+    if (spring.index1 == vertexIndex)\r
+      springDirection.multiplyScalar(-1);\r
 \r
-          springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength)));\r
-        }\r
-      }\r
-    }\r
+    springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength)));\r
   }\r
   \r
   let result = new THREE.Vector3(1, 1, 1);\r
+  result.multiplyScalar(M).multiply(g).add(fWind).add(externalForce).add(fAirResistance).sub(springSum);\r
+\r
+  document.getElementById("Output").innerText = "SpringSum: " + Math.floor(springSum.y);\r
+\r
+  let threshold = 1;\r
+  let forceReduktion = 0.8;\r
+  if(Math.abs(externalForce.z) > threshold){\r
+    externalForce.z *= forceReduktion;\r
+  } else {\r
+    externalForce.z = 0;\r
+  }\r
 \r
-  result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).sub(springSum);\r
+  if(Math.abs(externalForce.y) > threshold){\r
+    externalForce.y *= forceReduktion;\r
+  } else {\r
+    externalForce.y = 0;\r
+  }\r
+\r
+  if(Math.abs(externalForce.x) > threshold){\r
+    externalForce.x *= forceReduktion;\r
+  } else {\r
+    externalForce.x = 0;\r
+  }\r
+    \r
   \r
+\r
   return result;\r
 }\r
 \r
@@ -439,13 +520,19 @@ 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
-\r
+  const DRAG = 0.96;\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 - 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
@@ -459,5 +546,39 @@ euler(currentPosition, acceleration, passedTime) {
   return nextPosition;\r
 }\r
 \r
+wind(intersects) {\r
+  let intersect = intersects[0];\r
+  this.externalForces[intersect.face.a].z -= this.windForce;\r
+  this.externalForces[intersect.face.b].z -= this.windForce;\r
+  this.externalForces[intersect.face.c].z -= this.windForce;\r
+}\r
+\r
+mousePressed = false;\r
+mouseMoved = false;\r
+intersects;\r
+\r
+mousePress(intersects){\r
+  this.mousePressed = true;\r
+  this.intersects = intersects;\r
+\r
+}\r
+\r
+mouseMove(mousePos){\r
+  this.mouseMoved = true;\r
+  if(this.mousePressed){\r
+    let intersect = this.intersects[0];\r
+    this.externalForces[intersect.face.a].add(mousePos.clone().sub(this.geometry.vertices[intersect.face.a]).multiplyScalar(90));\r
+    /*\r
+    this.geometry.vertices[intersect.face.a].x = mousePos.x;\r
+    this.geometry.vertices[intersect.face.a].y = mousePos.y;\r
+    this.geometry.vertices[intersect.face.a].z = mousePos.z;\r
+  */  \r
+  }\r
+}\r
+\r
+mouseRelease(){\r
+  this.mousePressed = false;\r
+}\r
+\r
 }\r
 \r