Thinnizator - automatic thin instances

Hi guys!

EDIT: PG link updated: https://playground.babylonjs.com/#QZA92W#151

Entreé

This small script automatically creates thin instances of meshes located under a given node. It doesn’t care about the naming and tries to automatically find all “equal” meshes on the scene, creates a prefab mesh for the equal meshes, disposes the original meshes and creates the required amount of thin instances from the prefab mesh. The thin instances are positioned/rotated/scaled as the original meshes.

The nightmare

The model file I am working on (a part is used in this PG) is exported from an unnamed CAD software. The author must have been a joker, because he chose to name the chairs as Cubes, some of the tables are also Cubes, and some of the plants also. Than there are Planes, Spheres, or whatever. And some are instances, some not. And as a “cool” addition the meshes are super high poly (a simple chair thousands of vertices). I had an idea, whether I can tide up this mess automatically. At first because I didn’t want to mess with the Blender file and make changes in the master model and at second, because more model files are coming in the future from the same dude, so better be prepared!

The solution

So I got a model, with millions of vertices, randomly named meshes and high poly design. This is where thinnization may come handy.

The for the eye section

Here is an example with screenshots. Guys, please note, that the model is copyrighted and you are not allowed to download it by any means.

This is the GLB loaded without any tampering. Please note the numbers on the right side (92 FPS).

Below is the scene after I hit the button. It simply calls a method:

const root = scene.transformNodes.find(n => n.name === "Building")
thInnIze(root, matchNodePrefixesPredicate)

Everything below the root will be thinnized. And the predicate just filters out which meshes should be or not be thinnized. You can leave it empty to thinnize every mesh below the root node.

There is a treshold for thinnizing set. There must be at least two equals meshes to thinnize.

const matchNodePrefixesPredicate = (node) => {
    if (node && node.name) {
        const matched = nodePrefixesToThinnize.findIndex(p => node.name.startsWith(p)) > -1
        return matched
    }
    return false
}

Mind the numbers - 145 FPS - gain of 53 - not bad - it is much much more performant, but it is capped by the max refresh rate of my display

There is a possibility to visualize what meshes where picked as prefabs and where are the thin instances spawn positions (green - places where a regular mesh was replaced by a thin instance, red - prefabs):

And for the chaos lovers you can visualize the prefabs and/or spawn points by toggling on badges (badges are displayed only when spawn points and/or prefab positions are toggled on):

By clicking on the badge you get additional information about the prefab.

Your opinion matters

Let me guys know, whether you like the idea and I can make a library (a simple class) for this.

I need your HELP!

I wrote, that the thinnizator finds “equal” meshes, because for now two meshes are treated as equal when they have the same amount of vertices and are from the same material. This part needs to be enhanced and I will appreciate any input on how can I reliable compare two I would say not meshes, but geometries. Should I mess with the Geometry class or is there a pretty simple way?

Finals

More can be done by replacing the prefab meshes by a low poly version to gain even more FPS.

Please note, this is the first alpha version and yes, there are errors, known and visible and unknown :stuck_out_tongue:

Cheers!
R.

17 Likes

This is really great!

1 Like

Thank you! I was thinking about something like counting the number of faces from different directions and the count must match, as well the number of vertices, etc. Should this work?

EDIT: just realized, that this will not work, if for example the rotation is applied into the geometry

EDIT 2: so I could test for equal number of total vertices, number of faces, than find the center of the mesh, find the most distant face on both, if the vertices relative positions match on both faces I can go further and test the sibling faces with the same technique. What do you think?

Getting the length of both the positions & indices to match is going to really get a high probability correct. Having the same number of vertices & faces for meshes that do not match is not very likely, especially in the same model.

2 Likes

Thanks! And wanted to say, I love your videos! :sunglasses:

2 Likes

On YouTube. :sunglasses:

1 Like

Tough for my ears :joy:

I love where this thread is going !!!

1 Like

Guys, so terrible feeling, but I I thought that @JCPalmer is @PirateJC, lol! Nice introduction of myself. So to make things clear, I love PirateJC"s videos! Sorry for being dizzy :joy:

3 Likes

This made me suspicious, that we are both talking about someone else… :thinking:

1 Like

Pretty impressive! excellent job!

That could definitely deserves a page in our documentation!

3 Likes

Thank you! I will gladly contribute! I started to work on a mesh fingerprinting system to improve mesh comparison precision and I would like to share it as a part of the Thinnizator. So when it’s ready, we can roll the dices! :sunglasses: Bonne journée!

EDIT: However, feeling a bit nervous to guide other people when I am fighting with a “simple” rotation on a pivoted mesh around two axes and still couldn’t get it working right! :clown_face: I’ll keep trying and learning… :vulcan_salute:

EDIT2: Got it working! Everything is in the docs, just have to read carefully! :wink:

2 Likes

This is an exciting piece of work, congrats!

I wonder if you could use a performant hash library, compute the hash of the meshes to compare them?

1 Like

Thank you!
Actually I don’t expect to have so many meshes, that we would need a hash library. The heavy load gets done when hashing/fingerprinting the meshes and this is proprietary.

2 Likes

Amazing! Great Style!

2 Likes

Guys,
here is the thInnIzator in full action in a commercial stuff.

I am very sorry guys, only video this time. The PG link with the source is at the top of this thread though. I had to remove the copyrighted model files from our public server after capturing the video, but the code is the same, just the model is a lot bigger.

@Deltakosh
@sebavan

Plus 50-60 FPS just by calling a single method.

Like @PirateJC use to say: How cool is this! :vulcan_salute:

Guys, thank you so much for BJS and for all your hard work to make ours a lot easier!

R.

12 Likes

#HowCoolIsThat

Awesome work @roland! Love seeing your progress develop!

3 Likes

Sorry butchering it :hugs:
Thank you very much! I’ve learned a lot from your super cool videos! Waiting impatiently for the next part of the TA’s Journey series!

Not butchering it at all! It’s perfect! LOL

Part 2 of the TA’s Journey Series is coming out either today or tomorrow!

1 Like

I’ve edited the message, it wasn’t a question :rofl: so thank you very much!

1 Like