Expose TransformNode._localMatrix

Hi everyone,
In my app, I need to get TransformNode._localMatrix to play with Thin Instances of meshes under node.
I am worried that something may go wrong, because this property is hidden.

Yup it is not meant to be exposed and the behavior might change so you can only use it with potential breaking changes in future version.

Maybe if you explain your use case there might be another way to address it ?

@Evgeni_Popov is amazing with thin instances

1 Like

I am loading a model with a lot of same meshes. For better understanding, model is a building with all of windows being individual mesh, because I need to let client interact with each of them individually.

Window consists of several meshes, so meshes of single window are in their own TransformNode. Since it is faster to use thin instances, I need to get local matrix of TransformNode and put it in transform of thin instances.

In this case, you can use _localMatrix, but I would prefer to not expose it fully as despite being safe-ish there could be lots of gotcha with pivot, pose and such :slight_smile:

What you can do to avoid accessing the private variable is to compute the local matrix as something like Invert(World_parent) * World where Invert(World_parent) is the invert matrix of the world matrix of the parent node, and World is the world matrix of the current node.

1 Like

I tried your advice. I have a scene with 4 buildings. Two of them were ok, two of them failed :frowning: . With TransformNode._localMatrix it was all working. Maybe you have any ideas what is missing here. If it’s not that easy then I will recreate this in playground.

Here is my code.

let invert = new Matrix();
instanceParent.parent!.getWorldMatrix().invertToRef(invert);
let result = new Matrix();
invert.multiplyToRef(instanceParent.getWorldMatrix(), result);                    
windowPartMesh.thinInstanceAdd(result, true);

// this one is working
// windowPartMesh.thinInstanceAdd(instanceParent._localMatrix, true);

Here is what happening.

My mistake, you should post-multiply by the invert, not pre-multiply:

https://playground.babylonjs.com/#YIUKLJ

Also, don’t forget to call computeWorldMatrix(true), just to be sure the world matrices are up to date.

2 Likes