Some Question of Instanced Mesh

Hi guys!
I meet some trouble about InstancedMesh! I load model from gltf, and here is my model construct:


Model contains two children:(items:store root instanced mesh,floor: store all Room);
each room contain Collider(Collider Mesh), Item(store transformNode to create InstancedMesh),wall(Wall Mesh).

do not create anything: 65 drawcall
mesh.clone : 809 drawcall
mesh.createInstance:319 drawcall
it only has 22 root instance meshes, but drawcall increase 254


I only enable 4 roots in the rect,(4X1 mesh + 24X2 mesh +20X3 mesh +12X1mesh):total drawcall 72,increase 7, it means that this 4 roots work correctly.
but I enable the 5th root(44X3 mesh): total drawcall 105, increase 33 = 3X11 work wrong
I enable the 6th root(18X5 mesh): total drawcall 160, increase 55 = 5X11 work wrong
I enable the 7th root(4X1 mesh): total drawcall 163, increase 3 ???
I enable the 10th root(33X3 mesh): total drawcall 166, increase 3 work correctly


InstanceNode(node👇)
image

Sorry, I don’t really understand what’s your problem…

You should try to setup a repro in the Playground that clearly shows the problem and what you would expect so that we can have a look.

A shot in the dark: try to set scene.useRightHandedSystem = true just after scene creation and see if it solves the problem, whatever it is.

3 Likes

Hi Evgeni!
I create a playGround https://www.babylonjs-playground.com/#L1F71C#18,
line 54 to show/hide instance mesh
line 89&90 clone mesh line 91 createInstance
line 47 to set instance root (with some error)
1 mesh
DrawCall:hide:64 show(instance):171 show(clone):276
line 50 to set instance root (work correctly)
3 meshes
DrawCall:hide:64 show(instance):67 show(clone):163

@Moriy this is amazingly complex to troubleshoot and I wonder if you could provide a simple repro ?

I still do not understand the steps to repro and such :frowning:

Hi, I create 3 PG,
this is correct instance mesh Babylon.js Playground
this is error instance mesh Babylon.js Playground
this is hide instance mesh Babylon.js Playground


PG1 and PG2 are different in Line 47 and Line 50, you can see the different of DrawCall in the inspector

PG1


PG2

The “&& shelf 1 ##33” and “&& work chair 1 ##212” meshes have a number of child mesh (respectively 34 and 213). So, when you disable one, the children won’t get drawn and the number of draw calls is decreased. The number of draw calls that is saved depends on the number of children and if there are instance meshes in those children.

Hi, only the Lod0 node is the root of Instance, else are all son instances createInstance by meshes in node Lod0. I set them parent to Lod0’s parent is only for easy to hide.

https://www.babylonjs-playground.com/#1C6X9I#3
https://www.babylonjs-playground.com/#2M1XMH#1
you can see this two PG


&& work chair 1 ##212 has 212 child instances
&& shelf 1 ##33 has 33 child instances

You have more draw calls than expected because some of the parent nodes of your instances have negative scaling, which inverts the determinant of the world matrix, which in the end prevents to batch the instance.

To demonstrate this, I have applied a Math.abs on the x/y/z scaling values of the nodes, and you can see you now have a single additional draw calls for all the instances:

https://www.babylonjs-playground.com/#2M1XMH#2

2 Likes

Ohhhhh, it works!!! Thank you very much!!!