我正在用 Three.js 渲染一个 obj 文件(来自搅拌机),我遇到的问题是动画随着时间的推移而变慢(相当戏剧化!)。
这是我使用的代码:
<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>
在 Safari 上,性能下降的速度有点慢,在 Chrome 上它很快。
任何帮助表示赞赏:)
杰弗里