]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/main.js
Wind Force added, Cloth dragging added, fix vertex normals and change lighting
[cloth_sim] / Scripts / main.js
index 4c57ff972420a68b22f0e7b0d1f3be70a8621939..c26998279b23b5ccc026d64a4c7ca8c756cee3a2 100644 (file)
@@ -52,11 +52,11 @@ function init() {
   \r
   /** setup cloth and generate debug mesh */\r
   let cloth = new Cloth();\r
-  cloth.createBasic(10, 10, 50, 50);\r
+  cloth.createBasic(10, 10, 10, 10);\r
   //cloth.createDebugMesh(scene);\r
 \r
   //const material = new THREE.MeshBasicMaterial({ color: 0x0000ff, side: THREE.DoubleSide });\r
-  const material = new THREE.MeshStandardMaterial({ color: 0x0000ff, side: THREE.DoubleSide, flatShading: false });\r
+  const material = new THREE.MeshStandardMaterial({ color: 0xC70039, side: THREE.DoubleSide, flatShading: false });\r
   const mesh = new THREE.Mesh(cloth.geometry, material);\r
   //const mesh = new THREE.WireframeGeometry(cloth.geometry);\r
   //const line = new THREE.LineSegments(mesh);\r
@@ -67,18 +67,19 @@ function init() {
 \r
   scene.add( new THREE.AmbientLight( 0x222222 ) );\r
 \r
-  const light1 = new THREE.PointLight( 0xffffff, 1, 100 );\r
-  light1.position.set( 2, 1, 80 );\r
+  const light1 = new THREE.PointLight( 0xffffff, 1, 50 );\r
+  light1.position.set( 15, 1, 40 );\r
   scene.add( light1 );\r
-  const light2 = new THREE.PointLight( 0xffffff, 1, 100 );\r
-  light2.position.set( -2, 1, 80 );\r
+  const light2 = new THREE.PointLight( 0xffffff, 1, 50 );\r
+  light2.position.set( -15, 0, 40 );\r
   scene.add( light2 );\r
-  const light3 = new THREE.PointLight( 0xffffff, 1, 100 );\r
-  light3.position.set( 0, -1, 80 );\r
+  const light3 = new THREE.PointLight( 0xffffff, 1, 50 );\r
+  light3.position.set( 0, -1, 40 );\r
   scene.add( light3 );\r
   \r
   let raycaster = new THREE.Raycaster();\r
-\r
+  let intersects;\r
+  let rightMousePressed;\r
   /**\r
    * function called every frame\r
    * @param {number} dt - time passed since last frame in ms\r
@@ -88,7 +89,7 @@ function init() {
 \r
     raycaster.setFromCamera( new THREE.Vector2((mousePos.x / w) * 2 - 1, ((h - mousePos.y) / h) * 2 - 1), camera );\r
 \r
-    const intersects = raycaster.intersectObject( mesh );\r
+    intersects = raycaster.intersectObject( mesh );\r
 \r
     if ( intersects.length > 0 ) {\r
       cloth.wind(intersects);\r
@@ -121,9 +122,39 @@ function init() {
     animate(performance.now());\r
   }\r
 \r
+  \r
+\r
   /** add mouse move callback */\r
   canvas.onmousemove = (evt) => {\r
     mousePos.x = evt.clientX;\r
     mousePos.y = evt.clientY;\r
+\r
+    var vec = new THREE.Vector3(); // create once and reuse\r
+    var pos = new THREE.Vector3(); // create once and reuse\r
+\r
+    vec.set(\r
+      ( evt.clientX / window.innerWidth ) * 2 - 1,\r
+    - ( evt.clientY / window.innerHeight ) * 2 + 1,\r
+      0.5 );\r
+\r
+    vec.unproject( camera );\r
+\r
+    vec.sub( camera.position ).normalize();\r
+\r
+    var distance = - camera.position.z / vec.z;\r
+\r
+    pos.copy( camera.position ).add( vec.multiplyScalar( distance ) );\r
+\r
+    cloth.mouseMove(pos);\r
   };\r
+\r
+  canvas.onmousedown = (evt) => {\r
+    if(intersects.length > 0){\r
+      cloth.mousePress(intersects);\r
+    }\r
+  }\r
+  \r
+  canvas.onmouseup = (evt) => {\r
+    cloth.mouseRelease();\r
+  }\r
 }
\ No newline at end of file