]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/main.js
Code refactoring
[cloth_sim] / Scripts / main.js
index 5f6595ee0510d859b97ff1f313109f0d0536ef25..01e3283236abcfab54ad73cb2efd67b162ede951 100644 (file)
@@ -1,12 +1,22 @@
 import { Face, Spring, Cloth } from './cloth.js';\r
 \r
+function addLights(scene){\r
+  \r
+  scene.add( new THREE.AmbientLight( 0x222222 ) );\r
 \r
-/**\r
- * rendering\r
- * Einheiten konsistent\r
- * Wind\r
- * evtl. an Stoff ziehen\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
@@ -27,7 +37,7 @@ function setup_scene(canvasSpace) {
   /** position camera */\r
   camera.position.y = 5;\r
   camera.position.z = 10;\r
-\r
+  addLights(scene);\r
   return [scene, camera, renderer];\r
 }\r
 \r
@@ -55,27 +65,13 @@ function init() {
   cloth.createBasic(10, 10, 10, 10);\r
   //cloth.createDebugMesh(scene);\r
 \r
-  //const material = new THREE.MeshBasicMaterial({ color: 0x0000ff, side: THREE.DoubleSide });\r
+\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
-  //line.material.depthTest = false;\r
-  //line.material.opacity = 0.25;\r
-  //line.material.transparent = true;\r
+\r
   scene.add(mesh);\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
-  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, 50 );\r
-  light3.position.set( 0, -1, 40 );\r
-  scene.add( light3 );\r
   \r
   let raycaster = new THREE.Raycaster();\r
   let intersects;\r
@@ -129,6 +125,33 @@ function init() {
     mousePos.x = evt.clientX;\r
     mousePos.y = evt.clientY;\r
 \r
+    cloth.mouseMove(calculateMousePosToWorld(evt));\r
+  };\r
+\r
+  /**\r
+   * Prevent context menu while blowing wind\r
+   */\r
+  canvas.addEventListener('contextmenu', function(evt) { \r
+    evt.preventDefault();\r
+  }, false);\r
+\r
+\r
+  canvas.onmousedown = (evt) => {\r
+\r
+    // Check mouse click\r
+    rightMousePressed = evt.button == 2;\r
+    \r
+    if(intersects.length > 0 && evt.button == 0){\r
+      cloth.mousePress(intersects);\r
+    } \r
+  }\r
+  \r
+  canvas.onmouseup = (evt) => {\r
+    cloth.mouseRelease();\r
+    rightMousePressed = false;\r
+  }\r
+\r
+  function calculateMousePosToWorld(evt){\r
     var vec = new THREE.Vector3(); // create once and reuse\r
     var pos = new THREE.Vector3(); // create once and reuse\r
 \r
@@ -144,20 +167,6 @@ function init() {
     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
-    rightMousePressed = evt.button == 2;\r
-    \r
-    if(intersects.length > 0 && evt.button == 0){\r
-      cloth.mousePress(intersects);\r
-    } \r
-  }\r
-  \r
-  canvas.onmouseup = (evt) => {\r
-    cloth.mouseRelease();\r
-    rightMousePressed = false;\r
+    return pos;\r
   }\r
 }
\ No newline at end of file