]> gitweb.ps.run Git - cloth_sim/blob - Scripts/main.js
Rendering Cloth
[cloth_sim] / Scripts / main.js
1 function init() {\r
2   class Point {\r
3     constructor(x, y) {\r
4       this.x = x;\r
5       this.y = y;\r
6     }\r
7 \r
8     add(that) {\r
9       return new Point(\r
10         this.x + that.x,\r
11         this.y + that.y\r
12       );\r
13     }\r
14 \r
15     sub(that) {\r
16       return new Point(\r
17         this.x - that.x,\r
18         this.y - that.y\r
19       );\r
20     }\r
21 \r
22     dist(that) {\r
23       let a = this.x - that.x;\r
24       let b = this.y - that.y;\r
25       return Math.sqrt(a * a + b * b)\r
26     }\r
27   }\r
28 \r
29   class Cloth {\r
30     geometry = new THREE.Geometry();\r
31 \r
32     \r
33 \r
34     static CreateBasic(width, height, numPointsWidth, numPointsHeight) {\r
35       let vertices = [];\r
36       let faces = [];\r
37 \r
38       let stepWidth = width / (numPointsWidth - 1);\r
39       let stepHeight = height / (numPointsHeight - 1);\r
40 \r
41       for (let y = 0; y < numPointsHeight; y++) {\r
42         for (let x = 0; x < numPointsWidth; x++) {\r
43           vertices.push(\r
44             new THREE.Vector3(x * stepWidth, height - y * stepHeight, 0)\r
45           );\r
46         }\r
47       }\r
48 \r
49       function getVertexIndex(x, y) {\r
50         return y * numPointsWidth + x;\r
51       }\r
52       \r
53       for (let y = 0; y < numPointsHeight - 1; y++) {\r
54         for (let x = 0; x < numPointsWidth - 1; x++) {\r
55           faces.push(\r
56             new THREE.Face3(\r
57               getVertexIndex(x, y),\r
58               getVertexIndex(x, y + 1),\r
59               getVertexIndex(x + 1, y),\r
60             )\r
61           );\r
62           faces.push(\r
63             new THREE.Face3(\r
64               getVertexIndex(x + 1, y),\r
65               getVertexIndex(x, y + 1),\r
66               getVertexIndex(x + 1, y + 1),\r
67             )\r
68           );\r
69         }\r
70       }\r
71 \r
72       return this.CreateExplicit(vertices, faces);\r
73     }\r
74 \r
75     static CreateExplicit(vertices, faces) {\r
76       let result = new Cloth();\r
77 \r
78       for (let i in vertices) {\r
79         result.geometry.vertices.push(vertices[i]);\r
80       }\r
81       for (let i in faces) {\r
82         result.geometry.faces.push(faces[i]);\r
83       }\r
84 \r
85       result.geometry.computeBoundingSphere();\r
86 \r
87       return result;\r
88     }\r
89   }\r
90 \r
91   let mousePos = new Point();\r
92 \r
93   const canvasSpace = 200;\r
94 \r
95   const scene = new THREE.Scene();\r
96   const camera = new THREE.PerspectiveCamera(75, window.innerWidth / (window.innerHeight - canvasSpace), 0.1, 1000);\r
97 \r
98   const renderer = new THREE.WebGLRenderer();\r
99   renderer.setSize(window.innerWidth, window.innerHeight - canvasSpace);\r
100   document.getElementById("threejscontainer").appendChild(renderer.domElement);\r
101 \r
102   const directionalLight = new THREE.DirectionalLight(0xffffff, 1);\r
103   scene.add(directionalLight);\r
104 \r
105   let cloth = Cloth.CreateBasic(10, 10, 4, 4);\r
106   const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });\r
107   const mesh = new THREE.Mesh(cloth.geometry, material);\r
108   scene.add(mesh);\r
109 \r
110   camera.position.y = 5;\r
111   camera.position.z = 10;\r
112 \r
113   function animate(dt) {\r
114     requestAnimationFrame(animate);\r
115     renderer.render(scene, camera);\r
116   }\r
117 \r
118   let canvas = document.getElementsByTagName("canvas")[0];\r
119   let resize = function () {\r
120     w = window.innerWidth;\r
121     h = window.innerHeight - 200;\r
122     canvas.width = w;\r
123     canvas.height = h;\r
124   }\r
125   window.onresize = resize;\r
126   resize();\r
127   if (canvas.getContext) {\r
128     ctx = canvas.getContext('2d');\r
129     animate(performance.now());\r
130   }\r
131   canvas.onmousemove = (evt) => {\r
132     mousePos.x = evt.clientX;\r
133     mousePos.y = evt.clientY;\r
134   };\r
135 }