]> gitweb.ps.run Git - cloth_sim/blob - Scripts/cloth.js
render cloth
[cloth_sim] / Scripts / cloth.js
1 // cloth rendering\r
2 // simulate\r
3 // setup scene\r
4 \r
5 const MASS = 0.1;\r
6 \r
7 class Particle {\r
8   constructor(x, y, z, mass) {\r
9     this.position = new THREE.Vector3(x, y, z);\r
10     this.previous = new THREE.Vector3(x, y, z);\r
11     this.acceleration = new THREE.Vector3(0, 0, 0);\r
12     this.mass = mass;\r
13   }\r
14   addForce(force) {\r
15 \r
16   }\r
17   verlet(dt) {\r
18 \r
19   }\r
20 }\r
21 \r
22 class Cloth {\r
23   constructor(width, height, numPointsWidth, numPointsHeight) {\r
24     this.width = width;\r
25     this.height = height;\r
26     this.numPointsWidth = numPointsWidth;\r
27     this.numPointsHeight = numPointsHeight;\r
28 \r
29     /**\r
30      * distance between two vertices horizontally/vertically\r
31      * divide by the number of points minus one\r
32      * because there are (n - 1) lines between n vertices\r
33      */\r
34     let stepWidth = width / (numPointsWidth - 1);\r
35     let stepHeight = height / (numPointsHeight - 1);\r
36 \r
37     /**\r
38      * iterate over the number of vertices in x/y axis\r
39      * and add a new Particle to "particles"\r
40      */\r
41     this.particles = [];\r
42     for (let y = 0; y < numPointsHeight; y++) {\r
43       for (let x = 0; x < numPointsWidth; x++) {\r
44         this.particles.push(\r
45           new Particle(\r
46             (x - ((numPointsWidth-1)/2)) * stepWidth,\r
47             height - (y + ((numPointsHeight-1)/2)) * stepHeight,\r
48             0,\r
49             MASS)\r
50         );\r
51       }\r
52     }\r
53   }\r
54   generateGeometry() {\r
55     const geometry = new THREE.BufferGeometry();\r
56 \r
57     const vertices = [];\r
58     const normals = [];\r
59     const indices = [];\r
60 \r
61     for (let particle of this.particles) {\r
62       vertices.push(\r
63         particle.position.x,\r
64         particle.position.y,\r
65         particle.position.z);\r
66     }\r
67 \r
68     const numPointsWidth = this.numPointsWidth;\r
69     const numPointsHeight = this.numPointsHeight;\r
70 \r
71     /**\r
72      * helper function to calculate index of vertex\r
73      * in "vertices" array based on its x and y positions\r
74      * in the mesh\r
75      * @param {number} x - x index of vertex\r
76      * @param {number} y - y index of vertex\r
77      */\r
78     function getVertexIndex(x, y) {\r
79       return y * numPointsWidth + x;\r
80     }\r
81 \r
82     /**\r
83      * generate faces based on 4 vertices\r
84      * and 6 springs each\r
85      */\r
86     for (let y = 0; y < numPointsHeight - 1; y++) {\r
87       for (let x = 0; x < numPointsWidth - 1; x++) {\r
88         indices.push(\r
89           getVertexIndex(x, y),\r
90           getVertexIndex(x+1, y),\r
91           getVertexIndex(x+1, y+1)\r
92         );\r
93         indices.push(\r
94           getVertexIndex(x, y),\r
95           getVertexIndex(x+1, y+1),\r
96           getVertexIndex(x, y+1)\r
97         );\r
98       }\r
99     }\r
100 \r
101     geometry.setIndex(indices);\r
102     geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));\r
103     //geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));\r
104     geometry.computeBoundingSphere();\r
105     geometry.computeVertexNormals();\r
106 \r
107     return geometry;\r
108   }\r
109   updateGeometry(geometry) {\r
110 \r
111   }\r
112 }