Threejs 如何使用 WireframeHelper 删除线框 [英] threejs how to remove wireframe with WireframeHelper

查看:88
本文介绍了Threejs 如何使用 WireframeHelper 删除线框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为对象添加线框的功能,所以我使用以下函数来完成它,在它工作的第一个条件下,我可以应用线框

I have a function to add wireframe for the objects, so I am using the following function to do it, on the first condition it is working and I can apply wireframe

 function wireframe(state){
                        
                    
                      if(state=='on')
                      {
                          var wfh = new THREE.WireframeHelper( mesh, 0xf00e0e );
                          wfh.material.wireframe = true;
                          //wfh.material.linewidth = 1; // looks much better if your PC will support it
                          scene.add( wfh );                     
        
                      }
                      if(state=='off')
                      {
                           var wfh1 = new THREE.WireframeHelper( mesh,0xf00e0e );
                           wfh1.material.wireframe = false;
                           scene.remove(wfh1);  
                      }
                
            }

在第二种情况下,我想删除对象上应用的线框,因此使用 scene.remove(wfh1); 删除场景,但它不起作用.线框不会从对象中移除.

In the second condition I want to remove the applied wireframe on object, so remove the scene with scene.remove(wfh1);, but it doesn't work. Wireframes are not removed from the object.

推荐答案

我认为这就是您想要的.您需要一个全局变量来存储线框对象,只需为其赋值一次(if 语句),然后有选择地将其添加或从场景中删除.

I think this is what you want. You need a global variable to store the wireframe object assign a value to it just once (if statement) and then selectively add it or remove it from the scene.

var wfh;

function wireframe (state) {

    if  ( state=='on' )
    {
        if ( wfh === `undefined` )    // so you dont add a new wfh each time you call wireframe
            wfh = new THREE.WireframeHelper( mesh, 0xf00e0e );

        scene.add( wfh );                     
    }
    else if ( state=='off' )
    {
        scene.remove( wfh );  
    }
}

这篇关于Threejs 如何使用 WireframeHelper 删除线框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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