]> gitweb.ps.run Git - cloth_sim/commitdiff
render cloth
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Sat, 6 Feb 2021 10:25:43 +0000 (11:25 +0100)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Sat, 6 Feb 2021 10:25:43 +0000 (11:25 +0100)
Scripts/cloth.js
Scripts/main.js
index.html

index a05eb98926c45ab2e90522f9548c934af215a213..05ff6f1f30eb54da8985b99c786d3596662f214e 100644 (file)
@@ -1,3 +1,112 @@
 // cloth rendering\r
 // simulate\r
-// setup scene
\ No newline at end of file
+// setup scene\r
+\r
+const MASS = 0.1;\r
+\r
+class Particle {\r
+  constructor(x, y, z, mass) {\r
+    this.position = new THREE.Vector3(x, y, z);\r
+    this.previous = new THREE.Vector3(x, y, z);\r
+    this.acceleration = new THREE.Vector3(0, 0, 0);\r
+    this.mass = mass;\r
+  }\r
+  addForce(force) {\r
+\r
+  }\r
+  verlet(dt) {\r
+\r
+  }\r
+}\r
+\r
+class Cloth {\r
+  constructor(width, height, numPointsWidth, numPointsHeight) {\r
+    this.width = width;\r
+    this.height = height;\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
+     * because there are (n - 1) lines between n vertices\r
+     */\r
+    let stepWidth = width / (numPointsWidth - 1);\r
+    let stepHeight = height / (numPointsHeight - 1);\r
+\r
+    /**\r
+     * iterate over the number of vertices in x/y axis\r
+     * and add a new Particle to "particles"\r
+     */\r
+    this.particles = [];\r
+    for (let y = 0; y < numPointsHeight; y++) {\r
+      for (let x = 0; x < numPointsWidth; x++) {\r
+        this.particles.push(\r
+          new Particle(\r
+            (x - ((numPointsWidth-1)/2)) * stepWidth,\r
+            height - (y + ((numPointsHeight-1)/2)) * stepHeight,\r
+            0,\r
+            MASS)\r
+        );\r
+      }\r
+    }\r
+  }\r
+  generateGeometry() {\r
+    const geometry = new THREE.BufferGeometry();\r
+\r
+    const vertices = [];\r
+    const normals = [];\r
+    const indices = [];\r
+\r
+    for (let particle of this.particles) {\r
+      vertices.push(\r
+        particle.position.x,\r
+        particle.position.y,\r
+        particle.position.z);\r
+    }\r
+\r
+    const numPointsWidth = this.numPointsWidth;\r
+    const numPointsHeight = this.numPointsHeight;\r
+\r
+    /**\r
+     * helper function to calculate index of vertex\r
+     * in "vertices" array based on its x and y positions\r
+     * in the mesh\r
+     * @param {number} x - x index of vertex\r
+     * @param {number} y - y index of vertex\r
+     */\r
+    function getVertexIndex(x, y) {\r
+      return y * numPointsWidth + x;\r
+    }\r
+\r
+    /**\r
+     * generate faces based on 4 vertices\r
+     * and 6 springs each\r
+     */\r
+    for (let y = 0; y < numPointsHeight - 1; y++) {\r
+      for (let x = 0; x < numPointsWidth - 1; x++) {\r
+        indices.push(\r
+          getVertexIndex(x, y),\r
+          getVertexIndex(x+1, y),\r
+          getVertexIndex(x+1, y+1)\r
+        );\r
+        indices.push(\r
+          getVertexIndex(x, y),\r
+          getVertexIndex(x+1, y+1),\r
+          getVertexIndex(x, y+1)\r
+        );\r
+      }\r
+    }\r
+\r
+    geometry.setIndex(indices);\r
+    geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));\r
+    //geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));\r
+    geometry.computeBoundingSphere();\r
+    geometry.computeVertexNormals();\r
+\r
+    return geometry;\r
+  }\r
+  updateGeometry(geometry) {\r
+\r
+  }\r
+}
\ No newline at end of file
index 4d873377ca96cc4f6c5609b54bb723330d2553ff..d54aa9fc258ddc5e670268f519d3cfcb70eefe7c 100644 (file)
@@ -79,8 +79,16 @@ function init() {
 \r
   /** Setup scene */\r
   let [scene, camera, renderer] = setup_scene(canvasSpace);\r
-\r
-  // Add Cloth Initialization\r
+  \r
+  //const loader = new THREE.TextureLoader();\r
+  //Red color: 0xC70039\r
+\r
+  const cloth = new Cloth(1, 0.5, 20, 10);\r
+  const clothGeometry = cloth.generateGeometry();\r
+  //const clothMaterial = new THREE.MeshStandardMaterial({ map: loader.load('Textures/DeutschlandFlagge.jpg'), color: 0xffffff, side: THREE.DoubleSide, flatShading: false});\r
+  const clothMaterial = new THREE.MeshStandardMaterial({ color: 0xC70039, side: THREE.DoubleSide, flatShading: false });\r
+  const clothMesh = new THREE.Mesh(clothGeometry, clothMaterial);\r
+  scene.add(clothMesh);\r
   \r
   let raycaster = new THREE.Raycaster();\r
   let intersects;\r
index 20f3ed0640cd90ff37efb3967a9920b1b8bf8831..14f79e41311ad6a9018c97591356f3dfc5b445fe 100644 (file)
@@ -15,6 +15,7 @@
 </head>\r
 \r
 <body>\r
+       <script src="./Scripts/cloth.js"></script>\r
        <script src="./Scripts/main.js"></script>\r
 \r
        <div id="threejscontainer"></div>\r