X-Git-Url: https://gitweb.ps.run/cloth_sim/blobdiff_plain/c2adddfd9ecca5d483cf3147b8bd4870f3ea6150..4079007a1ed290280e228b93432129c4a262dae9:/Scripts/main.js diff --git a/Scripts/main.js b/Scripts/main.js index f395b6c..a0387ac 100644 --- a/Scripts/main.js +++ b/Scripts/main.js @@ -1,68 +1,110 @@ -function init() { - class Point { - constructor(x, y) { - this.x = x; - this.y = y; - } - - add(that) { - return new Point( - this.x + that.x, - this.y + that.y - ); - } - - sub(that) { - return new Point( - this.x - that.x, - this.y - that.y - ); - } - - dist(that) { - let a = this.x - that.x; - let b = this.y - that.y; - return Math.sqrt(a * a + b * b) - } +import { Face, Spring, Cloth } from './cloth.js'; + + +class Point { + constructor(x, y) { + this.x = x; + this.y = y; } - let mousePos = new Point(); + add(that) { + return new Point( + this.x + that.x, + this.y + that.y + ); + } + + sub(that) { + return new Point( + this.x - that.x, + this.y - that.y + ); + } + + dist(that) { + let a = this.x - that.x; + let b = this.y - that.y; + return Math.sqrt(a * a + b * b) + } +} - const scene = new THREE.Scene(); - const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); +/** + * setup THREE JS Scene, Camera and Renderer + */ +function setup_scene(canvasSpace) { + const scene = new THREE.Scene(); + const camera = new THREE.PerspectiveCamera(75, window.innerWidth / (window.innerHeight - canvasSpace), 0.1, 1000); const renderer = new THREE.WebGLRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight - 200); + /** size canvas to leave some space for UI */ + renderer.setSize(window.innerWidth, window.innerHeight - canvasSpace); + /** embed canvas in HTML */ document.getElementById("threejscontainer").appendChild(renderer.domElement); + /** add global light */ const directionalLight = new THREE.DirectionalLight(0xffffff, 1); scene.add(directionalLight); - const geometry = new THREE.BoxGeometry(); - const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); - const cube = new THREE.Mesh(geometry, material); - scene.add(cube); + /** position camera */ + camera.position.y = 5; + camera.position.z = 10; + + return [scene, camera, renderer]; +} + +/** call "init" when document is fully loaded */ +document.body.onload = init; + +function init() { + let mousePos = new Point(); + + /** + * Space left empty under canvas + * for UI elements + */ + const canvasSpace = 200; + + /** Setup scene */ + let [scene, camera, renderer] = setup_scene(canvasSpace); + + /** setup cloth and generate debug mesh */ + let cloth = new Cloth(); + cloth.createBasic(10, 10, 5, 5); + cloth.createDebugMesh(scene); - camera.position.z = 5; + const material = new THREE.MeshBasicMaterial({ color: 0x0000ff }); + const mesh = new THREE.Mesh(cloth.geometry, material); + scene.add(mesh); + /** + * function called every frame + * @param {number} dt - time passed since last frame + */ function animate(dt) { requestAnimationFrame(animate); renderer.render(scene, camera); } + /** add callback for window resize */ let canvas = document.getElementsByTagName("canvas")[0]; let resize = function () { - w = window.innerWidth; - h = window.innerHeight - 200; + let w = window.innerWidth; + let h = window.innerHeight - 200; canvas.width = w; canvas.height = h; } window.onresize = resize; resize(); + + /** + * if canvas has been successfully initialized + * start rendering + */ if (canvas.getContext) { - ctx = canvas.getContext('2d'); animate(performance.now()); } + + /** add mouse move callback */ canvas.onmousemove = (evt) => { mousePos.x = evt.clientX; mousePos.y = evt.clientY;