-function init() {\r
- class Point {\r
- constructor(x, y) {\r
- this.x = x;\r
- this.y = y;\r
- }\r
-\r
- add(that) {\r
- return new Point(\r
- this.x + that.x,\r
- this.y + that.y\r
- );\r
- }\r
-\r
- sub(that) {\r
- return new Point(\r
- this.x - that.x,\r
- this.y - that.y\r
- );\r
- }\r
-\r
- dist(that) {\r
- let a = this.x - that.x;\r
- let b = this.y - that.y;\r
- return Math.sqrt(a * a + b * b)\r
- }\r
- }\r
-\r
- function vectorLength(a, b) {\r
- let v1 = new THREE.Vector3();\r
- v1.set(a.x, a.y, a.z);\r
- let v2 = new THREE.Vector3();\r
- v2.set(b.x, b.y, b.z);\r
-\r
- return v1.sub(v2).length();\r
- }\r
-\r
- class Face {\r
- a;\r
- b;\r
- c;\r
- d;\r
-\r
- springs = [];\r
-\r
- constructor(a, b, c, d) {\r
- this.a = a;\r
- this.b = b;\r
- this.c = c;\r
- this.d = d;\r
- }\r
- }\r
-\r
- class Spring {\r
- restLength;\r
- currentLength;\r
- index1;\r
- index2;\r
-\r
- constructor(vertices, index1, index2) {\r
- this.index1 = index1;\r
- this.index2 = index2;\r
-\r
- let length = vectorLength(vertices[index1], vertices[index2]);\r
- this.restLength = length;\r
- this.currentLength = length;\r
- }\r
- }\r
-\r
- class Cloth {\r
- VertexWeight = 1;\r
-\r
- geometry = new THREE.Geometry();\r
-\r
- faces = [];\r
-\r
- vertexWeights = [];\r
-\r
- createBasic(width, height, numPointsWidth, numPointsHeight) {\r
- let vertices = [];\r
- let faces = [];\r
-\r
- let stepWidth = width / (numPointsWidth - 1);\r
- let stepHeight = height / (numPointsHeight - 1);\r
-\r
- for (let y = 0; y < numPointsHeight; y++) {\r
- for (let x = 0; x < numPointsWidth; x++) {\r
- vertices.push(\r
- new THREE.Vector3(x * stepWidth, height - y * stepHeight, 0)\r
- );\r
- }\r
- }\r
-\r
- function getVertexIndex(x, y) {\r
- return y * numPointsWidth + x;\r
- }\r
- \r
- for (let y = 0; y < numPointsHeight - 1; y++) {\r
- for (let x = 0; x < numPointsWidth - 1; x++) {\r
- let newFace = new Face(\r
- getVertexIndex(x, y),\r
- getVertexIndex(x, y + 1),\r
- getVertexIndex(x + 1, y),\r
- getVertexIndex(x + 1, y + 1),\r
- );\r
-\r
- newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y)));\r
- newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x, y + 1)));\r
- newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y + 1)));\r
- newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x, y + 1)));\r
- newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x + 1, y + 1)));\r
- newFace.springs.push(new Spring(vertices, getVertexIndex(x, y + 1), getVertexIndex(x + 1, y + 1)));\r
- \r
- faces.push(newFace);\r
- }\r
- }\r
-\r
- this.createExplicit(vertices, faces);\r
- }\r
-\r
- createExplicit(vertices, faces) {\r
- for (let i in vertices) {\r
- this.geometry.vertices.push(vertices[i]);\r
- this.vertexWeights.push(0);\r
- }\r
- for (let i in faces) {\r
- let face = faces[i];\r
-\r
- this.faces.push(face);\r
-\r
- this.geometry.faces.push(new THREE.Face3(\r
- face.a, face.b, face.c\r
- ));\r
- this.geometry.faces.push(new THREE.Face3(\r
- face.c, face.b, face.d\r
- ));\r
- \r
- let xLength = vectorLength(this.geometry.vertices[face.b], this.geometry.vertices[face.a]);\r
- let yLength = vectorLength(this.geometry.vertices[face.c], this.geometry.vertices[face.a]);\r
- let weight = xLength * yLength / 2;\r
-\r
- xLength = vectorLength(this.geometry.vertices[face.b], this.geometry.vertices[face.d]);\r
- yLength = vectorLength(this.geometry.vertices[face.c], this.geometry.vertices[face.d]);\r
- weight += xLength * yLength / 2;\r
-\r
- this.vertexWeights[face.a] += weight / 4;\r
- this.vertexWeights[face.b] += weight / 4;\r
- this.vertexWeights[face.c] += weight / 4;\r
- this.vertexWeights[face.d] += weight / 4;\r
- }\r
-\r
- this.geometry.computeBoundingSphere();\r
- }\r
-\r
- createDebugMesh(scene) {\r
- function addLine(from, to, color) {\r
- let geometry = new THREE.Geometry();\r
- geometry.vertices.push(from);\r
- geometry.vertices.push(to);\r
- let material = new THREE.LineBasicMaterial( { color: color, linewidth: 10 } );\r
- let line = new THREE.Line(geometry, material);\r
- line.renderOrder = 1;\r
- scene.add(line);\r
- }\r
- function addPoint(point, color) {\r
- const geometry = new THREE.SphereGeometry( 0.05, 32, 32 );\r
- const material = new THREE.MeshBasicMaterial( { color: color } );\r
- const sphere = new THREE.Mesh( geometry, material );\r
- sphere.position.set(point.x, point.y, point.z);\r
- scene.add( sphere );\r
- }\r
-\r
- let lineColor = 0x000000;\r
- let pointColor = 0xff00000;\r
-\r
- for (let i in this.faces) {\r
- let face = this.faces[i];\r
- addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.b], lineColor);\r
- addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.c], lineColor);\r
- addLine(this.geometry.vertices[face.a], this.geometry.vertices[face.d], lineColor);\r
- addLine(this.geometry.vertices[face.b], this.geometry.vertices[face.c], lineColor);\r
- addLine(this.geometry.vertices[face.b], this.geometry.vertices[face.d], lineColor);\r
- addLine(this.geometry.vertices[face.c], this.geometry.vertices[face.d], lineColor);\r
-\r
- addPoint(this.geometry.vertices[face.a], pointColor);\r
- addPoint(this.geometry.vertices[face.b], pointColor);\r
- addPoint(this.geometry.vertices[face.c], pointColor);\r
- addPoint(this.geometry.vertices[face.d], pointColor);\r
- }\r
- }\r
- }\r
-\r
- let mousePos = new Point();\r
-\r
- const canvasSpace = 200;\r
-\r