]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/main.js
Render Vertices and Springs
[cloth_sim] / Scripts / main.js
index 843c19a5044a32bc6ee7a24d8d01cdd4ecc50f59..fb7a8b21c9dd444f48bb1809df003d524a85561f 100644 (file)
@@ -26,12 +26,57 @@ function init() {
     }\r
   }\r
 \r
+  function vectorLength(a, b) {\r
+    let v1 = new THREE.Vector3();\r
+    v1.set(a.x, a.y, a.z);\r
+    let v2 = new THREE.Vector3();\r
+    v2.set(b.x, b.y, b.z);\r
+\r
+    return v1.sub(v2).length();\r
+  }\r
+\r
+  class Face {\r
+    a;\r
+    b;\r
+    c;\r
+    d;\r
+\r
+    springs = [];\r
+\r
+    constructor(a, b, c, d) {\r
+      this.a = a;\r
+      this.b = b;\r
+      this.c = c;\r
+      this.d = d;\r
+    }\r
+  }\r
+\r
+  class Spring {\r
+    restLength;\r
+    currentLength;\r
+    index1;\r
+    index2;\r
+\r
+    constructor(vertices, index1, index2) {\r
+      this.index1 = index1;\r
+      this.index2 = index2;\r
+\r
+      let length = vectorLength(vertices[index1], vertices[index2]);\r
+      this.restLength = length;\r
+      this.currentLength = length;\r
+    }\r
+  }\r
+\r
   class Cloth {\r
+    VertexWeight = 1;\r
+\r
     geometry = new THREE.Geometry();\r
 \r
-    \r
+    faces = [];\r
+\r
+    vertexWeights = [];\r
 \r
-    static CreateBasic(width, height, numPointsWidth, numPointsHeight) {\r
+    createBasic(width, height, numPointsWidth, numPointsHeight) {\r
       let vertices = [];\r
       let faces = [];\r
 \r
@@ -52,39 +97,96 @@ function init() {
       \r
       for (let y = 0; y < numPointsHeight - 1; y++) {\r
         for (let x = 0; x < numPointsWidth - 1; x++) {\r
-          faces.push(\r
-            new THREE.Face3(\r
-              getVertexIndex(x, y),\r
-              getVertexIndex(x, y + 1),\r
-              getVertexIndex(x + 1, y),\r
-            )\r
-          );\r
-          faces.push(\r
-            new THREE.Face3(\r
-              getVertexIndex(x + 1, y),\r
-              getVertexIndex(x, y + 1),\r
-              getVertexIndex(x + 1, y + 1),\r
-            )\r
+          let newFace = new Face(\r
+            getVertexIndex(x, y),\r
+            getVertexIndex(x, y + 1),\r
+            getVertexIndex(x + 1, y),\r
+            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
+          \r
+          faces.push(newFace);\r
         }\r
       }\r
 \r
-      return this.CreateExplicit(vertices, faces);\r
+      this.createExplicit(vertices, faces);\r
     }\r
 \r
-    static CreateExplicit(vertices, faces) {\r
-      let result = new Cloth();\r
-\r
+    createExplicit(vertices, faces) {\r
       for (let i in vertices) {\r
-        result.geometry.vertices.push(vertices[i]);\r
+        this.geometry.vertices.push(vertices[i]);\r
+        this.vertexWeights.push(0);\r
       }\r
       for (let i in faces) {\r
-        result.geometry.faces.push(faces[i]);\r
+        let face = faces[i];\r
+\r
+        this.faces.push(face);\r
+\r
+        this.geometry.faces.push(new THREE.Face3(\r
+          face.a, face.b, face.c\r
+        ));\r
+        this.geometry.faces.push(new THREE.Face3(\r
+          face.c, face.b, face.d\r
+        ));\r
+        \r
+        let xLength = vectorLength(this.geometry.vertices[face.b], this.geometry.vertices[face.a]);\r
+        let yLength = vectorLength(this.geometry.vertices[face.c], this.geometry.vertices[face.a]);\r
+        let weight = xLength * yLength / 2;\r
+\r
+        xLength = vectorLength(this.geometry.vertices[face.b], this.geometry.vertices[face.d]);\r
+        yLength = vectorLength(this.geometry.vertices[face.c], this.geometry.vertices[face.d]);\r
+        weight += xLength * yLength / 2;\r
+\r
+        this.vertexWeights[face.a] += weight / 4;\r
+        this.vertexWeights[face.b] += weight / 4;\r
+        this.vertexWeights[face.c] += weight / 4;\r
+        this.vertexWeights[face.d] += weight / 4;\r
       }\r
 \r
-      result.geometry.computeBoundingSphere();\r
+      this.geometry.computeBoundingSphere();\r
+    }\r
+\r
+    createDebugMesh(scene) {\r
+      function addLine(from, to, color) {\r
+        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 line = new THREE.Line(geometry, material);\r
+        line.renderOrder = 1;\r
+        scene.add(line);\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
+        sphere.position.set(point.x, point.y, point.z);\r
+        scene.add( sphere );\r
+      }\r
 \r
-      return result;\r
+      let lineColor = 0x000000;\r
+      let pointColor = 0xff00000;\r
+\r
+      for (let i in this.faces) {\r
+        let face = this.faces[i];\r
+        addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.b], lineColor);\r
+        addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.c], lineColor);\r
+        addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.d], lineColor);\r
+        addLine(this.geometry.vertices[face.b], this.geometry.vertices[face.c], lineColor);\r
+        addLine(this.geometry.vertices[face.b], this.geometry.vertices[face.d], lineColor);\r
+        addLine(this.geometry.vertices[face.c], this.geometry.vertices[face.d], lineColor);\r
+\r
+        addPoint(this.geometry.vertices[face.a], pointColor);\r
+        addPoint(this.geometry.vertices[face.b], pointColor);\r
+        addPoint(this.geometry.vertices[face.c], pointColor);\r
+        addPoint(this.geometry.vertices[face.d], pointColor);\r
+      }\r
     }\r
   }\r
 \r
@@ -102,13 +204,17 @@ function init() {
   const directionalLight = new THREE.DirectionalLight(0xffffff, 1);\r
   scene.add(directionalLight);\r
 \r
-  let cloth = Cloth.CreateBasic(10, 10, 4, 4);\r
-  const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });\r
+  let cloth = new Cloth();\r
+  cloth.createBasic(10, 10, 5, 5);\r
+  cloth.createDebugMesh(scene);\r
+\r
+  const material = new THREE.MeshBasicMaterial({ color: 0x0000ff });\r
   const mesh = new THREE.Mesh(cloth.geometry, material);\r
   scene.add(mesh);\r
 \r
   camera.position.y = 5;\r
   camera.position.z = 10;\r
+  //camera.lookAt(new THREE.Vector3(1, 0, 0));\r
 \r
   function animate(dt) {\r
     requestAnimationFrame(animate);\r
@@ -125,7 +231,6 @@ function init() {
   window.onresize = resize;\r
   resize();\r
   if (canvas.getContext) {\r
-    ctx = canvas.getContext('2d');\r
     animate(performance.now());\r
   }\r
   canvas.onmousemove = (evt) => {\r