obja/js/main.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-12-09 18:41:42 +01:00
let camera, scene, renderer, loader, light1, light2, controls, model;
init();
animate();
function init() {
2019-12-11 17:49:13 +01:00
let url = 'assets/' + (document.URL.split('?')[1] || "bunny.obj");
2019-12-11 16:48:29 +01:00
2019-12-11 17:06:34 +01:00
loader = new Loader(url, 1024, 20);
2019-12-09 18:41:42 +01:00
loader.start(function(elements) {
for (let element of elements) {
if (element !== undefined) {
2019-12-11 17:06:34 +01:00
try {
model.manageElement(element);
} catch(e) {
if (e.type === "custom") {
document.getElementById('errormessage').innerHTML = e;
} else {
throw e;
}
}
2019-12-09 18:41:42 +01:00
}
}
});
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.0001, 1000);
camera.position.z = 3;
scene = new THREE.Scene();
2019-12-11 17:06:34 +01:00
model = new Model(url);
2019-12-09 18:41:42 +01:00
scene.add(model);
light1 = new THREE.AmbientLight(0x999999);
scene.add(light1);
light2 = new THREE.DirectionalLight(0xffffff, 1.0);
light2.position.set(0.0, 1.0, 0.0);
scene.add(light2);
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
2019-12-11 16:48:29 +01:00
window.addEventListener('resize', onWindowResize, false);
2019-12-09 18:41:42 +01:00
controls = new THREE.OrbitControls(camera, renderer.domElement);
}
function animate() {
requestAnimationFrame(animate);
controls.update();
2019-12-11 16:48:29 +01:00
document.getElementById('progressbar').value = Math.floor(100 * loader.percentage()) || 0;
2019-12-11 16:02:46 +01:00
document.getElementById('percentage').innerHTML = Math.floor(100 * loader.percentage()) / 100 + "%";
2019-12-09 18:41:42 +01:00
renderer.render(scene, camera);
}
2019-12-11 16:48:29 +01:00
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}