-\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
+/**\r
+ * Convenience Function for calculating the distance between two vectors\r
+ * because THREE JS Vector functions mutate variables\r
+ * @param {Vector3} a - Vector A\r
+ * @param {Vector3} b - Vector B\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
+/**\r
+ * Class representing a quad face\r
+ * Each face consists of two triangular mesh faces\r
+ * containts four indices for determining vertices\r
+ * and six springs, one between each of the vertices\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
+/**\r
+ * Class representing a single spring\r
+ * has a current and resting length\r
+ * and indices to the two connected vertices\r
+ */\r
+class Spring {\r
+ restLength;\r
+ currentLength;\r
+ index1;\r
+ index2;\r
+\r
+ \r
+ /**\r
+ * set vertex indices\r
+ * and calculate inital length based on the\r
+ * vertex positions\r
+ * @param {Array of Vector3} vertices \r
+ * @param {number} index1 \r
+ * @param {number} 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
+/**\r
+ * Class representing a single piece of cloth\r
+ * contains THREE JS geometry,\r
+ * logically represented by an array of adjacent faces\r
+ * and vertex weights which are accessed by the same\r
+ * indices as the vertices in the Mesh\r
+ */\r
+class Cloth {\r
+ VertexWeight = 1;\r
+\r
+ geometry = new THREE.Geometry();\r
+\r
+ faces = [];\r
+\r
+ vertexWeights = [];\r
+\r
+ \r
+ /**\r
+ * creates a rectangular piece of cloth\r
+ * takes the size of the cloth\r
+ * and the number of vertices it should be composed of\r
+ * @param {number} width - width of the cloth\r
+ * @param {number} height - height of the cloth\r
+ * @param {number} numPointsWidth - number of vertices in horizontal direction\r
+ * @param {number} numPointsHeight - number of vertices in vertical direction\r
+ */\r
+ createBasic(width, height, numPointsWidth, numPointsHeight) {\r
+ /** resulting vertices and faces */\r
+ let vertices = [];\r
+ let faces = [];\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 Vector3 to "vertices"\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