vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
458 stars 72 forks source link

Material subtitution #140

Closed Omega1975 closed 2 years ago

Omega1975 commented 2 years ago

Can I upload a glb file and replace one of its materials with a material uploaded from another glb file?

Orignal file Bea_sedia_schienale.glb material file wood_brown.glb wood_brown.zip

vpenades commented 2 years ago

The easiest way I can imagine is this:

Notice that these steps don't automatically remove the replaced material from the model2 resources.

Omega1975 commented 2 years ago

Ok. Great! But how can i remove the material from Target? Below my code:

public bool ModifyTexture(SharpGLTF.Schema2.ModelRoot Targhet, String glbMatFile) { try { MaterialBuilder mb = null; SharpGLTF.Schema2.ModelRoot MaterialGlb = SharpGLTF.Schema2.ModelRoot.Load(glbMatFile); foreach (Material lm in MaterialGlb.LogicalMaterials) { mb=lm.ToMaterialBuilder(); break; }

            Material newm =Targhet.CreateMaterial(mb);  
            foreach (Mesh m in Targhet.LogicalMeshes ) {
                foreach (var p in m.Primitives) {
                    p.Material = newm;
                }                        
            }
            return true;
        }
        catch (Exception ex)
        {               
            return false;
        }            
    }
vpenades commented 2 years ago

how can i remove the material from Target?

It's a question that I get a lot: How to remove a mesh, how to remove a material, etc.

glTF is not a format designed for general edition. It's like a ZIP archive, if you want to remove a file from the archive, the whole archive is rebuilt.

So right now, the only way to "remove things" from glTF is to rebuild/copy all the elements of a model into a new model, minus the elements you actually want to remove.

Right now the best solution I can think of to "remove things" from a model is to convert it to a SceneBuilder, which is more editable, do the changes there, and convert back to a model.

In the future I might add some limited support to "remove things", but it would have the shape of a general ".RemoveUnusedResources()"

Omega1975 commented 2 years ago

A feature for removing unused resources would be great. Thanks for the quick support. Have a good day.