]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/main.js
Render Vertices and Springs
[cloth_sim] / Scripts / main.js
index f395b6c19993a1c5821ff1ee5ae8d4164629ab32..fb7a8b21c9dd444f48bb1809df003d524a85561f 100644 (file)
@@ -26,24 +26,195 @@ 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
+    faces = [];\r
+\r
+    vertexWeights = [];\r
+\r
+    createBasic(width, height, numPointsWidth, numPointsHeight) {\r
+      let vertices = [];\r
+      let faces = [];\r
+\r
+      let stepWidth = width / (numPointsWidth - 1);\r
+      let stepHeight = height / (numPointsHeight - 1);\r
+\r
+      for (let y = 0; y < numPointsHeight; y++) {\r
+        for (let x = 0; x < numPointsWidth; x++) {\r
+          vertices.push(\r
+            new THREE.Vector3(x * stepWidth, height - y * stepHeight, 0)\r
+          );\r
+        }\r
+      }\r
+\r
+      function getVertexIndex(x, y) {\r
+        return y * numPointsWidth + x;\r
+      }\r
+      \r
+      for (let y = 0; y < numPointsHeight - 1; y++) {\r
+        for (let x = 0; x < numPointsWidth - 1; x++) {\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
+      this.createExplicit(vertices, faces);\r
+    }\r
+\r
+    createExplicit(vertices, faces) {\r
+      for (let i in vertices) {\r
+        this.geometry.vertices.push(vertices[i]);\r
+        this.vertexWeights.push(0);\r
+      }\r
+      for (let i in faces) {\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
+      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
+      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
   let mousePos = new Point();\r
 \r
+  const canvasSpace = 200;\r
+\r
   const scene = new THREE.Scene();\r
-  const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);\r
+  const camera = new THREE.PerspectiveCamera(75, window.innerWidth / (window.innerHeight - canvasSpace), 0.1, 1000);\r
 \r
   const renderer = new THREE.WebGLRenderer();\r
-  renderer.setSize(window.innerWidth, window.innerHeight - 200);\r
+  renderer.setSize(window.innerWidth, window.innerHeight - canvasSpace);\r
   document.getElementById("threejscontainer").appendChild(renderer.domElement);\r
 \r
   const directionalLight = new THREE.DirectionalLight(0xffffff, 1);\r
   scene.add(directionalLight);\r
 \r
-  const geometry = new THREE.BoxGeometry();\r
-  const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });\r
-  const cube = new THREE.Mesh(geometry, material);\r
-  scene.add(cube);\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.z = 5;\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
@@ -60,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