diff --git a/index.html b/index.html index a42ce20..daf91f5 100644 --- a/index.html +++ b/index.html @@ -19,122 +19,75 @@ + diff --git a/scene.js b/scene.js new file mode 100644 index 0000000..e6ee68e --- /dev/null +++ b/scene.js @@ -0,0 +1,57 @@ +import * as T from 'http://esm.sh/three'; +import { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.js'; + +export { T as THREE }; +export function Scene(inSettings) +{ + const renderer = new T.WebGLRenderer({canvas:document.querySelector(inSettings.DOM)}); + const scene = new T.Scene(); + const camera = new T.PerspectiveCamera(80, 2, 0.1, 10); + const light = new T.DirectionalLight(0xFFFFFF, 1); + const controls = new OrbitControls(camera, renderer.domElement); + let dirty = false; + + const update = inTime => + { + let timeNew = new Date(); + let timeDelta = timeNew - inTime; + let delta = timeDelta * 0.001; + + inSettings.Update(scene, delta); + + controls.update(); + dirty = true; + setTimeout(()=>update(timeNew), 50); + } + + const render = inTime => + { + if(dirty) + { + renderer.render(scene, camera); + dirty = false; + } + requestAnimationFrame(render); + } + + const resize = () => + { + let canvas = renderer.domElement; + camera.aspect = canvas.clientWidth / canvas.clientHeight; + camera.updateProjectionMatrix(); + renderer.setSize(canvas.clientWidth, canvas.clientHeight, false); + }; + + scene.add(light); + scene.add(new T.GridHelper(10, 10)); + light.position.set(-1, 2, 4); + camera.position.set(0, 0, 2); + controls.update(); + + inSettings.Init(scene); + + window.addEventListener("resize", resize); + resize(); + render(0); + update(new Date()); +}; \ No newline at end of file