2 * Convenience Function for calculating the distance between two vectors
\r
3 * because THREE JS Vector functions mutate variables
\r
4 * @param {Vector3} a - Vector A
\r
5 * @param {Vector3} b - Vector B
\r
7 function vectorLength(a, b) {
\r
8 let v1 = new THREE.Vector3();
\r
9 v1.set(a.x, a.y, a.z);
\r
10 let v2 = new THREE.Vector3();
\r
11 v2.set(b.x, b.y, b.z);
\r
13 return v1.sub(v2).length();
\r
17 * Class representing a quad face
\r
18 * Each face consists of two triangular mesh faces
\r
19 * containts four indices for determining vertices
\r
20 * and six springs, one between each of the vertices
\r
30 constructor(a, b, c, d) {
\r
39 * Class representing a single spring
\r
40 * has a current and resting length
\r
41 * and indices to the two connected vertices
\r
43 export class Spring {
\r
51 * set vertex indices
\r
52 * and calculate inital length based on the
\r
54 * @param {Array<Vector3>} vertices
\r
55 * @param {number} index1
\r
56 * @param {number} index2
\r
58 constructor(vertices, index1, index2) {
\r
59 this.index1 = index1;
\r
60 this.index2 = index2;
\r
62 let length = vectorLength(vertices[index1], vertices[index2]);
\r
63 this.restLength = length;
\r
64 this.currentLength = length;
\r
67 getDirection(vertices) {
\r
68 let direction = new THREE.Vector3(
\r
69 vertices[this.index1].x,
\r
70 vertices[this.index1].y,
\r
71 vertices[this.index1].z
\r
74 direction.sub(vertices[this.index2]);
\r
75 direction.divideScalar(vectorLength(vertices[this.index1], vertices[this.index2]));
\r
81 let length = vectorLength(vertices[this.index1], vertices[this.index2]);
\r
82 this.currentLength = length;
\r
87 * Class representing a single piece of cloth
\r
88 * contains THREE JS geometry,
\r
89 * logically represented by an array of adjacent faces
\r
90 * and vertex weights which are accessed by the same
\r
91 * indices as the vertices in the Mesh
\r
93 export class Cloth {
\r
96 geometry = new THREE.Geometry();
\r
100 vertexWeights = [];
\r
102 vertexRigidness = [];
\r
105 * creates a rectangular piece of cloth
\r
106 * takes the size of the cloth
\r
107 * and the number of vertices it should be composed of
\r
108 * @param {number} width - width of the cloth
\r
109 * @param {number} height - height of the cloth
\r
110 * @param {number} numPointsWidth - number of vertices in horizontal direction
\r
111 * @param {number} numPointsHeight - number of vertices in vertical direction
\r
113 createBasic(width, height, numPointsWidth, numPointsHeight) {
\r
114 /** resulting vertices and faces */
\r
119 * distance between two vertices horizontally/vertically
\r
120 * divide by the number of points minus one
\r
121 * because there are (n - 1) lines between n vertices
\r
123 let stepWidth = width / (numPointsWidth - 1);
\r
124 let stepHeight = height / (numPointsHeight - 1);
\r
127 * iterate over the number of vertices in x/y axis
\r
128 * and add a new Vector3 to "vertices"
\r
130 for (let y = 0; y < numPointsHeight; y++) {
\r
131 for (let x = 0; x < numPointsWidth; x++) {
\r
133 new THREE.Vector3(x * stepWidth, height - y * stepHeight, 0)
\r
139 * helper function to calculate index of vertex
\r
140 * in "vertices" array based on its x and y positions
\r
142 * @param {number} x - x index of vertex
\r
143 * @param {number} y - y index of vertex
\r
145 function getVertexIndex(x, y) {
\r
146 return y * numPointsWidth + x;
\r
150 * generate faces based on 4 vertices
\r
151 * and 6 springs each
\r
153 for (let y = 0; y < numPointsHeight - 1; y++) {
\r
154 for (let x = 0; x < numPointsWidth - 1; x++) {
\r
155 let newFace = new Face(
\r
156 getVertexIndex(x, y),
\r
157 getVertexIndex(x, y + 1),
\r
158 getVertexIndex(x + 1, y),
\r
159 getVertexIndex(x + 1, y + 1),
\r
162 newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y)));
\r
163 newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x, y + 1)));
\r
164 newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y + 1)));
\r
165 newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x, y + 1)));
\r
166 newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x + 1, y + 1)));
\r
167 newFace.springs.push(new Spring(vertices, getVertexIndex(x, y + 1), getVertexIndex(x + 1, y + 1)));
\r
169 faces.push(newFace);
\r
174 * call createExplicit
\r
175 * with generated vertices and faces
\r
177 this.createExplicit(vertices, faces);
\r
180 * hand cloth from left and right upper corners
\r
182 this.vertexRigidness[0] = true;
\r
183 this.vertexRigidness[numPointsWidth-1] = true;
\r
187 * Generate THREE JS Geometry
\r
188 * (list of vertices and list of indices representing triangles)
\r
189 * and calculate the weight of each face and split it between
\r
190 * surrounding vertices
\r
191 * @param {Array<Vector3>} vertices
\r
192 * @param {Array<Face>} faces
\r
194 createExplicit(vertices, faces) {
\r
197 * Copy vertices and initialize vertex weights to 0
\r
199 for (let i in vertices) {
\r
200 this.geometry.vertices.push(vertices[i]);
\r
201 this.previousPositions.push(vertices[i]);
\r
202 this.vertexWeights.push(0);
\r
203 this.vertexRigidness.push(false);
\r
207 * generate two triangles per face,
\r
208 * calculate weight of face as its area
\r
209 * and split between the 4 vertices
\r
211 for (let i in faces) {
\r
212 let face = faces[i];
\r
214 /** copy faces to class member */
\r
215 this.faces.push(face);
\r
217 /** generate triangles */
\r
218 this.geometry.faces.push(new THREE.Face3(
\r
219 face.a, face.b, face.c
\r
221 this.geometry.faces.push(new THREE.Face3(
\r
222 face.c, face.b, face.d
\r
226 * calculate area of face as combined area of
\r
227 * its two composing triangles
\r
229 let xLength = vectorLength(this.geometry.vertices[face.b], this.geometry.vertices[face.a]);
\r
230 let yLength = vectorLength(this.geometry.vertices[face.c], this.geometry.vertices[face.a]);
\r
231 let weight = xLength * yLength / 2;
\r
233 xLength = vectorLength(this.geometry.vertices[face.b], this.geometry.vertices[face.d]);
\r
234 yLength = vectorLength(this.geometry.vertices[face.c], this.geometry.vertices[face.d]);
\r
235 weight += xLength * yLength / 2;
\r
238 * split weight equally between four surrounding vertices
\r
240 this.vertexWeights[face.a] += weight / 4;
\r
241 this.vertexWeights[face.b] += weight / 4;
\r
242 this.vertexWeights[face.c] += weight / 4;
\r
243 this.vertexWeights[face.d] += weight / 4;
\r
247 * let THREE JS compute bounding sphere around generated mesh
\r
248 * needed for View Frustum Culling internally
\r
250 this.geometry.computeBoundingSphere();
\r
254 * generate a debug mesh for visualizing
\r
255 * vertices and springs of the cloth
\r
256 * and add it to scene for rendering
\r
257 * @param {Scene} scene - Scene to add Debug Mesh to
\r
259 createDebugMesh(scene) {
\r
261 * helper function to generate a single line
\r
262 * between two Vertices with a given color
\r
263 * @param {Vector3} from
\r
264 * @param {Vector3} to
\r
265 * @param {number} color
\r
267 function addLine(from, to, color) {
\r
268 let geometry = new THREE.Geometry();
\r
269 geometry.vertices.push(from);
\r
270 geometry.vertices.push(to);
\r
271 let material = new THREE.LineBasicMaterial({ color: color, linewidth: 10 });
\r
272 let line = new THREE.Line(geometry, material);
\r
273 line.renderOrder = 1;
\r
277 * helper function to generate a small sphere
\r
278 * at a given Vertex Position with color
\r
279 * @param {Vector3} point
\r
280 * @param {number} color
\r
282 function addPoint(point, color) {
\r
283 const geometry = new THREE.SphereGeometry(0.05, 32, 32);
\r
284 const material = new THREE.MeshBasicMaterial({ color: color });
\r
285 const sphere = new THREE.Mesh(geometry, material);
\r
286 sphere.position.set(point.x, point.y, point.z);
\r
290 let lineColor = 0x000000;
\r
291 let pointColor = 0xff00000;
\r
294 * generate one line for each of the 6 springs
\r
295 * and one point for each of the 4 vertices
\r
296 * for all of the faces
\r
298 for (let i in this.faces) {
\r
299 let face = this.faces[i];
\r
300 addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.b], lineColor);
\r
301 addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.c], lineColor);
\r
302 addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.d], lineColor);
\r
303 addLine(this.geometry.vertices[face.b], this.geometry.vertices[face.c], lineColor);
\r
304 addLine(this.geometry.vertices[face.b], this.geometry.vertices[face.d], lineColor);
\r
305 addLine(this.geometry.vertices[face.c], this.geometry.vertices[face.d], lineColor);
\r
307 addPoint(this.geometry.vertices[face.a], pointColor);
\r
308 addPoint(this.geometry.vertices[face.b], pointColor);
\r
309 addPoint(this.geometry.vertices[face.c], pointColor);
\r
310 addPoint(this.geometry.vertices[face.d], pointColor);
\r
314 previousPositions = [];
\r
318 * @param {number} dt
\r
321 for (let i in this.geometry.vertices) {
\r
322 let currentPosition;
\r
323 let acceleration = this.getAcceleration(i, dt);
\r
325 // TODO: decide on clamping
\r
326 acceleration.clampLength(0, 100);
\r
328 currentPosition = this.verlet(this.geometry.vertices[i], this.previousPositions[i], acceleration, dt/500);
\r
329 //currentPosition = this.euler(this.geometry.vertices[i], acceleration, dt/10);
\r
331 this.previousPositions[i] = currentPosition;
\r
332 this.geometry.vertices[i] = currentPosition;
\r
335 //this.getAcceleration(1, dt, true);
\r
339 for (let face of this.faces) {
\r
340 for (let spring of face.springs) {
\r
341 spring.update(this.geometry.vertices);
\r
346 * let THREE JS compute bounding sphere around generated mesh
\r
347 * needed for View Frustum Culling internally
\r
350 this.geometry.verticesNeedUpdate = true;
\r
351 this.geometry.elementsNeedUpdate = true;
\r
352 this.geometry.computeBoundingSphere();
\r
359 * Equation of motion for each vertex which represents the acceleration
\r
360 * @param {number} vertexIndex The index of the current vertex whose acceleration should be calculated
\r
361 * @param {number} dt The time passed since last frame
\r
363 getAcceleration(vertexIndex, dt) {
\r
364 if (this.vertexRigidness[vertexIndex])
\r
365 return new THREE.Vector3(0, 0, 0);
\r
367 let vertex = this.geometry.vertices[vertexIndex];
\r
370 let M = this.vertexWeights[vertexIndex];
\r
371 // constant gravity
\r
372 let g = new THREE.Vector3(0, -9.8, 0);
\r
377 // TODO: include wind vector
\r
378 let fWind = new THREE.Vector3(
\r
379 Math.sin(vertex.x * vertex.y * this.time),
\r
380 Math.cos(vertex.z* this.time),
\r
381 Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))
\r
383 fWind = new THREE.Vector3(0, 0, 0);
\r
386 * constant determined by the properties of the surrounding fluids (air)
\r
387 * achievement of cloth effects through try out
\r
391 let velocity = new THREE.Vector3(
\r
392 (vertex.x - this.previousPositions[vertexIndex].x) * dt,
\r
393 (vertex.y - this.previousPositions[vertexIndex].y) * dt,
\r
394 (vertex.z - this.previousPositions[vertexIndex].z) * dt
\r
397 // TODO: include air resistance
\r
398 let fAirResistance = velocity.multiply(velocity).multiplyScalar(-a);
\r
399 fAirResistance = new THREE.Vector3(0, 0, 0);
\r
401 let springSum = new THREE.Vector3(0, 0, 0);
\r
403 // Get the bounding springs and add them to the needed springs
\r
405 for (let i in this.faces) {
\r
406 if (this.faces[i].a == vertexIndex || this.faces[i].b == vertexIndex || this.faces[i].c == vertexIndex || this.faces[i].d == vertexIndex) {
\r
407 for (let j in this.faces[i].springs) {
\r
408 if (this.faces[i].springs[j].index1 == vertexIndex || this.faces[i].springs[j].index2 == vertexIndex) {
\r
410 let spring = this.faces[i].springs[j];
\r
411 let springDirection = spring.getDirection(this.geometry.vertices);
\r
414 if (this.faces[i].springs[j].index2 == vertexIndex)
\r
415 springDirection.multiplyScalar(-1);
\r
417 springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength)));
\r
423 let result = new THREE.Vector3(1, 1, 1);
\r
425 result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).sub(springSum);
\r
431 * The Verlet algorithm as an integrator
\r
432 * to get the next position of a vertex
\r
433 * @param {Vector3} currentPosition
\r
434 * @param {Vector3} previousPosition
\r
435 * @param {Vector3} acceleration
\r
436 * @param {number} passedTime The delta time since last frame
\r
438 verlet(currentPosition, previousPosition, acceleration, passedTime) {
\r
439 // verlet algorithm
\r
440 // next position = 2 * current Position - previous position + acceleration * (passed time)^2
\r
441 // acceleration (dv/dt) = F(net)
\r
442 // Dependency for one vertex: gravity, fluids/air, springs
\r
444 let nextPosition = new THREE.Vector3(
\r
445 (2 * currentPosition.x) - previousPosition.x + acceleration.x * (passedTime * passedTime),
\r
446 (2 * currentPosition.y) - previousPosition.y + acceleration.y * (passedTime * passedTime),
\r
447 (2 * currentPosition.z) - previousPosition.z + acceleration.z * (passedTime * passedTime),
\r
450 return nextPosition;
\r
453 euler(currentPosition, acceleration, passedTime) {
\r
454 let nextPosition = new THREE.Vector3(
\r
455 currentPosition.x + acceleration.x * passedTime,
\r
456 currentPosition.y + acceleration.y * passedTime,
\r
457 currentPosition.z + acceleration.z * passedTime,
\r
460 return nextPosition;
\r