]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/main.js
comments
[cloth_sim] / Scripts / main.js
index 51d676254df65d909c4d90deb63df3101a72ddb4..1a30f7a9db2cc3c240631e90e03d8505e0a00c15 100644 (file)
@@ -82,13 +82,15 @@ function init() {
   const loader = new THREE.TextureLoader();\r
   //Red color: 0xC70039\r
 \r
+  /** Create cloth and generate mesh */\r
   const cloth = new Cloth(1, 0.5, 20, 20);\r
   const clothGeometry = cloth.generateGeometry();\r
-  const clothMaterial = new THREE.MeshStandardMaterial({ map: loader.load('Textures/hsrm2.png'), color: {type: "c", value: new THREE.Color(0xffffff)}, side: THREE.DoubleSide, flatShading: false, transparent: true });\r
+  const clothMaterial = new THREE.MeshStandardMaterial({ map: loader.load('Textures/hsrm2.png'), color: 0xffffff, side: THREE.DoubleSide, flatShading: false });\r
   //const clothMaterial = new THREE.MeshStandardMaterial({ color: 0xC70039, side: THREE.DoubleSide, flatShading: false });\r
   const clothMesh = new THREE.Mesh(clothGeometry, clothMaterial);\r
   scene.add(clothMesh);\r
   \r
+  /** Register wind checkbox */\r
   document.getElementById("windToggle").checked = options.wind;\r
   document.getElementById("windToggle").onchange = (e) => {\r
     options.wind = e.target.checked;\r
@@ -104,25 +106,31 @@ function init() {
    * @param {number} dt - time passed since last frame in ms\r
    */\r
   function animate(dt) {\r
+    /** run simulation step */\r
     cloth.simulate(dt / 1000);\r
 \r
     cloth.updateGeometry(clothGeometry);\r
-    \r
-    raycaster.setFromCamera(new THREE.Vector2((mousePos.x / w) * 2 - 1, ((h - mousePos.y) / h) * 2 - 1), camera);\r
 \r
+    /** raycast from camera to cursor */\r
+    raycaster.setFromCamera(new THREE.Vector2((mousePos.x / w) * 2 - 1, ((h - mousePos.y) / h) * 2 - 1), camera);\r
     intersects = raycaster.intersectObject(clothMesh);\r
 \r
+    /** provide user interaction */\r
     if (intersects.length > 0) {\r
       if (windKeyDown)\r
         cloth.blow(camera.position, intersects);\r
+      /** "grab" vertex index */\r
       if (dragKeyDown && draggedIndex == -1)\r
         draggedIndex = intersects[0].face.a;\r
     }\r
     if (dragKeyDown && draggedIndex != -1)\r
       cloth.drag(calculateMousePosToWorld(mousePos), draggedIndex);\r
+\r
+    /** queue next frame */\r
     setTimeout(() => {\r
       animate(frameTime);\r
     }, frameTime);\r
+\r
     renderer.render(scene, camera);\r
   }\r
 \r
@@ -163,6 +171,7 @@ function init() {
     evt.preventDefault();\r
   }, false);\r
 \r
+  /** register key events */\r
   document.onkeydown = (evt) => {\r
     if (evt.code === "KeyW")\r
       windKeyDown = true;\r
@@ -179,6 +188,7 @@ function init() {
     }\r
   };\r
 \r
+  /** helper function to turn mouse position into 3D coordinates */\r
   function calculateMousePosToWorld(mousePos){\r
     var vec = new THREE.Vector3(); // create once and reuse\r
     var pos = new THREE.Vector3(); // create once and reuse\r