I am trying to understand how a mesh is loaded and rendered in a scene in Babylonjs. It goes without question that I am very new to all of this, and the reason I want to understand this is because of a silly error I am facing.
I am trying to toggle the visibility of a mesh in this silly little demo - p2dl-test-1 (please give 30-60 seconds to load completely).
In the demo I am able to hide the object, but I don’t understand why it is not reappearing.
I am basically copy-pasting this exact same code - Show - Hide Mesh | Babylon.js Playground (babylonjs.com)
Why can’t I do this simply using some setVsibility command? All of this feels very complicated …
This is my code - p2dl-error ($3703674) · Snippets · GitLab
Hello
First of all, it a good practise to share a Playground reproducing your issue, it ables us to help
Then, did you try to log ?
I added in the playground above some logs :
function toggleMeshes(meshes){
for (mesh of meshes){
let enabled = mesh.isEnabled();
mesh.setEnabled(!enabled);
console.log(mesh.name, ":", enabled, "-->", mesh.isEnabled());
}
}
And the result helps to understand :
__root__ : true --> false
GROUND_ASHPLAT : false --> false
GROUND_GRASS : false --> false
GROUND_RUNWAY_LIGHTS : false --> false
GROUND_RUNWAY : false --> false
What’s happenning is that you first setEnabled(false)
the parent.
Which enable as false
the children too. And they cannot be set to true
since parent is false
Then when you click again :
__root__ : false --> true
GROUND_ASHPLAT : true --> false
GROUND_GRASS : true --> false
GROUND_RUNWAY_LIGHTS : true --> false
GROUND_RUNWAY : true --> false
Boom, the parent comes true
alongs with the children, who now go false
So, solution is to deal only with the parent :
function toggleMeshes(meshes){
meshes[0].setEnabled(!meshes[0].isEnabled());
}
And here is the Fixed Playground
++
Tricotou
3 Likes
Thank you so much. This is not a practice I am familiar with, and I will be implementing it
You are a life saver