get back to normal after the traverse in threejs

梦想与她 提交于 2019-12-13 05:04:52

问题


in threejs am working around with the traverse method to apply the WireframeHelper for the models that is loaded using OBJMTLLoder, for this kind of models we must use traverse to apply the Wireframe for child of the Object, so here i can apply the wireframes and so on using traverse but after the traverse i can't get back to my normal object meaning that i can't remove the wireframes with the traversed mesh, the mesh is added with the scene with scene.add( wfh ); where wfh is WireframeHelper , but if i use scene.remove( wfh ); to remove the meshed WireframeHelper it doesn't work

i need to know that after the traverse we can get back to normal ?? in most cases am using traverse to make changes on my model:

Here is the code:

scene.traverse ( function (child)
{
    if (child instanceof THREE.Mesh)
    {
        wfh = new THREE.WireframeHelper( child, 0xffffff );
        scene.add( wfh );  
    }
});

updated code:

globalObject.traverse ( function (child) {
    if (child instanceof THREE.Mesh)
        {
        wfh = new THREE.WireframeHelper( child,whfcolor );
        wfh.name = "wireframe_helper";

        wfh.material.opacity = 0.2;
        wfh.material.transparent = true;
        globalObject.add( wfh );

        }

     });

here globalObject is global variable assigned to Object now i can see the wireframe_helper on the child of the Object and can remove the wireframe by following code

globalObject.traverse( function ( child ) {

  if (child instanceof THREE.Object3D)
  {
      //alert(child.name);
      if ( child.name && child.name === "wireframe_helper" && child.material ) {

          //alert('hi');male-02-1noCullingID_male-02-1noCulling.JP

              globalObject.remove( child );

               //child.material.wireframe = true;

          }
  }
});  

after removed the wireframe still wireframe is remains some part of the Object any clue on this ?? and am getting

TypeError: this.children[i] is undefined
this.children[ i ].traverse( callback );

on line three.js 7885


回答1:


WireframeHelper() creates an object that is added to the scene-graph. When you are using the helper inside a traverse() operation you are adding many objects to the scene. So if you want to remove them, you have to save them off to a variable (array in this case since you have many of them). So something like this should work:

First name the helper inside the first traverse():

wfh = new ...
wfh.name = "wireframe_helper";
scene.add( wfh );

then you should be able to do:

scene.traverse ( function (child)
{
    if (child.name === "wireframe_helper")
    {
        scene.remove( child );
    }
}

The code above would probably crash when trying to traverse a child that has been removed. Here is updated code:

to_remove = [];
scene.traverse ( function (child)
{
    if (child.name === "wireframe_helper")
        to_remove.push( child );
}
for (var i = 0; i < to_remove.length(); i++)
    scene.remove( to_remove[i] );


来源:https://stackoverflow.com/questions/24656051/get-back-to-normal-after-the-traverse-in-threejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!