abouttreesummaryrefslogcommitdiff
diff options
context:
space:
mode:
-rw-r--r--Scripts/cloth.js77
1 files changed, 56 insertions, 21 deletions
diff --git a/Scripts/cloth.js b/Scripts/cloth.js
index 05ff6f1..db07ac3 100644
--- a/Scripts/cloth.js
+++ b/Scripts/cloth.js
@@ -1,9 +1,13 @@
-// cloth rendering
-// simulate
-// setup scene
-
const MASS = 0.1;
+class Constraint {
+ constructor(p1, p2, distance) {
+ this.p1 = p1;
+ this.p2 = p2;
+ this.distance = distance;
+ }
+}
+
class Particle {
constructor(x, y, z, mass) {
this.position = new THREE.Vector3(x, y, z);
@@ -50,6 +54,32 @@ class Cloth {
);
}
}
+
+ const REST_DIST_X = width / (numPointsWidth-1);
+ const REST_DIST_Y = height / (numPointsHeight-1);
+
+ /**
+ * generate constraints (springs)
+ */
+ this.constraints = [];
+ for (let y = 0; y < numPointsHeight; y++) {
+ for (let x = 0; x < numPointsWidth; x++) {
+ if (x < numPointsWidth-1) {
+ this.constraints.push(new Constraint(
+ this.particles[this.getVertexIndex(x, y)],
+ this.particles[this.getVertexIndex(x+1, y)],
+ REST_DIST_X
+ ));
+ }
+ if (x < numPointsWidth-1) {
+ this.constraints.push(new Constraint(
+ this.particles[this.getVertexIndex(x, y)],
+ this.particles[this.getVertexIndex(x, y+1)],
+ REST_DIST_Y
+ ));
+ }
+ }
+ }
}
generateGeometry() {
const geometry = new THREE.BufferGeometry();
@@ -69,31 +99,20 @@ class Cloth {
const numPointsHeight = this.numPointsHeight;
/**
- * helper function to calculate index of vertex
- * in "vertices" array based on its x and y positions
- * in the mesh
- * @param {number} x - x index of vertex
- * @param {number} y - y index of vertex
- */
- function getVertexIndex(x, y) {
- return y * numPointsWidth + x;
- }
-
- /**
* generate faces based on 4 vertices
* and 6 springs each
*/
for (let y = 0; y < numPointsHeight - 1; y++) {
for (let x = 0; x < numPointsWidth - 1; x++) {
indices.push(
- getVertexIndex(x, y),
- getVertexIndex(x+1, y),
- getVertexIndex(x+1, y+1)
+ this.getVertexIndex(x, y),
+ this.getVertexIndex(x+1, y),
+ this.getVertexIndex(x+1, y+1)
);
indices.push(
- getVertexIndex(x, y),
- getVertexIndex(x+1, y+1),
- getVertexIndex(x, y+1)
+ this.getVertexIndex(x, y),
+ this.getVertexIndex(x+1, y+1),
+ this.getVertexIndex(x, y+1)
);
}
}
@@ -109,4 +128,20 @@ class Cloth {
updateGeometry(geometry) {
}
+ satisfyConstraints() {
+
+ }
+ simulate() {
+
+ }
+ /**
+ * helper function to calculate index of vertex
+ * in "vertices" array based on its x and y positions
+ * in the mesh
+ * @param {number} x - x index of vertex
+ * @param {number} y - y index of vertex
+ */
+ getVertexIndex(x, y) {
+ return y * this.numPointsWidth + x;
+ }
} \ No newline at end of file