Thin instances + uv attribute weird result?

Hi,

In case of a thin instance, I just want to confirm that I do things right (I don’t):

let custom_mesh = new Mesh("name", scene);
let vd = new VertexData();
vd.positions = Float32Array.from(my_positions);
vd.normals = Float32Array.from(my_normals);
vd.applyToMesh(custom_mesh);

custom_mesh.thinInstanceSetBuffer("matrix", Float32Array.from(my_packed_matrices), 16, true);
custom_mesh.thinInstanceSetBuffer("uv", Float32Array.from(my_packed_uvs), 2, true);

the issue is that the resulting thin instanced meshes render like that:

It only happens when I load my uvs inside the thin instance. My stride of 2 should be correct. I tried with a stride of 6 because custom_mesh is a triangle (2 * 3) but same thing.

The faulty line is clearly
custom_mesh.thinInstanceSetBuffer("uv", Float32Array.from(my_packed_uvs), 2, true);

because when I do:
vd.uvs = Float32Array.from(my_packed_uvs);

I have my texture visually intelligible for the triangles, but overall incorrect (that’s normal, the same coordinates are applied to all the instances the same)

Do you see something weird in my logic?

Thanks

I separated each triangle to narrow the spectrum of possibilities.
Each triangle has its own normals and uvs arrays.

I only use 1 uv per triangle.

That code works:

let vd = new VertexData();
vd.positions = Float32Array.from(my_packed_vertices);
vd.indices = [0,1,2];
vd.normals = Float32Array.from(my_packed_normals)
vd.uvs = Float32Array.from(my_packed_uvs);
let custom_mesh = new Mesh("my_name", scene);
vd.applyToMesh(custom_mesh);
custom_mesh.thinInstanceSetBuffer("matrix", Float32Array.from(my_packed_matrices), 16, true);

This one doesn’t:

let vd = new VertexData();
vd.positions = Float32Array.from(my_packed_vertices);
vd.indices = [0,1,2];
vd.normals = Float32Array.from(my_packed_normals)
               
let custom_mesh = new Mesh("my_name", scene);
vd.applyToMesh(custom_mesh);
custom_mesh.thinInstanceSetBuffer("uv", Float32Array.from(my_packed_uvs), 2 , true)
custom_mesh.thinInstanceSetBuffer("matrix", Float32Array.from(my_packed_matrices), 16, true);

My uvs are the same and work perfectly fine (visually correct) in the first code I am not interesting in. I want to use my thin instances.
Once I use the uvs in thin instance, I get the black and white result above…

Can you share a Playground?

Sure,

I managed to ultra simplify it:

https://playground.babylonjs.com/#3C5QSK#3

To test what I am experiencing, please comment or uncomment the whole version 1/2 blocks. (only use one block at a time)

In the PG, the second version which has issue doesn’t have the same BW visual as I do but it doesn’t work either. Maybe something related to the env image…

I am missing an option somewhere.

Checked with the team, unfortunately we don’t do uvs per instance, only matrices and colors :frowning:

:fearful: :cold_sweat:

aww that sounds bad. Is it something you will enable in the future?

This is in most cases not viable, UVs are like positions inherent to the main mesh. This is not like a global color you can chose per instance. The only thing you can add would be a uv transform matrix per instance, not a dedicated set per instance.

It is not out of box.

But @Evgeni_Popov has a solution based on NodeMaterial. See his reply: Custom uv settings for each instance - #5 by Evgeni_Popov

in the following thread:

2 Likes

@slin this is as I mentioned earlier:

We can not really support a dedicated set per instance. This would also be really hard to define procedurally :slight_smile:

The only other approach would be to dynamically swap the UV index being use limiting to 6 fully different layouts.

2 Likes

@sebavan : this was one of the solution I was looking at, but the shapes imply too many UV variations and even “numberofuvs / 6” will imply a huge number of meshes.

I will play smart and carefully choose my textures. Something like noise will do great and I will add some geometry, no way around.

Back to work!

1 Like

@sebavan

Oh, just read the question again. It is the actual uv mappings @Julien1 wants to pass to thin instance?

1 Like

yes, absolutely :wink:

I already set up an alternative approach :wink:

1 Like

@Deltakosh being able to transform uv coords, per instance, when using thin instances, would be crazy useful, right?

Why not:) @Evgeni_Popov will soon have more time to work on that :wink:

(Yeah, this is a secret but we just hired @Evgeni_Popov in the core BJS team ;))

7 Likes

Should I put something on the backlog? (github issue)

:smiley:
@sebavan : the fact that you are ok for a material plugin is nice.
Will we still be able to have PBR/Physical materials?
Will we still have the 6 uvs?

(questions just out of my head)
How do you see it?