Jump to content

Mesh visibility of children


FlashyGoblin
 Share

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

 

Link to comment
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

Link to comment
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 :)

Link to comment
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(). 

Link to comment
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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...