bool importMaterial()

in libraries/hvvr/samples_shared/model_import_fbx.cpp [314:395]


bool importMaterial(ImportState& state, model_import::Mesh& mesh, FbxSurfaceMaterial* pFbxMaterial) {
    if (pFbxMaterial == nullptr) {
        printf("error: Material was null!\n");
        return false;
    }

    hvvr::SimpleMaterial material = {};
    if (pFbxMaterial->GetClassId().Is(FbxSurfacePhong::ClassId)) {
        FbxSurfacePhong* pPhong = (FbxSurfacePhong*)pFbxMaterial;

        FbxDouble3 Diffuse = pPhong->Diffuse.Get();
        FbxDouble DiffuseFactor = pPhong->DiffuseFactor.Get();
        FbxDouble3 Specular = pPhong->Specular.Get();
        FbxDouble SpecularFactor = pPhong->SpecularFactor.Get();
        FbxDouble3 Emissive = pPhong->Emissive.Get();
        FbxDouble EmissiveFactor = pPhong->EmissiveFactor.Get();
        FbxDouble3 TransColor = pPhong->TransparentColor.Get();
        FbxDouble TransFactor = pPhong->TransparencyFactor.Get();
        FbxDouble Shininess = pPhong->Shininess.Get();

        float transparency = float((TransColor[0] + TransColor[1] + TransColor[2]) / 3.0 * TransFactor);
        // Undo FBX glossiness to specular power mapping (n = 2^(g*10)) to get a 0..1 gloss value
        float glossiness = float(log2(fmax(Shininess, 1.0)) / 10.0);

        uint32_t diffuseID = importTexture(state, GetTextureFromProperty(&pPhong->Diffuse), true);
        uint32_t emissiveID = importTexture(state, GetTextureFromProperty(&pPhong->Emissive), true);
        uint32_t specularID = importTexture(state, GetTextureFromProperty(&pPhong->Specular), true);
        uint32_t glossinessID = importTexture(state, GetTextureFromProperty(&pPhong->Shininess), true);

        material.emissive = fbxColorConvert(Emissive, EmissiveFactor);
        material.diffuse = fbxColorConvert(Diffuse, DiffuseFactor);
        material.specular = fbxColorConvert(Specular, SpecularFactor);
        material.glossiness = glossiness;
        material.opacity = 1.0f - transparency;

        if (emissiveID != hvvr::SimpleMaterial::badTextureIndex) {
            material.textureIDsAndShadingModel = hvvr::SimpleMaterial::buildShadingCode(
                hvvr::ShadingModel::emissive, emissiveID, hvvr::SimpleMaterial::badTextureIndex,
                hvvr::SimpleMaterial::badTextureIndex);
        } else {
            material.textureIDsAndShadingModel =
                hvvr::SimpleMaterial::buildShadingCode(hvvr::ShadingModel::phong, diffuseID, specularID, glossinessID);
        }
    } else if (pFbxMaterial->GetClassId().Is(FbxSurfaceLambert::ClassId)) {
        FbxSurfaceLambert* pLam = (FbxSurfaceLambert*)pFbxMaterial;

        FbxDouble3 Diffuse = pLam->Diffuse.Get();
        FbxDouble DiffuseFactor = pLam->DiffuseFactor.Get();
        FbxDouble3 Emissive = pLam->Emissive.Get();
        FbxDouble EmissiveFactor = pLam->EmissiveFactor.Get();
        FbxDouble3 TransColor = pLam->TransparentColor.Get();
        FbxDouble TransFactor = pLam->TransparencyFactor.Get();

        float transparency = 1.0f - float((TransColor[0] + TransColor[1] + TransColor[2]) / 3.0 * TransFactor);

        uint32_t diffuseID = importTexture(state, GetTextureFromProperty(&pLam->Diffuse), true);
        uint32_t emissiveID = importTexture(state, GetTextureFromProperty(&pLam->Emissive), true);
        uint32_t specularID = hvvr::SimpleMaterial::badTextureIndex;
        uint32_t glossinessID = hvvr::SimpleMaterial::badTextureIndex;

        material.emissive = fbxColorConvert(Emissive, EmissiveFactor);
        material.diffuse = fbxColorConvert(Diffuse, DiffuseFactor);
        material.specular = hvvr::vector4(0, 0, 0, 0);
        material.glossiness = 1.0f;
        material.opacity = 1.0f - transparency;

        if (emissiveID != hvvr::SimpleMaterial::badTextureIndex) {
            material.textureIDsAndShadingModel = hvvr::SimpleMaterial::buildShadingCode(
                hvvr::ShadingModel::emissive, emissiveID, hvvr::SimpleMaterial::badTextureIndex,
                hvvr::SimpleMaterial::badTextureIndex);
        } else {
            material.textureIDsAndShadingModel =
                hvvr::SimpleMaterial::buildShadingCode(hvvr::ShadingModel::phong, diffuseID, specularID, glossinessID);
        }
    } else {
        printf("error: Unknown material type!\n");
        return false;
    }

    mesh.data.materials.emplace_back(std::move(material));
    return true;
}