- //console.log(this.getAcceleration(1, dt));\r
- \r
- this.time += dt;\r
-\r
- for (let face of this.faces) {\r
- for (let spring of face.springs) {\r
- spring.update(this.geometry.vertices);\r
- }\r
- }\r
-\r
- /**\r
- * let THREE JS compute bounding sphere around generated mesh\r
- * needed for View Frustum Culling internally\r
- */\r
-\r
- this.geometry.verticesNeedUpdate = true;\r
- this.geometry.elementsNeedUpdate = true;\r
- this.geometry.computeBoundingSphere();\r
- this.geometry.computeFaceNormals();\r
- this.geometry.computeVertexNormals();\r
-\r
- }\r
-\r
-\r
-\r
-/**\r
- * Equation of motion for each vertex which represents the acceleration \r
- * @param {number} vertexIndex The index of the current vertex whose acceleration should be calculated\r
- * @param {number} dt The time passed since last frame\r
- */\r
-getAcceleration(vertexIndex, dt) {\r
- if (this.vertexRigidness[vertexIndex])\r
- return new THREE.Vector3(0, 0, 0);\r
-\r
- let externalForce = this.externalForces[vertexIndex];\r
- let vertex = this.geometry.vertices[vertexIndex];//.add(externalForce);\r
-\r
- // Mass of vertex\r
- let M = this.vertexWeights[vertexIndex];\r
- // constant gravity\r
- let g = new THREE.Vector3(0, -9.8, 0);\r
- // stiffness\r
- let k = 1000;\r
-\r
- // Wind vector\r
- let fWind = new THREE.Vector3(\r
- Math.sin(vertex.x * vertex.y * this.time),\r
- Math.cos(vertex.z * this.time),\r
- Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
- );\r
-\r
- /**\r
- * constant determined by the properties of the surrounding fluids (air)\r
- * achievement of cloth effects through try out\r
- * */\r
- let a = 0.1;\r
- \r
- let velocity = new THREE.Vector3(\r
- (this.previousPositions[vertexIndex].x - vertex.x) / dt,\r
- (this.previousPositions[vertexIndex].y - vertex.y) / dt,\r
- (this.previousPositions[vertexIndex].z - vertex.z) / dt\r
- );\r
-\r
- //console.log(velocity, vertex, this.previousPositions[vertexIndex]);\r
-\r
- let fAirResistance = velocity.multiply(velocity).multiplyScalar(-a);\r
- \r
- let springSum = new THREE.Vector3(0, 0, 0);\r
-\r
- // Get the bounding springs and add them to the needed springs\r
- // TODO: optimize\r
-\r
- const numPointsX = this.numPointsWidth;\r
- const numPointsY = this.numPointsHeight;\r
- const numFacesX = numPointsX - 1;\r
- const numFacesY = numPointsY - 1;\r
-\r
- function getFaceIndex(x, y) {\r
- return y * numFacesX + x;\r
- }\r
-\r
- let indexX = vertexIndex % numPointsX;\r
- let indexY = Math.floor(vertexIndex / numPointsX);\r
-\r
- let springs = [];\r
-\r
- // 0 oben\r
- // 1 links\r
- // 2 oben links -> unten rechts diagonal\r
- // 3 oben rechts -> unten links diagonal\r
- // 4 rechts\r
- // 5 unten\r
-\r
- let ul = indexX > 0 && indexY < numPointsY - 1;\r
- let ur = indexX < numPointsX - 1 && indexY < numPointsY - 1;\r
- let ol = indexX > 0 && indexY > 0;\r
- let or = indexX < numPointsX - 1 && indexY > 0;\r
-\r
- if (ul) {\r
- let faceUL = this.faces[getFaceIndex(indexX - 1, indexY)];\r
- springs.push(faceUL.springs[3]);\r
- if (!ol)\r
- springs.push(faceUL.springs[0]);\r
- springs.push(faceUL.springs[4]);\r
- }\r
- if (ur) {\r
- let faceUR = this.faces[getFaceIndex(indexX, indexY)];\r
- springs.push(faceUR.springs[2]);\r
- if (!or)\r
- springs.push(faceUR.springs[0]);\r
- if (!ul)\r
- springs.push(faceUR.springs[1]);\r
- }\r
- if (ol) {\r
- let faceOL = this.faces[getFaceIndex(indexX - 1, indexY - 1)];\r
- springs.push(faceOL.springs[2]);\r
- springs.push(faceOL.springs[4]);\r
- springs.push(faceOL.springs[5]);\r