在threejs中遍历后恢复正常 [英] get back to normal after the traverse in threejs

查看:38
本文介绍了在threejs中遍历后恢复正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在threejs中,我正在使用traverse方法为使用OBJMTLLoder加载的模型应用WireframeHelper,对于这种模型,我们必须使用traverse为Object的孩子应用Wireframe,所以在这里我可以应用线框等使用遍历但遍历后我无法回到我的正常对象,这意味着我无法删除带有遍历网格的线框,网格与场景一起添加 scene.add( wfh); 其中 wfh 是 WireframeHelper ,但是如果我使用 scene.remove( wfh ); 删除网格化的 WireframeHelper 它不起作用

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

我需要知道在遍历之后我们可以恢复正常吗??在大多数情况下,我使用 traverse 对我的模型进行更改:

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:

代码如下:

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

更新代码:

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 );

        }

     });

这里 globalObject 是分配给 Object 的全局变量,现在我可以看到对象的子对象上的 wireframe_helper 并且可以通过以下代码

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 );

在线三.js 7885

on line three.js 7885

推荐答案

WireframeHelper() 创建添加到场景图中的对象.当您在 traverse() 操作中使用助手时,您将向场景中添加许多对象.所以如果你想删除它们,你必须将它们保存到一个变量中(在这种情况下是数组,因为你有很多).所以这样的事情应该有效:

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:

在第一个 traverse() 中命名帮助器:

First name the helper inside the first traverse():

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

那么你应该能够做到:

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] );

这篇关于在threejs中遍历后恢复正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆