]> gitweb.ps.run Git - cloth_sim/commitdiff
change material
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Fri, 22 Jan 2021 15:16:32 +0000 (16:16 +0100)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Fri, 22 Jan 2021 15:16:32 +0000 (16:16 +0100)
Scripts/cloth.js
Scripts/main.js

index f1ac682fa3c9a4841f425ee9412b0b9d523ff159..038b235e4a4da2817c487f0d539af2562d340640 100644 (file)
@@ -156,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
@@ -247,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
@@ -350,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
@@ -401,23 +403,69 @@ getAcceleration(vertexIndex, dt) {
 \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 = 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
-          if (this.faces[i].springs[j].index1 == vertexIndex)\r
-            springDirection.multiplyScalar(-1);\r
-     \r
-          springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength)));\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
-        }\r
-      }\r
-    }\r
+  for (let spring of springs) {\r
+    let springDirection = spring.getDirection(this.geometry.vertices);\r
+\r
+    if (spring.index1 == vertexIndex)\r
+      springDirection.multiplyScalar(-1);\r
+\r
+    springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength)));\r
   }\r
   \r
   let result = new THREE.Vector3(1, 1, 1);\r
index 8c61f82566a2653bc8f890435f7a41a30f098186..4e7c2ef7bb32e51f874a950f2697f75586f547e4 100644 (file)
@@ -84,7 +84,8 @@ function init() {
   cloth.createBasic(10, 10, 10, 10);\r
   //cloth.createDebugMesh(scene);\r
 \r
-  const material = new THREE.MeshBasicMaterial({ color: 0x0000ff });\r
+  //const material = new THREE.MeshBasicMaterial({ color: 0x0000ff, side: THREE.DoubleSide });\r
+  const material = new THREE.MeshPhongMaterial({ color: 0x0000ff, side: THREE.DoubleSide });\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
@@ -93,6 +94,12 @@ function init() {
   //line.material.transparent = true;\r
   scene.add(mesh);\r
 \r
+  scene.add( new THREE.AmbientLight( 0x666666 ) );\r
+\r
+  const light = new THREE.DirectionalLight( 0xffffff, 0.5 );\r
+  light.position.set( 0, 1, 0.5 );\r
+  scene.add( light );\r
+\r
   /**\r
    * function called every frame\r
    * @param {number} dt - time passed since last frame in ms\r