X-Git-Url: https://gitweb.ps.run/cloth_sim/blobdiff_plain/fcfb3d929c2fefdfdcb6cb9351bcdd0d2d14f9f2..e05a5c0a7aa891179af219eff0df34647acc8eb3:/Scripts/cloth.js diff --git a/Scripts/cloth.js b/Scripts/cloth.js index e84facc..9389ef6 100644 --- a/Scripts/cloth.js +++ b/Scripts/cloth.js @@ -46,12 +46,12 @@ export class Spring { index1; index2; - + /** * set vertex indices * and calculate inital length based on the * vertex positions - * @param {Array of Vector3} vertices + * @param {Array} vertices * @param {number} index1 * @param {number} index2 */ @@ -63,6 +63,19 @@ export class Spring { this.restLength = length; this.currentLength = length; } + + getDirection(vertices) { + let direction = new THREE.Vector3( + vertices[this.index1].x, + vertices[this.index1].y, + vertices[this.index1].z + ); + + direction.sub(vertices[this.index2]); + direction.divideScalar(vectorLength(vertices[this.index1], vertices[this.index2])); + + return direction; + } } /** @@ -81,7 +94,8 @@ export class Cloth { vertexWeights = []; - + + /** * creates a rectangular piece of cloth * takes the size of the cloth @@ -126,7 +140,7 @@ export class Cloth { function getVertexIndex(x, y) { return y * numPointsWidth + x; } - + /** * generate faces based on 4 vertices * and 6 springs each @@ -146,7 +160,7 @@ export class Cloth { newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x, y + 1))); newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x + 1, y + 1))); newFace.springs.push(new Spring(vertices, getVertexIndex(x, y + 1), getVertexIndex(x + 1, y + 1))); - + faces.push(newFace); } } @@ -163,15 +177,17 @@ export class Cloth { * (list of vertices and list of indices representing triangles) * and calculate the weight of each face and split it between * surrounding vertices - * @param {Array of Vector3} vertices - * @param {Array of Face} faces + * @param {Array} vertices + * @param {Array} faces */ createExplicit(vertices, faces) { + /** * 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.vertexWeights.push(0); } /** @@ -193,7 +209,7 @@ export class Cloth { this.geometry.faces.push(new THREE.Face3( face.c, face.b, face.d )); - + /** * calculate area of face as combined area of * its two composing triangles @@ -240,7 +256,7 @@ export class Cloth { let geometry = new THREE.Geometry(); geometry.vertices.push(from); geometry.vertices.push(to); - let material = new THREE.LineBasicMaterial( { color: color, linewidth: 10 } ); + let material = new THREE.LineBasicMaterial({ color: color, linewidth: 10 }); let line = new THREE.Line(geometry, material); line.renderOrder = 1; scene.add(line); @@ -252,11 +268,11 @@ export class Cloth { * @param {number} color */ function addPoint(point, color) { - const geometry = new THREE.SphereGeometry( 0.05, 32, 32 ); - const material = new THREE.MeshBasicMaterial( { color: color } ); - const sphere = new THREE.Mesh( geometry, material ); + const geometry = new THREE.SphereGeometry(0.05, 32, 32); + const material = new THREE.MeshBasicMaterial({ color: color }); + const sphere = new THREE.Mesh(geometry, material); sphere.position.set(point.x, point.y, point.z); - scene.add( sphere ); + scene.add(sphere); } let lineColor = 0x000000; @@ -282,4 +298,138 @@ export class Cloth { addPoint(this.geometry.vertices[face.d], pointColor); } } -} \ No newline at end of file + + previousPositions = []; + time = 0; + /** + * + * @param {number} dt + */ + simulate(dt) { + + + + for (let i in this.geometry.vertices) { + let currentPosition; + let acceleration = this.getAcceleration(i, dt); + + currentPosition = this.verlet(this.geometry.vertices[i], this.previousPositions[i], acceleration, dt/2000); + + this.previousPositions[i] = currentPosition; + this.geometry.vertices[i] = currentPosition; + + } + console.log(this.geometry.vertices[0]); + this.time += dt; + + /** + * let THREE JS compute bounding sphere around generated mesh + * needed for View Frustum Culling internally + */ + + this.geometry.verticesNeedUpdate = true; + this.geometry.elementsNeedUpdate = true; + this.geometry.computeBoundingSphere(); + + } + + + +/** + * Equation of motion for each vertex which represents the acceleration + * @param {number} vertexIndex The index of the current vertex whose acceleration should be calculated + * @param {number} dt The time passed since last frame + */ +getAcceleration(vertexIndex, dt) { + + 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); + // stiffness + let k = 5; + + // Wind vector + let fWind = new THREE.Vector3( + Math.sin(vertex.x * vertex.y * this.time), + Math.cos(vertex.z* this.time), + Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z)) + ); + + /** + * constant determined by the properties of the surrounding fluids (air) + * achievement of cloth effects through try out + * */ + let a = 1; + + 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 + ); + + + let fAirResistance = velocity.multiplyScalar(-a); + + let springSum = new THREE.Vector3(0, 0, 0); + + // Get the bounding springs and add them to the needed springs + 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) { + if (this.faces[i].springs[j].index1 == vertexIndex || this.faces[i].springs[j].index2 == vertexIndex) { + + let spring = this.faces[i].springs[j]; + let springDirection = spring.getDirection(this.geometry.vertices); + + + if (this.faces[i].springs[j].index1 == vertexIndex) + springDirection.multiplyScalar(-1); + + springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength))); + + } + } + } + } + + + let result = new THREE.Vector3(1, 1, 1); + + + result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).sub(springSum); + + + return result; + + +} + +/** + * The Verlet algorithm as an integrator + * to get the next position of a vertex + * @param {Vector3} currentPosition + * @param {Vector3} previousPosition + * @param {Vector3} acceleration + * @param {number} passedTime The delta time since last frame + */ +verlet(currentPosition, previousPosition, acceleration, passedTime) { + // verlet algorithm + // next position = 2 * current Position - previous position + acceleration * (passed time)^2 + // acceleration (dv/dt) = F(net) + // 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), + ); + + return nextPosition; +} + + +} +