【发布时间】:2015-02-21 13:20:12
【问题描述】:
在我的场景中,我有 4 个点光源,其中 3 个连接在相机上,还有大约 100 到 300 个立方体。 我有许多类别的立方体,每个类别介于 100 到 300 之间。根据用户菜单选择,我的场景中可能随时出现 1 个类别的立方体。
(100 个立方体的类别)renderer.info:
memory:
Objectgeometries: 2
programs: 3
textures: 100
render:
calls: 203
faces: 1360
points: 0
vertices: 4080
在一个循环中,我为每个类别生成我的立方体,如下所示:
var materials = [
backgroundMaterial,
backgroundMaterial,
backgroundMaterial,
backgroundMaterial,
productMaterial,
backgroundMaterial
];
var cubeMaterial = new THREE.MeshFaceMaterial( materials );
var object3D = new THREE.Mesh( geometryBox, cubeMaterial );
材料backgroundMaterial在循环外定义一次;
var backgroundMaterial = new THREE.MeshPhongMaterial({
color: this.options.models.boxColor,
specular: this.options.models.boxSpecular,
//emissive : 0xefefef,
//side: THREE.DoubleSide,
overdraw: false,
transparent: false,
metal:true,
shininess: this.options.models.boxShininess,
reflectivity: this.options.models.boxReflectivity,
fog:false
});
每次循环都在循环中使用 productMaterial,因为每个立方体的纹理都不同。
var productMaterial = new THREE.MeshBasicMaterial({
map: productModelTexture,
color: that.options.models.boxColor,
specular: that.options.models.boxSpecular,
//emissive : 0xefefef,
side: THREE.FrontSide,
overdraw: false,
transparent: false,
metal:true,
shininess: that.options.models.textureShininess,
reflectivity: that.options.models.textureReflectivity,
opacity: 1,
fog:false
});
另外我此时没有将网格添加到场景中,它们被设置为visible = false
之后,我将我的立方体推入一个数组对象中,该对象内的每个数组都是一个长度在 100 到 300 之间的立方体类别。
当我的应用程序启动时,我会运行一个类似于下面的动画,它将一类立方体带入场景。
helix : function( category ) {
if ( this.models[category] && this.models[category].length > 0 ) {
TWEEN.removeAll();
new TWEEN.Tween( this.camera.position ).to( {x:0,y:0,z:90000}, 1000 ).easing( TWEEN.Easing.Exponential.InOut ).start();
new TWEEN.Tween( this.camera.rotation ).to( {x:0,y:0,z:0}, 1000 ).easing( TWEEN.Easing.Exponential.InOut ).start();
this.models.reset( category );
for ( var i in this.models[category] ) {
var model = this.models[category][i];
model.visible = true;
this.scene.add( model );
new TWEEN.Tween( model.position ).to({
x: model.helix.position.x,
y: model.helix.position.y,
z: model.helix.position.z
}, randBtwn( 1000, 3000 ) ).easing( TWEEN.Easing.Exponential.InOut ).delay( 1001 ).start();
new TWEEN.Tween( model.rotation ).to( {
x: model.helix.rotation.x,
y: model.helix.rotation.y,
z: model.helix.rotation.z
}, randBtwn( 1000, 3000 ) ).easing( TWEEN.Easing.Exponential.InOut ).delay( 1001 ).onComplete(function(){
}).start();
}
}
}.bind( that )
此外,您会注意到 helix 内部的另一个函数调用,这个:this.models.reset( category );
下面的代码实质上是重置对象位置并将它们设置为visible = false,最后将它们从场景中移除。
reset : function( category, callback ) {
for ( var j in this.models ) {
if ( this.models[j] instanceof Array && this.models[j].length > 0 && category !== j ) {
for ( var i in this.models[j] ) {
var model = this.models[j][i];
model.visible = true;
new TWEEN.Tween( model.position ).to({
x: model.outside.position.x,
y: model.outside.position.y,
z: model.outside.position.z
}, 1000 ).easing( TWEEN.Easing.Exponential.InOut ).start();
new TWEEN.Tween( model.rotation ).to( {
x: model.outside.rotation.x,
y: model.outside.rotation.y,
z: model.outside.rotation.z
}, 1000 ).easing( TWEEN.Easing.Exponential.InOut ).onComplete(function ( m ){
m.visible = false;
this.scene.remove( m );
if ( callback ) {
callback();
}
}.bind( that, model )).start();
}
}
}
}.bind( that )
在一台电脑上,一切都很顺利,我以 36 fps 的速度运行。我的 gpu 是新的 nvidia GTX(不过我不知道 36 是否可以接受)。
问题是,当我尝试使用最新的 chrome 在我的 nexus 5 上运行我的应用程序时,我在离开场景的多维数据集和其他进入的多维数据集之间的过渡之间会出现巨大的 fps 损失。大多数情况下,这会导致chrome 崩溃...除此之外,如果我不更改类别并且没有播放动画,它在我的手机上运行正常。
PS:我无法合并几何图形,因为每个网格都必须在用户选择时自行移动。(如果我没有记错的话)
这种性能下降/崩溃的原因可能是什么?如果您在场景外移动 200 个立方体并在场景内移动另外 200 个立方体,您将如何处理类似的场景?有什么我应该考虑的提示吗,请记住我还是three.js的新手。
需要任何其他可能是原因的来源,请告诉我,我会更新我的问题。
【问题讨论】:
标签: animation 3d three.js webgl