2

I've got a simple scene with a transparent wall and 2 cubes.

enter image description here

The the base color of the transparent wall in Blender is set to: RGBA = {0.8, 0.2, 0.6, 0.3}

I've exported the scene to the gltf format and loaded it using assimp.

My material extraction code looks like this (it may be kinda messy, because I've been playing around with it to find the solution):

void ExtractMaterial(const aiScene* scene)
{
    std::unordered_map<std::string, std::string> customProperties;
    if (scene->HasMaterials()) {
        for (uint32_t i = 0; i < scene->mNumMaterials; i++) {
            auto aiMaterial = scene->mMaterials[i];
            auto aiMaterialName = aiMaterial->GetName();

            aiColor4D aiColor;
            if (aiMaterial->Get(AI_MATKEY_COLOR_DIFFUSE, aiColor) == AI_SUCCESS) {
                std::cout << std::endl;
            }

            float opacity = 1.0f;
            if (aiMaterial->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS) {
                std::cout << std::endl;
            }

            float transFac = 1.0f;
            if (aiMaterial->Get(AI_MATKEY_TRANSPARENCYFACTOR, transFac) == AI_SUCCESS) {
                std::cout << std::endl;
            }

            float transCol = 1.0f;
            if (aiMaterial->Get(AI_MATKEY_COLOR_TRANSPARENT, transCol) == AI_SUCCESS) {
                std::cout << std::endl;
            }

            for (int p = 0; p < aiMaterial->mNumProperties; p++) {
                aiMaterialProperty* pt = aiMaterial->mProperties[p];
                switch (pt->mType) {
                    case aiPTI_String: {
                        const char* data = pt->mData;
                        customProperties[pt->mKey.C_Str()] = data;
                    }
                    break;
                    case aiPTI_Float: {
                        std::stringstream ss;
                        ss << *(float*)pt->mData;
                        customProperties[pt->mKey.C_Str()] = ss.str();

                        if (strcmp(pt->mKey.C_Str(), "$mat.opacity") == 0) {
                            float num = *(float*)pt->mData;
                            if (num != 1.0) {
                                std::cout << std::endl;
                            }
                        }
                        else if (strcmp(pt->mKey.C_Str(), "$mat.gltf.alphaMode") == 0) {
                            float mode = *(float*)pt->mData;

                            std::cout << std::endl;
                        }
                    }
                    break;
                }
            }
        }
    }
}

I set breakpoints in places where std::cout << std::endl; is written. std::cout << std::endl; is to enforce the debugger to execute the code in these places.

aiColor4D aiColor; of the wall is the same as in Blender except for the alpha which is 1.0f (should be 0.3f).

The rest variables opacity, transFac, transCol and the customProperties map are just for me to check out which one is the actual alpha value. None of them is.

How do I get the alpha value of the wall?

The material should be exported fine because the default Windows 10 3D mesh explorer works fine with it.

BrodaJarek3
  • 311
  • 1
  • 9

0 Answers0