voxel/scene.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-07-07 07:00:50 -04:00
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();
2021-07-11 09:24:39 -04:00
const camera = new T.PerspectiveCamera(80, 2, 0.1, 100);
2021-07-07 07:00:50 -04:00
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);
2021-07-07 12:09:52 -04:00
//scene.add(new T.GridHelper(10, 10));
2021-07-07 07:00:50 -04:00
light.position.set(-1, 2, 4);
2021-07-11 09:24:39 -04:00
camera.position.set(0, 5, -5);
camera.lookAt(new T.Vector3(0,0,0));
2021-07-07 07:00:50 -04:00
controls.update();
inSettings.Init(scene);
window.addEventListener("resize", resize);
resize();
render(0);
update(new Date());
};