- \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
- );\r
- }\r
+ }\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
+ 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