Automatically Convert Instances to Thin Instances?

I saw the Thinnizator demo but it does not really do what I need.

Basically am trying to turn all instances with different positioning or what have you into thin instances.

The problem (I think) is that I need to figure out how to negate the base meshes matrix in order for them to go into the correct position.

Any ideas how I can keep the scene consistent after converting them?

I think this gets close to what you want, except for the faces all being inverted because of the root node’s negative z scale:

https://playground.babylonjs.com/#FIXETD#6

Mesh.createInstance doesn’t set the instance parent to the base mesh. The instances made with Mesh.createThinInstance don’t have parent mesh, but the given instance’s matrix is applied after the base mesh’s world matrix when rendered, so you need to multiply the thin instance matrix by the inverse of the base’s world matrix to get it into the same position as a non-thin instance.

Awesome thank you, can you think of a way to get the faces to be correct as well?

I tried Mesh.flipFaces() and that worked for all the instances except the one added with thinInstanceAddSelf, so no I can’t think of a way to cover the edge cases, yet.

@Evgeni_Popov might know.

I tried to force a flip, but the left one doesn’t work.

There’s a problem because the instances have a world matrix determinant > 0 whereas the world matrix of the base mesh has a determinant < 0 (because of the -Z scaling on its parent). See:

It will work if you parent your instances to the same parent than the base mesh because they will all have the same sign for their determinant:

1 Like

@Evgeni_Popov
Then how would I make it work for a setup like this?

I was trying to do what you said but no matter what combination of parents I tried to set things to it still left the faces inverted.

This would be a real world example of when I want to do this. The artist has a bunch of negative scaling to reflect the one side of the scenes models to the other. I need to make sure that no matter what the structure is that the glb is in that I can run this process on it.

You will need to loop over all the instances, and sort them according to their world matrix determinant: the negative ones and the positive ones.

If you only have negative or positive instances, then no changes. If you have both, you will have to create an additional base mesh and assign all the negative instances to one base mesh and all the positive to the other. You will also have to clone the material, reverse the sideOrientation property and assign this material to the second base mesh.

See how it is done in the PGs linked in the doc:

1 Like

Got it thank you!

I was just checking the scalingDeterminate then it clicked you were saying globalMatrix.

Sorry that Im dense af… but following your instructions I can not for the life of me get it to work.

I iterated through all the instances identified if they are negative or positive got two separate lists of them, made a new object with a material that has the inverted prop and assigned them.

Oddly enough as well, if I just do mapping.baseMesh.clone() the object does not get cloned into the scene? So I had to search for the mesh which Im not too sure why that is?

Is it possible to make a repro with only a single mesh + its instances that would not work after thin instance conversion?

The scene is a bit heavy and it’s hard to understand what’s going on.

Thanks in advance!

1 Like

this is just being applied to the brick ground area now.

If you fly under the stage and look up you will see that the inversed Thin instance is there.

The two objects are “CapitolTile.001_skiplm” and “Inverted:CapitolTile.001_skiplm” now.

Changing the material side orientation is seeming to do nothing, the only way I can get it to appear is telling it backfaceCulling = false which does not seem correct.


Here’s a working PG:

The main problem was that the glTF loader sets the mesh.overrideMaterialSideOrientation property, which takes precedence over the material.sideOrientation property, meaning the new material side orientation was not taken into account.

4 Likes

I would have never figured that out, you are amazing!

1 Like