vpenades / SharpGLTF

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

[BUG] CopyChannelsTo drops AlphaMode #139

Closed Maxxxel closed 2 years ago

Maxxxel commented 2 years ago

Bug in Title.

SharpGLTF.Schema2.Material Material = Primitive.Material;
Console.WriteLine(Material.Alpha); // ===> BLEND
MaterialBuilder MeshMaterial = new MaterialBuilder();
// Only Copy the Channels i need
Material.CopyChannelsTo(MeshMaterial, new string[4] { "BaseColor", "Normal", "MetallicRoughness", "Emissive"});
Console.WriteLine(MeshMaterial.AlphaMode); // ===> OPAQUE

Workaround:

SharpGLTF.Schema2.Material Material = Primitive.Material;
Console.WriteLine(Material.Alpha); // ===> BLEND
MaterialBuilder MeshMaterial = new MaterialBuilder();
// save the mode
SharpGLTF.Materials.AlphaMode alphaMode = (SharpGLTF.Materials.AlphaMode)partialMaterial.Alpha;
// Only Copy the Channels i need
Material.CopyChannelsTo(MeshMaterial, new string[4] { "BaseColor", "Normal", "MetallicRoughness", "Emissive"});
// set the mode
MeshMaterial.AlphaMode = alphaMode;
Console.WriteLine(MeshMaterial.AlphaMode); // ===> BLEND
vpenades commented 2 years ago

I don't see why this is a bug.

There's a few primary properties that are implicit to the material, like DoubleSide and AlphaBlend, and then there's the channels.

Channels represent a combination of an [optional] texture and an argument that usually multiplies the values of the texture if it exists, while implicit properties like AlphaBlend are at a lower level and usually require branching in the shader's implementation.

I agree that in an ideal world, a material structure could treat every property as a "channel" of sorts, but this is not the case, because MaterialBuilder is just a helper class that mimics how glTF materials work at a lower level.

In fact, glTF materials are absolutely terrible to handle because like 75% of what you see as "channels" are actually handled via glTF extensions.

Maxxxel commented 2 years ago

Makes sense 🙂