glTF 2.0: Assign default material to meshes with no material reference.

fixes  #1691
pull/1692/head
Axel Wefers 2018-01-08 14:57:55 -08:00
parent 89a62edb9a
commit dc79b572cf
1 changed files with 66 additions and 53 deletions

View File

@ -211,15 +211,9 @@ inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset&
} }
} }
void glTF2Importer::ImportMaterials(glTF2::Asset& r) static aiMaterial* ImportMaterial(std::vector<int>& embeddedTexIdxs, Asset& r, Material& mat)
{ {
mScene->mNumMaterials = unsigned(r.materials.Size()); aiMaterial* aimat = new aiMaterial();
mScene->mMaterials = new aiMaterial*[mScene->mNumMaterials];
for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
aiMaterial* aimat = mScene->mMaterials[i] = new aiMaterial();
Material& mat = r.materials[i];
if (!mat.name.empty()) { if (!mat.name.empty()) {
aiString str(mat.name); aiString str(mat.name);
@ -268,6 +262,21 @@ void glTF2Importer::ImportMaterials(glTF2::Asset& r)
SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.specularGlossinessTexture, aimat, aiTextureType_SPECULAR); SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.specularGlossinessTexture, aimat, aiTextureType_SPECULAR);
} }
return aimat;
}
void glTF2Importer::ImportMaterials(glTF2::Asset& r)
{
const unsigned int numImportedMaterials = unsigned(r.materials.Size());
Material defaultMaterial;
mScene->mNumMaterials = numImportedMaterials + 1;
mScene->mMaterials = new aiMaterial*[mScene->mNumMaterials];
mScene->mMaterials[numImportedMaterials] = ImportMaterial(embeddedTexIdxs, r, defaultMaterial);
for (unsigned int i = 0; i < numImportedMaterials; ++i) {
mScene->mMaterials[i] = ImportMaterial(embeddedTexIdxs, r, r.materials[i]);
} }
} }
@ -479,6 +488,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
if (prim.material) { if (prim.material) {
aim->mMaterialIndex = prim.material.GetIndex(); aim->mMaterialIndex = prim.material.GetIndex();
} }
else {
aim->mMaterialIndex = mScene->mNumMaterials - 1;
}
} }
} }