Instanced Meshes As LOD Meshes

Yo @Deltakosh

There seems to be a problem with instanced meshes as LOD meshes. You fixed this issue once before for instanced meshes coming from GLTF.

In the GLTF Parser all nodes in the the GLTF callbacks are Babylon.Mesh. When setting up LOD and the meshes you use in the add LOD level function call is actually an InstancedMesh the LOD does not work properly.

I setup a few playgrounds with a demo cliff rock mesh, that has different colors for the LOD so you can easily see the different LOD meshes when you pull the camera back.

But there is a problem setting up the exact scenario in the playground because in the playground you get InstancedMesh when call mesh.createInstance and the instanced mesh DOES NOT have a addLODLevel function like the BABYLON.Mesh does… even though if you use GLTF mesh instancing… the object is ALWAYS a Babylon.Mesh even though it might actually me an InstancedMesh and you can call addLODLevel on the instancedMesh.

Anyways… i cant seem to even setup the playground to use InstancedMesh as a LOD Level.

Here a few playground to demo LOD setup with demo rocks

Raw Sample Meshes (NO LOD SETUP)
https://playground.babylonjs.com/#ZHDEMI

LOD Setup: Loaded Meshes (Single Rock With LOD)
https://playground.babylonjs.com/#ZHDEMI#1

LOD Setup: Cloned Meshes (Cloned Single Rock With LOD)
https://playground.babylonjs.com/#ZHDEMI#2

But this instanced mesh does not work, i cant even call addLODLevel in playground

LOD Setup: Instanced Meshes (Instances AS LOD)
https://playground.babylonjs.com/#ZHDEMI#3

Can you please fix this again, we need to be able to use instanced mesh as an LOD.

As always… thanks so much :slight_smile:

Instanced meshes can’t be used as LOD but I don’t see why you would need it?

In your examples, you can simply set your lod hierarchy on cliff_lod0 as you do and create instances from cliff_lod0:

https://playground.babylonjs.com/#ZHDEMI#4

Note that I had to put the instance under the same parent than cliff_lod0 for the lod to work for the instance. An alternate way is to set scene.useRightHandedSystem = true; (as this will set an identity transformation for the root node).

In unity you can paint trees :evergreen_tree: on your terrain… trees can have lods… exported as gltf…

So the Gltf parser is acutely creating the instanced mesh

@Deltakosh had fixed this before

If your saying we can no longer do that… wow that just killed my Unity Terrain exporting :slightly_frowning_face:

I am pretty sure there is a mismatch here. Instances can only render the same geometry so they would all be the same as it happens in one draw call (there is technically no way to render in on draw different geometry in babylon)

This means that if we have LOD + instances, it probably checks only the distance to the main mesh to decide which lod to use ?

@Deltakosh could you confirm ?

Well lod system happens before rendering so I see no reason why we could not select an instance

Ok checked offline with @Deltakosh, so what should be possible is to dispatch the instances draw to several calls (per LOD defined on the main mesh) I ll check if this works correctly.

From the thread you linked in your first post, Babylon.js Playground is a PG you did that did not work before @Deltakosh made his PR. It still works in 5.0.

We have a validation test that corresponds to the latest PG you provided and that still does work: https://playground.babylonjs.com/#UAIZCS#0

Note that to my knowledge, using addLODLevel on InstancedMesh has never worked, this method has never been a method of InstancedMesh.

In unity you can paint trees :evergreen_tree: on your terrain… trees can have lods…

In Babylon.js too:

  • create tree1, tree2, tree3, tree4, … where tree2…treeX are regular meshes and will be setup as LODs for tree1
  • create thousands of instances of tree1. You will only get as many draw calls as the number of different lods that are currently displayed.

So, I’m not sure what the problem you are referring to is… Could you provide a PG that did work before (in 4.2 I guess) and that does not work in 5.0?

Regarding glTF, the meshes you are going to use as LODs should be intantiated a single time in the file. If they are instantiated more than once, the system will create them as InstancedMesh and you will get some errors because of that.

2 Likes

Thanks a ton @Evgeni_Popov at least it confirms the lod + instances behavior is still fully ok :slight_smile:

@MackeyK24 I think you mismatched the code by trying to put the lod on the instances as this had never been supported.

If your problem is because the mesh you want to use as a lod is an InstancedMesh when loaded from a glTF file, you should use mesh.sourceMesh.clone(...) as the lod instead of mesh.

1 Like

Thanks you guys for looking into this … @sebavan , @Evgeni_Popov and @Deltakosh

I will see what i can do to make this work from GLTF as well… Thanks again guys :slight_smile:

1 Like

Got it working based on my original posting…

Basically only setup lod for master mashes… I let Gltf create the instances for lod 0 only…

Works like a charm again :blush:

Thanks guys

2 Likes