FlashyGoblin

Mesh visibility of children

Recommended Posts

Is there a way to set the visibility of a mesh to false and have all of it's child meshes be affected as well? I'm setting the children using .parent = parentMesh. Currently it seems that the only way is to loop through all of the children manually. The major problem with this approach is when you have children of children of children. 

Here is a PG to demonstrate. Uncomment the comment block. http://www.babylonjs-playground.com/#17IGZF#0 

var materialSphere = new BABYLON.StandardMaterial("texture2", scene);
materialSphere.diffuseColor = new BABYLON.Color3(1, 0, 0); //Red
materialSphere.alpha = 0.2;

// grand parent
var grandParent = BABYLON.Mesh.CreateSphere("sphere1", 16, 3, scene);
grandParent.material = materialSphere;

// parent
var parent = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
parent.material = materialSphere;
parent.parent = grandParent;

// child
var child = BABYLON.Mesh.CreateSphere("sphere1", 16, 1, scene);
child.material = materialSphere;
child.parent = parent;
	
// uncomment block below to hide the grandParent and its children
/* 
grandParent.visibility = false; // 
grandParent.getChildren().forEach(function(_child) {
	_child.visibility = false;
}, this);
*/	

// although children's children are not effected

 

Share this post


Link to post
Share on other sites

You're welcome :)

Samuel - Nice, I didn't know there is already a built-in way to recursively collect all child meshes.

Why not call the function only once? Although the performance difference is probably negligible in this case, it's a good practice.

By the way, are visibility and isVisible completely interchangeable or is there a difference?

http://www.babylonjs-playground.com/#17IGZF#4

Share this post


Link to post
Share on other sites

@royibernthal: One of the interesting uses of mesh.visibility is that you can create fade ins/outs with code like this - ie visibility animations::

aCurtain.actionManager = new BABYLON.ActionManager(myScene);
var action = new BABYLON.InterpolateValueAction(BABYLON.ActionManager.NothingTrigger, aCurtain, "visibility", 0.0, 3000);
aCurtain.actionManager.registerAction(action)
	
......

action.execute();

Where aCurtain is a big plane or box between the camera or the scene.

cheers, gryff :)

Share this post


Link to post
Share on other sites
8 hours ago, Kesshi said:

Instead of "grandParent.isVisible = false;" you can use "grandParent.setEnabled(false);". This disables/hides the parent and all the children.

 

17 hours ago, royibernthal said:

I'd suggest creating a recursion.

http://www.babylonjs-playground.com/#17IGZF#1

Both of these solutions work pretty well. I'm wondering what the pros and cons would be between setting a mesh's visibility vs setEnabled(). 

Share this post


Link to post
Share on other sites

With setEnabled you don't need to care about your children. Even if you add a new children to the parent it will be automatically disabled if the parent is disabled (even if you have several nested children).
With the visibility you have to manage your children by yourself. If you add a new children you have to check if there parent is an invisible parent and adjust the visibility flag of the child.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.