How do I load roughness/metallic map with Assimp using gltf format?

Viewed 3788

I try to load a scene that's in gltf format. It has metallic and roughness textures (not embedded). Now I want to load the model with the specified textures using Assimp. I retrieve the textures using GetTexture(), but aiTextureType doesn't have members for roughness / metallic.

So the question: How do I import them? Since Assimp supports gltf, it has to support physically based material maps as well somehow.

Thanks

4 Answers

There are generic PBR material properties since Assimp 5.1.0 that work even for non-glTF files:

// for some formats (like glTF) metallic and roughness may be the same file
aiString fileBaseColor, fileMetallic, fileRoughness;
material->GetTexture(AI_MATKEY_BASE_COLOR_TEXTURE, &fileBaseColor);
material->GetTexture(AI_MATKEY_METALLIC_TEXTURE, &fileMetallic);
material->GetTexture(AI_MATKEY_ROUGHNESS_TEXTURE, &fileRoughness);

Full list here: https://github.com/assimp/assimp/blob/v5.1.4/include/assimp/material.h#L972


Old answer: (deprecated since Assimp 5.1.0)

For GLTF PBR materials you can use the material keys defined in pbrmaterial.h

#include <assimp/pbrmaterial.h>

aiString fileBaseColor, fileMetallicRoughness;
material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_TEXTURE, &fileBaseColor);
material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &fileMetallicRoughness);

So if anyone has the same problem: GetTexture(aiTextureType_UNKNOWN) returns a path to the pbr texture.

So there's no better alternative than getting aiTextureType_UNKNOWN (or most likely AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE) since the mettalic and roughness textures are specified in the same material property metallicRoughnessTexture in the gltf? Like this:

  "images": [
    {
      "uri": "textures/DefaultMaterial_metallicRoughness.png"
    },
    {
      "uri": "textures/DefaultMaterial_baseColor.jpeg"
    },
    {
      "uri": "textures/DefaultMaterial_normal.png"
    }
  ],
  "materials": [
    {
      "doubleSided": true,
      "emissiveFactor": [
        0,
        0,
        0
      ],
      "name": "DefaultMaterial",
      "normalTexture": {
        "index": 2,
        "scale": 1,
        "texCoord": 0
      },
      "pbrMetallicRoughness": {
        "baseColorFactor": [
          1,
          1,
          1,
          1
        ],
        "baseColorTexture": {
          "index": 1,
          "texCoord": 0
        },
        "metallicFactor": 1,
        "metallicRoughnessTexture": {
          "index": 0,
          "texCoord": 0
        },
        "roughnessFactor": 0.86756859760000005
      }
    }
  ],

I tried:

material->GetTexture(AI_MATKEY_METALLIC_TEXTURE, &fileMetallic);
material->GetTexture(AI_MATKEY_ROUGHNESS_TEXTURE, &fileRoughness);

with assimp 5.2.2, but it fails with my glTF test file.

Related