How to Hide/Show Objects using Three.js release 54?

不打扰是莪最后的温柔 提交于 2019-12-24 03:34:07

问题


I've asked this question as a part of Huge question but was recommended to ask in parts. Here comes the part of my previous question. My previous question was: Here

I've been using Three.js Release 50 and able to show/hide the objects (In my application, it is a mesh child) with the help of:

THREE.SceneUtils.traverseHierarchy(mesh,function(child){
var z = document.getElementById("cameras").selectedIndex*5 -10;
if (z === -10){
    child.visible = true;
} else if (child.position.z !== z){
    child.visible = false;
} else {

    child.visible = true;
};
}); 

But while using release 54, it is said that to use, object.traverse but found very difficult to the same. How to replace the above code using release 54? The error I'm getting while using release 54 is:

Please help me to sort this out.


回答1:


Is the 'mesh' variable you are sending the 'traverseHierarchy' function an Object3d? If so have you tried 'mesh.children' which should return an array of child objects, or you could use the traverse function on the mesh object.

See: http://mrdoob.github.com/three.js/docs/54/#Reference/Core/Object3D

mesh.traverse(function(child) {
    var z = document.getElementById("cameras").selectedIndex * 5 - 10;
    if (z === -10) {
        child.visible = true;
    } else if (child.position.z !== z) {
        child.visible = false;
    } else {
        child.visible = true;
    };
});



回答2:


Dig deeper, answers you will find.

object.traverseHierarchy() was renamed to object.traverse()

$ grep -A10 'traverse: function' build/three.js
    traverse: function ( callback ) {

            callback( this );

            for ( var i = 0, l = this.children.length; i < l; i ++ ) {

                    this.children[ i ].traverse( callback );

            }

    },



回答3:


simply use the object traverse method to hide the mesh in three.js.In my code hide the object based on its name

object.traverse ( function (child) {
    if (child instanceof THREE.Mesh) {  
        if (child.name.includes("3F")) {
            child.visible = true;
        } else {
            child.visible = false;
        }
    }
});


来源:https://stackoverflow.com/questions/14122919/how-to-hide-show-objects-using-three-js-release-54

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