Mesh instances with different UVs not working

I’m working on a lightmap based workflow where instanced meshes of a GLB share baked lightmaps each with its own UV coordinates in the lightmap atlas. As a first step, I wanted to get it working in playground, but for some reason the UV2Kind vertex buffers get shared between all the instances causing the same coordinates to be applied to them (see console log).

I did notice https://www.babylonjs-playground.com/#64K1I1#2 doing something similar but it uses custom pbr materials and I’d like to use the material shipped with the GLB.

P.S. The x axis was flipped on the instance in spite of parenting the mesh to the same parent. I’ve hardcoded the flip on X here after parenting but not sure if that’s the right way of doing it. Would be nice to include this in the docs here: Instances | Babylon.js Documentation

Instances share the same vertex buffers than their master mesh, you can’t have different vertex buffers on a per instance basis. You will have to create regular meshes if you want to do this (use clone + makeGeometryUnique to make sure the mesh can use different vertex buffers):

Also, to avoid scaling X by -1, you should use mesh.parent = ... instead of mesh.setParent(...), because the latter is updating the mesh matrix in the process.

@Evgeni_Popov we are trying to limit the draw calls in our scene (currently > 200 which is causing FPS to drop. Our scene has many instances of the exact same GLB placed in different locations. Because it is in different locations each instance’s lightmap is stored in the same file with an offset). Cloning will help with the memory pressure but the draw calls wont be affected, right?

@Evgeni_Popov Is there a way to achieve this by using NME where the UV offsets per instance can be manipulated by the shader at run time? I also saw custom buffers, but they seem to only hold 1 value per instance. In my case, I’d like that value to be an offset.

I’m trying real hard not to redesign the scene by taking out the instances, but looks like I will be forced to do that unless I find a way to apply the lightmaps separately.

Yes, in that case you should use a single texture atlas and pass the (u,v) offset into this texture to your instance. That’s basically what the PG you linked does (https://www.babylonjs-playground.com/#64K1I1#2).

If you want to be able to do it with a regular standard / PBR material, you should create a material plugin.

See this answer from @Blake, who is doing it for instanced meshes, but it would be mostly the same for thin instances:

1 Like

That worked like a charm! Thanks @Evgeni_Popov.