vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
467 stars 75 forks source link

Duplicate Materials on Export #54

Closed tomspilman closed 4 years ago

tomspilman commented 4 years ago

On creating the meshes we're careful to share MaterialBuilders when creating meshes. We basically have one material per-texture. Later when we export we find duplicate materials with the same name...

image

Is there a way to keep it from doing this?

tomspilman commented 4 years ago

@vpenades - I hack things by doing this:

image

... but likely that is a bad solution.

The issue is each call i make to add a mesh to a node looks like this:

node.WithMesh(root.CreateMesh(mesh));

... each of these calls can end up creating duplicate materials in the ModelRoot as it never checks if the material already exists.

vpenades commented 4 years ago

The Schema2 namespace creation methods are quite explicit and low level, and it is expected that you build some minimum infrastructure over them.

That's why there's also a root.CreateMeshes(...);

CreateMeshes is able to find all the shared materials in the input meshes and coalesce them.

Anyway, I would suggest you to use Toolkit's SceneBuilder to create your meshes; it is easier to follow than the glTF DOM, and has additional optimizations when converted to schema2 glTF DOM.

tomspilman commented 4 years ago

That's why there's also a root.CreateMeshes(...); CreateMeshes is able to find all the shared materials in the input meshes and coalesce them.

I see... so my mistake is not building all the meshes in one call... then after that doing the node association.

Anyway, I would suggest you to use Toolkit's SceneBuilder to create your meshes;

I didn't realize i wasn't using the recommended path. There seems to be several ways to do things and not every clear guidance as to which way is best.

Thanks for clearing this up!