X-Git-Url: https://gitweb.ps.run/cloth_sim/blobdiff_plain/8bf51c36d322cb8d4f099d3475563ee4e520ab92..eac8eb4ec5115f16fc17b94fb194fdc1c005ddb5:/Scripts/main.js diff --git a/Scripts/main.js b/Scripts/main.js index 5e68a4f..1a30f7a 100644 --- a/Scripts/main.js +++ b/Scripts/main.js @@ -1,19 +1,19 @@ function addLights(scene){ - scene.add( new THREE.AmbientLight( 0x222222 ) ); + scene.add(new THREE.AmbientLight(0x222222)); - const light1 = new THREE.PointLight( 0xffffff, 1, 50 ); - light1.position.set( 15, 1, 40 ); - scene.add( light1 ); + const light1 = new THREE.PointLight(0xffffff, 1, 50); + light1.position.set(15, 1, 40); + scene.add(light1); - const light2 = new THREE.PointLight( 0xffffff, 1, 50 ); - light2.position.set( -15, 0, 40 ); - scene.add( light2 ); + const light2 = new THREE.PointLight(0xffffff, 1, 50); + light2.position.set(-15, 0, 40); + scene.add(light2); - const light3 = new THREE.PointLight( 0xffffff, 1, 50 ); - light3.position.set( 0, -1, 40 ); - scene.add( light3 ); + const light3 = new THREE.PointLight(0xffffff, 1, 50); + light3.position.set(0, -1, 40); + scene.add(light3); } /** @@ -45,11 +45,11 @@ function setup_scene(canvasSpace) { }); /** add flag pole */ - const geometry = new THREE.CylinderGeometry( 0.02, 0.02, 5, 32 ); - const material = new THREE.MeshStandardMaterial( {color: 0xffffff} ); - const cylinder = new THREE.Mesh( geometry, material ); + const geometry = new THREE.CylinderGeometry(0.02, 0.02, 5, 32); + const material = new THREE.MeshStandardMaterial({color: 0xffffff}); + const cylinder = new THREE.Mesh(geometry, material); cylinder.position.set(-0.5, -2.25, 0); - scene.add( cylinder ); + scene.add(cylinder); /** add global light */ const directionalLight = new THREE.DirectionalLight(0xffffff, 1); @@ -82,13 +82,15 @@ function init() { const loader = new THREE.TextureLoader(); //Red color: 0xC70039 + /** Create cloth and generate mesh */ const cloth = new Cloth(1, 0.5, 20, 20); const clothGeometry = cloth.generateGeometry(); - const clothMaterial = new THREE.MeshStandardMaterial({ map: loader.load('Textures/hsrm.jpg'), color: 0xffffff, side: THREE.DoubleSide, flatShading: false}); + const clothMaterial = new THREE.MeshStandardMaterial({ map: loader.load('Textures/hsrm2.png'), color: 0xffffff, side: THREE.DoubleSide, flatShading: false }); //const clothMaterial = new THREE.MeshStandardMaterial({ color: 0xC70039, side: THREE.DoubleSide, flatShading: false }); const clothMesh = new THREE.Mesh(clothGeometry, clothMaterial); scene.add(clothMesh); + /** Register wind checkbox */ document.getElementById("windToggle").checked = options.wind; document.getElementById("windToggle").onchange = (e) => { options.wind = e.target.checked; @@ -96,26 +98,39 @@ function init() { let raycaster = new THREE.Raycaster(); let intersects; - let rightMousePressed; + let windKeyDown = false; + let dragKeyDown = false; + let draggedIndex = -1; /** * function called every frame * @param {number} dt - time passed since last frame in ms */ function animate(dt) { + /** run simulation step */ cloth.simulate(dt / 1000); cloth.updateGeometry(clothGeometry); - - raycaster.setFromCamera( new THREE.Vector2((mousePos.x / w) * 2 - 1, ((h - mousePos.y) / h) * 2 - 1), camera ); - // intersects = raycaster.intersectObject( mesh ); - - // if ( intersects.length > 0 && rightMousePressed) { - // // Cloth mouse interaction - // } + /** raycast from camera to cursor */ + raycaster.setFromCamera(new THREE.Vector2((mousePos.x / w) * 2 - 1, ((h - mousePos.y) / h) * 2 - 1), camera); + intersects = raycaster.intersectObject(clothMesh); + + /** provide user interaction */ + if (intersects.length > 0) { + if (windKeyDown) + cloth.blow(camera.position, intersects); + /** "grab" vertex index */ + if (dragKeyDown && draggedIndex == -1) + draggedIndex = intersects[0].face.a; + } + if (dragKeyDown && draggedIndex != -1) + cloth.drag(calculateMousePosToWorld(mousePos), draggedIndex); + + /** queue next frame */ setTimeout(() => { animate(frameTime); }, frameTime); + renderer.render(scene, camera); } @@ -138,7 +153,7 @@ function init() { * start rendering */ if (canvas.getContext) { - animate(performance.now()); + animate(frameTime); } @@ -147,8 +162,6 @@ function init() { canvas.onmousemove = (evt) => { mousePos.x = evt.clientX; mousePos.y = evt.clientY; - - //cloth.mouseMove(calculateMousePosToWorld(evt)); }; /** @@ -158,38 +171,40 @@ function init() { evt.preventDefault(); }, false); + /** register key events */ + document.onkeydown = (evt) => { + if (evt.code === "KeyW") + windKeyDown = true; + if (evt.code === "KeyD") + dragKeyDown = true; + }; - canvas.onmousedown = (evt) => { - - // Check mouse click - rightMousePressed = evt.button == 2; - - if(intersects.length > 0 && evt.button == 0){ - //cloth.mousePress(intersects); - } - } - - canvas.onmouseup = (evt) => { - //cloth.mouseRelease(); - rightMousePressed = false; - } + document.onkeyup = (evt) => { + if (evt.code === "KeyW") + windKeyDown = false; + if (evt.code === "KeyD") { + dragKeyDown = false; + draggedIndex = -1; + } + }; - function calculateMousePosToWorld(evt){ + /** helper function to turn mouse position into 3D coordinates */ + function calculateMousePosToWorld(mousePos){ var vec = new THREE.Vector3(); // create once and reuse var pos = new THREE.Vector3(); // create once and reuse vec.set( - ( evt.clientX / window.innerWidth ) * 2 - 1, - - ( evt.clientY / window.innerHeight ) * 2 + 1, - 0.5 ); + (mousePos.x / window.innerWidth) * 2 - 1, + - (mousePos.y / window.innerHeight) * 2 + 1, + 0.5); - vec.unproject( camera ); + vec.unproject(camera); - vec.sub( camera.position ).normalize(); + vec.sub(camera.position).normalize(); var distance = - camera.position.z / vec.z; - pos.copy( camera.position ).add( vec.multiplyScalar( distance ) ); + pos.copy(camera.position).add(vec.multiplyScalar(distance)); return pos; } } \ No newline at end of file