# Separate mesh by loose parts

Hello,

I have a mesh that is (visually) separated in to three sections / objects. Is there a way they can be separated into meshes/submeshes?

The intention is that I can then apply different materials to different parts of this mesh.

I have made a playground here https://playground.babylonjs.com/#VKBJN#1278 to demonstrate the question.

Thank you,

You can use the `SubMesh` class to construct sub meshes:

https://playground.babylonjs.com/#VKBJN#1279

Thanks for the quick reply @Evgeni_Popov. On your (and my) example, we know already that the first subMesh is from 0 → 23, second from 24 → 47 and the last is from 48 → 71.

The question is how can we do it, automagically? Blender has a section about doing it manually here Separate Mesh/Selection – Blender Knowledgebase

1 Like

I don’t see how it can be done magically… You have a list of triangles on entry (the indices), how can you determine which triangles should be part of which mesh? There’s no notion of meshes in the list of indices.

Could you use three separate meshes and ‘join’ them with a parent?

please let me know when the ‘magically done’ option will finally be available in BJS

Let’s say that the magical solution doesn’t exist at the moment.

Please let me know if I should post another question in another thread instead. Basically the mesh with three (separated) parts above were what I extracted from a subMesh with a specific Material. What I am trying to do is to apply different materials to different parts of this (sub)mesh. Is there any different from above question?

I suppose it can be automated,
as long as the meshes are indexed one after the other in the geometry data,

then start comparing positions within epsilon (e.g. 0.001),
if indices 0,1,2 are within epsilon of indices 3,4,5 it’s part of same “submesh”,
if indices 6,7,8 are within epsilon of indices 3,4,5 OR 0,1,2 it’s part of same “submesh”,
if indices 9,10,11 are not within epsilon of 0,1,2 , 3,4,5 or 6,7,8, it’s not part of same “submesh”,
thus first submesh is from 0 to 8, next submesh starts from 9…
etc

Thanks @aWeirdo, I don’t think that distance between vertices helps here, but your response gave me a direction that I will try to see if it works.

Let’s assume that the mesh is indexed (and optimised by Blender). And epsilon is provided to determine if to vertices are at the same position. Here go the rules

123 → no block existing → block 1 (123)
134 → only one new vertex → add it to block 1 (1234)
145 → only one new vertex → add it to block 1 (12345)

567 → two new vertices → new block 2 (567)
578 → only one new vertex → add it to block 2 (5678)

Looks like my assumptions are correct. Here is the algorithm that works, at least for my simple example.

https://playground.babylonjs.com/#VKBJN#1415

1 Like