FlashyGoblin Posted June 20, 2016 Share Posted June 20, 2016 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 JackFalcon 1 Quote Link to comment Share on other sites More sharing options...
royibernthal Posted June 20, 2016 Share Posted June 20, 2016 I'd suggest creating a recursion. http://www.babylonjs-playground.com/#17IGZF#1 FlashyGoblin and JackFalcon 2 Quote Link to comment Share on other sites More sharing options...
FlashyGoblin Posted June 20, 2016 Author Share Posted June 20, 2016 1 minute ago, royibernthal said: I'd suggest creating a recursion. http://www.babylonjs-playground.com/#17IGZF#1 Yep, that works pretty great! Thanks! Quote Link to comment Share on other sites More sharing options...
Samuel Girardin Posted June 20, 2016 Share Posted June 20, 2016 var p = grandParent for (var i = 0; i < p.getChildMeshes(false).length; i++){ p.getChildMeshes(false)[i].visibility = false; } @FlashyGoblins JackFalcon 1 Quote Link to comment Share on other sites More sharing options...
royibernthal Posted June 20, 2016 Share Posted June 20, 2016 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 JackFalcon 1 Quote Link to comment Share on other sites More sharing options...
gryff Posted June 20, 2016 Share Posted June 20, 2016 Quote are visibility and isVisible completely interchangeable or is there a difference? @royibernthal Visibility can be set to to values between 0 and 1 - so you can create semitransparent meshes. Is Visible is true/false. Playground cheers, gryff Samuel Girardin, JackFalcon and royibernthal 3 Quote Link to comment Share on other sites More sharing options...
royibernthal Posted June 20, 2016 Share Posted June 20, 2016 @gryff I see, thanks Quote Link to comment Share on other sites More sharing options...
gryff Posted June 21, 2016 Share Posted June 21, 2016 @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 JackFalcon and royibernthal 2 Quote Link to comment Share on other sites More sharing options...
Kesshi Posted June 21, 2016 Share Posted June 21, 2016 Instead of "grandParent.isVisible = false;" you can use "grandParent.setEnabled(false);". This disables/hides the parent and all the children. c75, FlashyGoblin and JackFalcon 3 Quote Link to comment Share on other sites More sharing options...
FlashyGoblin Posted June 21, 2016 Author Share Posted June 21, 2016 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. This is exactly what I was looking for! Thanks @Kesshi! Quote Link to comment Share on other sites More sharing options...
FlashyGoblin Posted June 21, 2016 Author Share Posted June 21, 2016 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(). Quote Link to comment Share on other sites More sharing options...
Kesshi Posted June 21, 2016 Share Posted June 21, 2016 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. FlashyGoblin, royibernthal and c75 3 Quote Link to comment Share on other sites More sharing options...
FlashyGoblin Posted June 21, 2016 Author Share Posted June 21, 2016 That makes sense! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.