Changing visibility for individual instances?

Hi everyone!

I have a model (glb) where I change the mesh’s visibility.
everything is worked great when I need to change the visibility of all the meshes.
the problems start when i change the visibility of an individual instance mesh. when I select an instance mesh it does not get affected by the visibility property and if i select his parent its changes the visibility of child instance (like he should)

how i can break/destroy the connection between the instance and parent and make each instance unique?
I am asking because I have tried all
change parent to null play with customBuffers and change the alphaMode and try to remove the instance on the parent with the child instance
nothing help.
thanks for the help.

Hi and welcome to the Community,
Hope you like your experience of BJS so far.
To answer your question, I would first want to clarify if we are talking about ‘instances’ or ‘thin-instances’?

There’s a difference in the handling but they also have things in common.
I.E. you cannot ‘break’ the link between the ‘source’ and the instanciated object. Simple instances are taking from the source (same as thin-instances) with the difference that a simple instance can still be worked as a mesh (up to some shared parameters). I.E. Setting the mesh ‘visibility’ individually on a instance can be done straight by selecting the instance (by name or id) and giving the new value for ‘visibility’ - Whereas, thin-instances require you to write to the buffer before they are rendered because of the way they are computed.

From the reading above, I understand you would also need to make a difference between the mesh ‘visibility’ and the ‘material’ settings (with everything related to alpha). A same material can be shared along meshes and instances. Changing this material will affect all meshes and instances using it.
Also, parenting does not affect the way an instance or thin-instance is rendered. Making it a child or nulling it won’t change anything towards how they are linked to the parameters they get from the source.

So, for the short answer, either you manage to set-up a playground environment (best case scenario) and we’ll fix it for you - - OR - - copy/paste some of the code you use to create and manage your instance/thin-instance.

Meanwhile, have a great day :sunglasses:

HI MAWA
first of all, let me just say thanks for your quick response!
i am sorry that i wasnt clear, i am talking about ‘instances’ and not ‘thin instances’
i know that the material is shared between meshes and instances.
but the problem is when i select instance (by name or id) and set his visibility value nothing happened only if change the source mesh visibility value the source and is instances are changing.
so how can i change an instance mesh visibility without changing the source mesh visibility?
i hope that i have been more clear about my issues.
once again thanks!

Do you know how to load external assets in the playground?
It would be the easiest for us to be able to interact straight with the scene and see the nodes loaded from your glb. Instances should have a different name but eventually they don’t. Sadly, I cannot know without the PG. One thing is sure though, you can give different ‘visibility’ to your instances of meshes (without calling the buffer or anything). I do this all the time. Only thing is, you need to be able to select just the instance you want, so they should have a different name or id.

Edit: One more thing. FYI: Good practice when using instances is that you would make the source mesh invisible and render just the instances (however, this doesn’t change anything for your issue). Try to set-up that PG for us and let us know if you don’t manage…

its interesting that you say that its works for you when you set visibility to an instance because I don’t do anything special
anyway I think I succeeded to upload a playground in the next link

even there you can see that only if I change the source mesh its work and affect all others If I change one of the other meshes it’s not working
and the names are different

Apologies. I was using clones in this case, not instances. My bad, sry for the confusion.
You should be able to change the alpha through the ‘instancedBuffers.color’ (as in the link below) but for some reason I’m getting an error when I try this from your glb

May be I’ll ask the expert @Evgeni_Popov to have a look it, will you? Thanks,

Actually, changing the visibility property on instances won’t work because this parameter ends up being a material parameter (it is used in shader code): it will have some effect only on the master mesh, and will affect all instances. You should use the mesh.setEnabled(true/false) function instead.

Ah, it seems I didn’t understand the question!

Here’s a PG from the thread that @mawa linked that is instancing an object from a gltf file and which is updating the alpha through the color buffer:

HI evgeni thanks…
I give it another shot but ‘registerInstancedBuffer(“color”,4)’ throw me an error
“Uncaught TypeError: Cannot read properties of null (reading ‘toArray’)”
so I can’t find any solution to it.

Yes, I got the exact same issue. Couldn’t figure why. I never did create my instances with a glb format. It might just not be handled the same as gltf. Is it @Evgeni_Popov ?

Could you provide a playground that shows the error?

oops, I deleted it (with rage :grin:)…should be ez to quickly reproduce though… Mine was just very simply based on your PG for this…

Edit:

Depending on what I do I can get various results/errors.
If I use setParent on the parented source mesh, I get this:

However (LOL) while doing this, I found a way to have it work (like it should in this PG for meshes[2]/Sphere002. Still don’t really get the thing with setParent and having to do a let for mesh selection. Must be just my small brain :brain: :sweat_smile:

Edit 2: Fancy me cannot reproduce the second error now where it couldn’t read the color. LOL.
In fine, I believe it works (somehow) so I guess you can forget about the above :grin: :sweat_smile:

Footnote: Except I would still like to understand why setParent doesn’t seem to work here?

This PR will fix the crash of this PG:

The color of the boombox still won’t be ok because vertex color will be enabled for the instance (because of the BoomBox.registerInstancedBuffer('color', 4) call), but the instanced boom box is not considered as an instance for rendering purpose (see my comment in the PR), so it will be rendered as a regular mesh instead. But the regular boom box doesn’t have a vertex color buffer, so the shader will use a black color instead.

To have the instance being considered as a real instance, the sign of the determinant of its world matrix must be the same than the one of the source mesh. To do that, you can either:

Lastly, when you enable an instance buffer for a mesh, you must give a value for all its instances. So, in the example with 5 balls, all 5 balls must have an instance color, else it will crash.

1 Like

thx a lot for the explanation and the PR. Sure makes more sense when you read it like this :smiley:

Once again thanks!
i tried to check the implementation of the scene.useRightHandedSystem=true but I am afraid that it will cause me some problems on my current project so I built the next PR. with the second method

I set the same parent to each instance and set all the instances to the same instance color buffer and well it looks good now all I left is to check on my project and hope it will be good :slight_smile: .
can I ask one more question…you can see in the example that the balls sometimes disappear when playing with the camera, do you have any idea why?

cancel the scene.createDefaultEnvironment(); solve it so i guess all good

1 Like