Hi guys!
EDIT: PG updated once again, updated URLs to assets: https://playground.babylonjs.com/#QZA92W#156
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
Cheers!
R.