Having an issue with animations slowing down over time when rendering an obj file from Blender using Three.js. Here's the code snippet I'm working with:
<script>
var objcontainer, stats;
var camera, scene, renderer;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
init();
animate();
function init() {
objcontainer = document.createElement( 'div' );
document.body.appendChild( objcontainer );
// scene
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 300;
camera.position.x = 0;
camera.position.y = 0;
scene.add( camera );
/*var ambient = new THREE.AmbientLight( 0x101030 );
scene.add( ambient );*/
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.set( 1, 1, 1 ).normalize();
scene.add( directionalLight );
var loader = new THREE.OBJMTLLoader();
loader.addEventListener( 'load', function ( event ) {
var object = event.content;
object.position.y = 20;
object.position.x = 0;
object.position.z = 0;
object.rotation.x = -(Math.PI / 2);
scene.add( object );
}, false);
loader.load('objects/coque.obj', 'objects/coque.mtl');
loader.load('objects/ecran_int.obj', 'objects/ecran_int.mtl');
loader.load('objects/ecran_tour.obj', 'objects/ecran_tour.mtl');
loader.load('objects/app_photo.obj', 'objects/app_photo.mtl');
renderer = new THREE.WebGLRenderer({
antialias : true // to get smoother output
});
renderer.setClearColorHex( 0xe5e5e5, 1 );
renderer.setSize( window.innerWidth, window.innerHeight );
objcontainer.appendChild( renderer.domElement );
}
$('#left').bind('mouseenter', function() {
this.iid = setInterval(function() {
camera.position.x = camera.position.x + 10;
camera.lookAt( scene?.position );
renderer.clear();
renderer.render( scene, camera );
}, 25);
}).bind('mouseleave', function(){
this.iid && clearInterval(this.iid);
});
$('#right').bind('mouseenter', function() {
this.iid = setInterval(function() {
camera.position.x = camera.position.x - 10;
camera.lookAt( scene?.position );
renderer.clear();
renderer.render( scene, camera );
}, 25);
}).bind('mouseleave', function(){
this.iid && clearInterval(this.iid);
});
$('#up').bind('mouseenter', function() {
this.iid = setInterval(function() {
camera.position.y = camera.position.y - 10;
camera.lookAt( scene?.position );
renderer.clear();
renderer.render( scene, camera );
}, 25);
}).bind('mouseleave', function(){
this.iid && clearInterval(this.iid);
});
$('#down').bind('mouseenter', function() {
this.iid = setInterval(function() {
camera.position.y = camera.position.y + 10;
camera.lookAt( scene?.position );
renderer.clear();
renderer.render( scene, camera );
}, 25);
}).bind('mouseleave', function(){
this.iid && clearInterval(this.iid);
});
$('#zoom-in').bind('mouseenter', function() {
this.iid = setInterval(function() {
camera.position.z = camera.position.z - 10;
camera.lookAt( scene?.position );
renderer.clear();
renderer.render( scene, camera );
}, 25);
}).bind('mouseleave', function(){
this.iid && clearInterval(this.iid);
});
$('#zoom-out').bind('mouseenter', function() {
this.iid = setInterval(function() {
camera.position.z = camera.position.z + 10;
camera.lookAt( scene?.position );
renderer.clear();
renderer.render( scene, camera );
}, 25);
}).bind('mouseleave', function(){
this.iid && clearInterval(this.iid);
});
$('#init').bind('click', function() {
camera.position.z = 300;
camera.position.x = 0;
camera.position.y = 20;
camera.lookAt( scene?.position );
renderer.clear();
renderer.render( scene, camera );
});
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
}
</script>
Noticing performance decrease slightly slower on Safari compared to Chrome. Any advice or help would be greatly appreciated :)
Geoffrey