Assimp - 如何使用任何文件格式导入带有纹理的网格?
Assimp - How do you import a mesh with textures using any file format?
当我导入网格时,我得到 material,但无法访问纹理的文件名。 .mtl 文件明确显示纹理的文件名。在代码中,它显示纹理计数为 1,但文件名字段显示空字符串,而 fullPath 输出“*0”。在 mTexture 中,它确实显示了纹理文件扩展名“.png”,但没有显示纹理本身的文件名。感谢您的帮助。
if (scene->HasMaterials())
{
for (unsigned int i = 0; i < scene->mNumMaterials; ++i)
{
aiMaterial* material = scene->mMaterials[i];
aiString name;
material->Get(AI_MATKEY_NAME, name);
aiReturn texFound = scene->mMaterials[i]->GetTexture(aiTextureType_DIFFUSE, i, &name);
if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0)
{
aiString path;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS)
{
std::string fullPath = path.data;
}
}
}
}
这应该可以正常工作。您能否提供 obj-model 及其相应的 material 文件并在此处生成新的问题报告:https://github.com/assimp/assimp/issues
然后我们可以尝试调查您的示例中出了什么问题。
如果有人对我如何解决我的问题感到好奇,如下所示。在 .mtl 文件中,我必须添加 map_Kd diffusefile.png 以添加漫反射文件,以便 assimp 可以获取纹理文件。
if (scene->HasMaterials())//True when number of materials is greater than 0
{
for (unsigned int m = 0; m < scene->mNumMaterials; ++m)
{
aiMaterial* material = scene->mMaterials[m];//Get the current material
aiString materialName;//The name of the material found in mesh file
aiReturn ret;//Code which says whether loading something has been successful of not
ret = material->Get(AI_MATKEY_NAME, materialName);//Get the material name (pass by reference)
if (ret != AI_SUCCESS) materialName = "";//Failed to find material name so makes var empty
//Diffuse maps
int numTextures = material->GetTextureCount(aiTextureType_DIFFUSE);//Amount of diffuse textures
aiString textureName;//Filename of the texture using the aiString assimp structure
if (numTextures > 0)
{
//Get the file name of the texture by passing the variable by reference again
//Second param is 0, which is the first diffuse texture
//There can be more diffuse textures but for now we are only interested in the first one
ret = material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), textureName);
std::string textureType = "diff_";
std::string textureFileName = textureType + textureName.data;//The actual name of the texture file
}
}
当我导入网格时,我得到 material,但无法访问纹理的文件名。 .mtl 文件明确显示纹理的文件名。在代码中,它显示纹理计数为 1,但文件名字段显示空字符串,而 fullPath 输出“*0”。在 mTexture 中,它确实显示了纹理文件扩展名“.png”,但没有显示纹理本身的文件名。感谢您的帮助。
if (scene->HasMaterials())
{
for (unsigned int i = 0; i < scene->mNumMaterials; ++i)
{
aiMaterial* material = scene->mMaterials[i];
aiString name;
material->Get(AI_MATKEY_NAME, name);
aiReturn texFound = scene->mMaterials[i]->GetTexture(aiTextureType_DIFFUSE, i, &name);
if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0)
{
aiString path;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS)
{
std::string fullPath = path.data;
}
}
}
}
这应该可以正常工作。您能否提供 obj-model 及其相应的 material 文件并在此处生成新的问题报告:https://github.com/assimp/assimp/issues
然后我们可以尝试调查您的示例中出了什么问题。
如果有人对我如何解决我的问题感到好奇,如下所示。在 .mtl 文件中,我必须添加 map_Kd diffusefile.png 以添加漫反射文件,以便 assimp 可以获取纹理文件。
if (scene->HasMaterials())//True when number of materials is greater than 0
{
for (unsigned int m = 0; m < scene->mNumMaterials; ++m)
{
aiMaterial* material = scene->mMaterials[m];//Get the current material
aiString materialName;//The name of the material found in mesh file
aiReturn ret;//Code which says whether loading something has been successful of not
ret = material->Get(AI_MATKEY_NAME, materialName);//Get the material name (pass by reference)
if (ret != AI_SUCCESS) materialName = "";//Failed to find material name so makes var empty
//Diffuse maps
int numTextures = material->GetTextureCount(aiTextureType_DIFFUSE);//Amount of diffuse textures
aiString textureName;//Filename of the texture using the aiString assimp structure
if (numTextures > 0)
{
//Get the file name of the texture by passing the variable by reference again
//Second param is 0, which is the first diffuse texture
//There can be more diffuse textures but for now we are only interested in the first one
ret = material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), textureName);
std::string textureType = "diff_";
std::string textureFileName = textureType + textureName.data;//The actual name of the texture file
}
}