Set Material alphaMode properties to glTF/GLB 2 export
Issue #49
resolved
When I add texture from a PNG file, we don’t have the background transparency after export to glTF/GLB 2.0.
I edited directly the glTF file and add the key value "alphaMode": "BLEND"
to material and after that, the texture is ok, with transparency.
The material definition:
"materials": [
...
{
"alphaMode": "BLEND",
"pbrMetallicRoughness": {
"metallicFactor": 0,
"roughnessFactor": 1,
"baseColorTexture": {
"index": 0
}
},
"name": "Material_Texture_2"
}
],
The Assimp.Material does not provide a property to set the Material alphaMode.
Below, the way I create the material with texture:
We can check the glTF Alpha Coverage Specification for more details.
Comments (4)
-
repo owner -
reporter Thanks for the first line you wrote, it gave me a light to follow!
for other people who need this option, follow the workaroud done:
Dictionary<String, MaterialProperty> m_properties = (Dictionary<String, MaterialProperty>)GetInstanceField(materialTexture, "m_properties"); MaterialProperty matProp = new MaterialProperty("$mat.gltf.alphaMode", "BLEND"); m_properties.Add(matProp.FullyQualifiedName, matProp); FieldInfo mProperties = typeof(Assimp.Material).GetField("m_properties", BindingFlags.Instance | BindingFlags.NonPublic); mProperties.SetValue((object)materialTexture, (object)m_properties); ... private static object GetInstanceField<T>(T instance, string fieldName) { BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; FieldInfo field = typeof(T).GetField(fieldName, bindFlags); return field.GetValue(instance); }
-
repo owner I was thinking along the lines of:
materialTexture.AddProperty(new MaterialProperty("$mat.gltf.alphaMode", "BLEND"));
All the methods for accessing the material property dictionary are public APIs, no reflection is necessary.
-
repo owner - changed status to resolved
- Log in to comment
It is important to note that those properties are for convenience, all of them use the (public) AddProperty/GetProperty/HasProperty methods and are just key-value pairs.
On the 5.0 branch I think this is the last thing I wanted to sort out before doing a RC release…I hope to make some time this week since I’m getting requests for getting that on nuget. I’ve been buy with other projects.
The GLTF PBR keys can be found here:
https://github.com/assimp/assimp/blob/master/include/assimp/pbrmaterial.h
What is confusing (to me at least) in the 5.0 version of native Assimp is they define GLTF-specific keys here, and then “generic” PBR texture keys in (search for PBR Material):
https://github.com/assimp/assimp/blob/master/include/assimp/material.h
I don’t know why they chose to have it this way.