X-Git-Url: https://gitweb.ps.run/cloth_sim/blobdiff_plain/e05a5c0a7aa891179af219eff0df34647acc8eb3..9866c25fe964b215e0e491638ee541bdf8d29f56:/Scripts/cloth.js diff --git a/Scripts/cloth.js b/Scripts/cloth.js index 9389ef6..f1ac682 100644 --- a/Scripts/cloth.js +++ b/Scripts/cloth.js @@ -6,9 +6,9 @@ */ function vectorLength(a, b) { let v1 = new THREE.Vector3(); - v1.set(a.x, a.y, a.z); + v1.copy(a); let v2 = new THREE.Vector3(); - v2.set(b.x, b.y, b.z); + v2.copy(b); return v1.sub(v2).length(); } @@ -65,17 +65,19 @@ export class Spring { } getDirection(vertices) { - let direction = new THREE.Vector3( - vertices[this.index1].x, - vertices[this.index1].y, - vertices[this.index1].z - ); + let direction = new THREE.Vector3(); + direction.copy(vertices[this.index1]); direction.sub(vertices[this.index2]); direction.divideScalar(vectorLength(vertices[this.index1], vertices[this.index2])); return direction; } + + update(vertices) { + let length = vectorLength(vertices[this.index1], vertices[this.index2]); + this.currentLength = length; + } } /** @@ -94,7 +96,7 @@ export class Cloth { vertexWeights = []; - + vertexRigidness = []; /** * creates a rectangular piece of cloth @@ -170,6 +172,12 @@ export class Cloth { * with generated vertices and faces */ this.createExplicit(vertices, faces); + + /** + * hand cloth from left and right upper corners + */ + this.vertexRigidness[0] = true; + this.vertexRigidness[numPointsWidth-1] = true; } /** @@ -186,9 +194,12 @@ export class Cloth { * Copy vertices and initialize vertex weights to 0 */ for (let i in vertices) { - this.geometry.vertices.push(vertices[i]); - this.previousPositions.push(vertices[i]); + this.geometry.vertices.push(vertices[i].clone()); + this.previousPositions.push(vertices[i].clone()); + // this.geometry.vertices.push(vertices[i]); + // this.previousPositions.push(vertices[i]); this.vertexWeights.push(0); + this.vertexRigidness.push(false); } /** * copy faces, @@ -303,25 +314,34 @@ export class Cloth { time = 0; /** * - * @param {number} dt + * @param {number} dt time in seconds since last frame */ simulate(dt) { - - - for (let i in this.geometry.vertices) { - let currentPosition; let acceleration = this.getAcceleration(i, dt); + + //acceleration.clampLength(0, 10); + + if (Math.abs(acceleration.length()) <= 10e-4) { + acceleration.set(0, 0, 0); + } - currentPosition = this.verlet(this.geometry.vertices[i], this.previousPositions[i], acceleration, dt/2000); + let currentPosition = this.verlet(this.geometry.vertices[i].clone(), this.previousPositions[i].clone(), acceleration, dt); + //let currentPosition = this.euler(this.geometry.vertices[i], acceleration, dt); - this.previousPositions[i] = currentPosition; - this.geometry.vertices[i] = currentPosition; - + this.previousPositions[i].copy(this.geometry.vertices[i]); + this.geometry.vertices[i].copy(currentPosition); } - console.log(this.geometry.vertices[0]); + //console.log(this.getAcceleration(1, dt)); + this.time += dt; + for (let face of this.faces) { + for (let spring of face.springs) { + spring.update(this.geometry.vertices); + } + } + /** * let THREE JS compute bounding sphere around generated mesh * needed for View Frustum Culling internally @@ -341,41 +361,46 @@ export class Cloth { * @param {number} dt The time passed since last frame */ getAcceleration(vertexIndex, dt) { + if (this.vertexRigidness[vertexIndex]) + return new THREE.Vector3(0, 0, 0); let vertex = this.geometry.vertices[vertexIndex]; // Mass of vertex let M = this.vertexWeights[vertexIndex]; // constant gravity - let g = new THREE.Vector3(0, -1.8, 0); + let g = new THREE.Vector3(0, -9.8, 0); // stiffness - let k = 5; + let k = 500; // Wind vector let fWind = new THREE.Vector3( Math.sin(vertex.x * vertex.y * this.time), - Math.cos(vertex.z* this.time), + Math.cos(vertex.z * this.time), Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z)) ); + fWind.set(0, 0, 0); /** * constant determined by the properties of the surrounding fluids (air) * achievement of cloth effects through try out * */ - let a = 1; - + let a = 0.01; + let velocity = new THREE.Vector3( - (vertex.x - this.previousPositions[vertexIndex].x) / dt, - (vertex.y - this.previousPositions[vertexIndex].y) / dt, - (vertex.z - this.previousPositions[vertexIndex].z) / dt + (this.previousPositions[vertexIndex].x - vertex.x) / dt, + (this.previousPositions[vertexIndex].y - vertex.y) / dt, + (this.previousPositions[vertexIndex].z - vertex.z) / dt ); + //console.log(velocity, vertex, this.previousPositions[vertexIndex]); - let fAirResistance = velocity.multiplyScalar(-a); - + let fAirResistance = velocity.multiply(velocity).multiplyScalar(-a); + let springSum = new THREE.Vector3(0, 0, 0); // Get the bounding springs and add them to the needed springs + // TODO: optimize for (let i in this.faces) { if (this.faces[i].a == vertexIndex || this.faces[i].b == vertexIndex || this.faces[i].c == vertexIndex || this.faces[i].d == vertexIndex) { for (let j in this.faces[i].springs) { @@ -387,24 +412,21 @@ getAcceleration(vertexIndex, dt) { if (this.faces[i].springs[j].index1 == vertexIndex) springDirection.multiplyScalar(-1); - - springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength))); + + springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength))); } } } } - let result = new THREE.Vector3(1, 1, 1); - - result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).sub(springSum); - + let temp = result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).clone(); + result.sub(springSum); + document.getElementById("Output").innerText = "SpringSum: " + Math.floor(springSum.y) + "\nTemp: " + Math.floor(temp.y); return result; - - } /** @@ -422,14 +444,23 @@ verlet(currentPosition, previousPosition, acceleration, passedTime) { // Dependency for one vertex: gravity, fluids/air, springs let nextPosition = new THREE.Vector3( - 2 * currentPosition.x - previousPosition.x + acceleration.x * (passedTime * passedTime), - 2 * currentPosition.y - previousPosition.y + acceleration.y * (passedTime * passedTime), - 2 * currentPosition.z - previousPosition.z + acceleration.z * (passedTime * passedTime), + (2 * currentPosition.x) - previousPosition.x + acceleration.x * (passedTime * passedTime), + (2 * currentPosition.y) - previousPosition.y + acceleration.y * (passedTime * passedTime), + (2 * currentPosition.z) - previousPosition.z + acceleration.z * (passedTime * passedTime), ); return nextPosition; } +euler(currentPosition, acceleration, passedTime) { + let nextPosition = new THREE.Vector3( + currentPosition.x + acceleration.x * passedTime, + currentPosition.y + acceleration.y * passedTime, + currentPosition.z + acceleration.z * passedTime, + ); + + return nextPosition; +} }