2013-12-20 75 views
2

我想在我的ThreeJS场景中使用THREE.LOD对象。我一直启发http://threejs.org/examples/webgl_lod.html三个JS中的LOD DAE对象的可见性

,但我想进一步推的想法,并使用DAE模型(使用此装载机:http://threejs.org/examples/webgl_loader_collada.html

问题是我不能切换LOD级别的可见性。首先,我想在我的渲染功能的自动化一个(基于摄像机的距离,在本例中):

this.m_Scene.traverse(function (object) { 

    if (object instanceof THREE.LOD) { 

     object.update(that.m_Camera); 
    } 
}); 

因为它不工作(我所有的LOD是在同一时间显示)。我尝试了更多的手册。并且它看起来Object3D.visibility属性没有被渲染器真正使用,或者没有被子代继承。

据我了解,这个属性是用于网格。但我不确定它在渲染时检查。

因此预期这不起作用:

var LodTemporaryObject = new THREE.LOD(); 
function LoadLod1() 
{ 
    //TEST COLLADA LOADER 
    var loader = new THREE.ColladaLoader(); 
    loader.options.convertUpAxis = true; 

    loader.load(Lod2Path, function (collada) { 

    dae = collada.scene; 

    dae.scale.x = dae.scale.y = dae.scale.z = 0.1; 
    dae.updateMatrix(); 
    dae.visible = false; //THIS HAS NO EFFECT 
    LodTemporaryObject.addLevel(dae,100); 

    AddLodToScene(LodTemporaryObject); //where the lod is added to the threeJS scene object 
}); 

}

因此质疑:实际上,我怎么(中)可见任何Object3D或子场景设置?

回答

0

THX到WestLangley回答上面,我想出了一个递归解决我的问题:

第一,递归函数来更新孩子的可见性,以配合母公司:

function SetChildrenVisible(parent) 
{ 
    if(parent instanceof THREE.Object3D) 
    { 
     for(var i = 0; i< parent.children.length; i ++) 
     { 
      parent.children[i].visible = parent.visible; 
      SetChildrenVisible(parent.children[i]); 
     } 
    } 
} 

然后在我的渲染循环:

this.m_Scene.traverse(function (object) { 

    if (object instanceof THREE.LOD) { 
     //save all lodLevel state before updating 
     var oldVisible =[]; object.visible; 
     for(var i = 0; i< object.children.length; i++) 
     { 
      oldVisible.push(object.children[i].visible) 
     } 
     //Update Lod 
     object.update(that.m_Camera); 

     //Check for changes and update accordingly 
     for(var i = 0; i< object.children.length; i++) 
     { 
      if(oldVisible[i] != object.children[i].visible) 
      { 
       SetChildrenVisible(object.children[i]); 
      } 
     } 

    } 

}); 

目标是只更新对象,它的属性改变。

4

编辑:下面的答案是过时的。可见性现在被继承。例如参见Show children of invisible parents

three.js所r.71


能见度不被孩子继承WebGLRenderer

的解决办法是使用像这样的模式:

object.traverse(function(child) { 

    if (child instanceof THREE.Mesh) { 

     child.visible = false; 

    } 

} 

three.js所r.64