]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
Wind Force added, Cloth dragging added, fix vertex normals and change lighting
[cloth_sim] / Scripts / cloth.js
index e39b95b9850400cf98cbd7c20f5f3aa1b5d73fbb..c873a3d05cd232d1aa5c361dc336b6e343393ed3 100644 (file)
@@ -98,6 +98,9 @@ export class Cloth {
 \r
   vertexRigidness = [];\r
 \r
+  externalForces = [];\r
+  windForce = 0;\r
+\r
   /**\r
    * creates a rectangular piece of cloth\r
    * takes the size of the cloth\r
@@ -203,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
@@ -251,6 +255,7 @@ export class Cloth {
      */\r
     this.geometry.computeBoundingSphere();\r
     this.geometry.computeFaceNormals();\r
+    this.geometry.computeVertexNormals();\r
   }\r
 \r
   /**\r
@@ -355,6 +360,7 @@ export class Cloth {
     this.geometry.elementsNeedUpdate = true;\r
     this.geometry.computeBoundingSphere();\r
     this.geometry.computeFaceNormals();\r
+    this.geometry.computeVertexNormals();\r
 \r
   }\r
 \r
@@ -369,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
@@ -471,10 +478,31 @@ getAcceleration(vertexIndex, dt) {
   }\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 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
+  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
+  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
@@ -492,7 +520,7 @@ 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
+  const DRAG = 0.96;\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
@@ -520,9 +548,36 @@ euler(currentPosition, acceleration, passedTime) {
 \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
+  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