-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
- static 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
- return this.CreateExplicit(vertices, faces);\r
- }\r
-\r
- static CreateExplicit(vertices, faces) {\r
- let result = new Cloth();\r
-\r
- for (let i in vertices) {\r
- result.geometry.vertices.push(vertices[i]);\r
- result.vertexWeights.push(0);\r
- }\r
- console.log(vertices);\r
- for (let i in faces) {\r
- let face = faces[i];\r
-\r
- result.geometry.faces.push(new THREE.Face3(\r
- face.a, face.b, face.c\r
- ));\r
- result.geometry.faces.push(new THREE.Face3(\r
- face.c, face.b, face.d\r
- ));\r
-\r
- console.log(face);\r
- \r
- let xLength = vectorLength(result.geometry.vertices[face.b], result.geometry.vertices[face.a]);\r
- let yLength = vectorLength(result.geometry.vertices[face.c], result.geometry.vertices[face.a]);\r
- let weight = xLength * yLength / 2;\r
-\r
- xLength = vectorLength(result.geometry.vertices[face.b], result.geometry.vertices[face.d]);\r
- yLength = vectorLength(result.geometry.vertices[face.c], result.geometry.vertices[face.d]);\r
- weight += xLength * yLength / 2;\r
-\r
- result.vertexWeights[face.a] += weight / 4;\r
- result.vertexWeights[face.b] += weight / 4;\r
- result.vertexWeights[face.c] += weight / 4;\r
- result.vertexWeights[face.d] += weight / 4;\r
- }\r
-\r
- result.geometry.computeBoundingSphere();\r
-\r
- return result;\r
- }\r
- }\r
-\r
- let mousePos = new Point();\r
-\r
- const canvasSpace = 200;\r
-\r
+import { Face, Spring, Cloth } from './cloth.js';\r
+import { OrbitControls } from './OrbitControls.js';\r
+\r
+function addLights(scene){\r
+ \r
+ scene.add( new THREE.AmbientLight( 0x222222 ) );\r
+\r
+ const light1 = new THREE.PointLight( 0xffffff, 1, 50 );\r
+ light1.position.set( 15, 1, 40 );\r
+ scene.add( light1 );\r
+\r
+ const light2 = new THREE.PointLight( 0xffffff, 1, 50 );\r
+ light2.position.set( -15, 0, 40 );\r
+ scene.add( light2 );\r
+\r
+ const light3 = new THREE.PointLight( 0xffffff, 1, 50 );\r
+ light3.position.set( 0, -1, 40 );\r
+ scene.add( light3 );\r
+ \r
+}\r
+\r
+/**\r
+ * setup THREE JS Scene, Camera and Renderer\r
+ */\r
+function setup_scene(canvasSpace) {\r