General questions about multimaterials

I want to apply a MultiMaterial to a mesh i created in this post/playground.

I understand the concept of multi materials and i am learning from this example and this example. You can see my results in this playground.

My issues are:

  1. When this is the signature of the constructor:
    new BABYLON.SubMesh(materialID, verticesStart, verticesCount, indexStart, indexCount, mesh);
    I don’t fully know what to put in the indexStart and indexCount parameters. Is it the index for the vertex (like: Hey SubMesh, you start from the fourth vertex to the sixteenth therefore indexStart = 4 and indexCount = 12) or is it the real buffer values, which the would be the values x 3 because e.g. the positions buffer is three times the vertices count.
    To be honest, for me it doesn’t look that complicated, but i am getting strange results (take a look at the playground) and therefore i am confused. An example why i am also confused: In this playground multimaterial example the sphere is divided in three submeshes. How can it come that the last submeshes’ index starts at 1800 and has the size of 2088 but the vertices count is 703 and therefore the maximum buffer size should be 703*3 = 2109?!

  2. (This is only an observation, not really hindering me from texturing my lovely wall)
    By creating a SubMesh with new SubMesh, it is pushed into mesh.subMeshes internally, which cannot be seen from the programmers perspective. If you console.log(sphere.subMeshes); in the multimaterial example, you’ll see that the subMeshes array is twice the size of the createdSubmeshes.

Thanks for any hint and/or enlightenment.
Have a nice weekend!
Mamu

P.S.
Read Article and Posts about multimaterials:
Discussion about the second and third parameter
A blog post about the sphere example

Hi Mamu,

I think this is coming from a mix-up between vertices and indices. Vertices describe positions in space, but it’s only the indices that determine how those vertices are grouped together to create triangles. By providing the SubMesh() method with a starting index and an index count, you’re actually telling it which part of the index buffer—not the vertex buffer—you want to constitute your sub-mesh. In short, you’re grouping triangles (i.e., triplets of indices), not vertices.

In the example Playground you referenced, the sphere only has 703 vertices, as you pointed out. However, those vertices are referenced through the index buffer to make 1296 face. Each face is a triangle, and each triangle is a triplet of vertices; 1296 * 3 = 3888, which is the same value as the last sub-mesh’s start index plus index count.

Regarding your own Playground, I wish I could help more directly, but I’m not exactly sure how it’s supposed to look. :upside_down_face: Looks like a cool use case, though. Hopefully the above explanation can help shed some light on why you’re not seeing the results you expect. Good luck!

1 Like

The other things that multi-materials / submeshes implies is that the:

  • Indices for each submesh must be contigious for them to be expressed as a range.
  • Indices must be duplicated for vertices which participate in more than one material.

These kind of restrictions mean that they are not generated by hand very much, but by exporters of other programs.

1 Like

Hi @syntheticmagus , hi @JCPalmer!

I got the knot out of my head and made it work.
Thanks syntheticmagnus for your explanation of the difference between vertices and indices.
JCPalmer, luckily the parts i want to separate are also lying one after another in the buffer (even after using csg). - Luckily!

Have a nice week!