Set Material alphaMode properties to glTF/GLB 2 export

Issue #49 resolved
Anderson Luiz Nichele created an issue

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)

  1. Nicholas Woodfield repo owner

    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.

  2. Anderson Luiz Nichele 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);
            }
    

  3. Nicholas Woodfield 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.

  4. Log in to comment