(UE4 Assimp) 在运行时加载 mtl 纹理的问题 [英] (UE4 Assimp) Issues loading mtl texture at runtime

查看:121
本文介绍了(UE4 Assimp) 在运行时加载 mtl 纹理的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Assimp 在运行时通过 Unreal Engine 导入 3d Obj.由于 mtl 文件,我尝试导入纹理,我可以获得名称纹理文件,但我的问题是,如何使用它在 UTexture2D 中导入纹理并将其放入 MID 中?

if (scene->HasMaterials()){for (unsigned int m = 0; m mNumMaterials; ++m){aiMaterial* material = 场景->mMaterials[m];aiString materialName;aiReturn ret;ret = material->Get(AI_MATKEY_NAME, materialName);if (ret != AI_SUCCESS) materialName = "";//漫反射贴图int numTextures = material->GetTextureCount(aiTextureType_DIFFUSE);aiString 纹理名称;如果 (numTextures > 0){ret = material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), textureName);std::string textureType = "diffuse_";std::string textureFileName = textureType + textureName.data;//命名纹理文件}}}

谢谢.

解决方案

希望我能根据您提供的内容在一定程度上回答您的问题.

根据我在您的问题中的理解,您可以访问保存纹理的文件名(必须是完整路径)并希望将其作为 UTexture2D 导入.我以前没有在 C++ 中使用过导入,但我认为它和在蓝图中一样简单.在蓝图中有一个现有的节点可以做到这一点.看一看: 如您所见,我创建了一个带有 TextureObjectParameter 的材质.将此参数命名为您想要的名称,重要的是给它一个相关的名称,因为这是您在修改 DMI 时将引用的名称(在本例中,我将其命名为颜色").

接下来,当您想要创建此材料的 DMI 时,这是在蓝图中完成的方式: 在这张图片中,仅作为示例,我导入了我的纹理首先,然后我创建类 TestMaterial 的 DMI,并将我在原始材料类中创建的任何参数修改为我想要的(在本例中,要更改的参数是颜色",我想要它更改为我刚刚导入的纹理).您可以根据需要修改任意数量的参数,但请记住,这些参数首先必须存在于原始材质类中.但请记住,DMI 也只存在于运行时,退出游戏后所有对它们的引用都将被删除,因此请谨慎使用它们!

我真的希望我能正确理解你的问题,我很抱歉无法提供实际的 C++ 帮助.祝您的游戏开发顺利!

I'm using Assimp to import 3d Obj at runtime with Unreal Engine. I try to import textures thanks to the mtl file, I can get the name texture file, but my problem is, how to use it to import the texture in a UTexture2D and put it in a MID ?

if (scene->HasMaterials())
    {
        for (unsigned int m = 0; m < scene->mNumMaterials; ++m)
        {
            aiMaterial* material = scene->mMaterials[m];
            aiString materialName;
            aiReturn ret;

            ret = material->Get(AI_MATKEY_NAME, materialName);
            if (ret != AI_SUCCESS) materialName = "";

            //Diffuse maps
            int numTextures = material->GetTextureCount(aiTextureType_DIFFUSE);
            aiString textureName;

            if (numTextures > 0)
            {
               
                ret = material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), textureName);

                std::string textureType = "diffuse_";
                std::string textureFileName = textureType + textureName.data;//Name Texture file 
            }
        }
    }

Thank you.

解决方案

I hope I can answer your question to some extent based on what you've provided.

From what I understand in your question is that you have access to the filename (this has to be the full path) that holds your texture and want to import it as a UTexture2D. I haven't worked with importing in C++ before, but I'm assuming it's just as simple to do as in blueprints. In blueprints there's an existing node that does exactly that. Take a look: Import File as Texture 2D

Now you have to remember that importing it as such will not actually create a permanent material for you that you can reference even after resetting your game. This will just import it at runtime for the duration of your game, and delete all references to it after you exit game.

As for putting it in an MID, I'm unsure what an MID is. Do you mean DMI (Dynamic Material Instance)? If not, please clarify and hopefully I'll be of further help. If you do mean DMI, then that's also quite simple, although unfortunately I haven't worked with that in C++ either. However I'll run you through the blueprint and hopefully you can interpret that in C++ yourself.

To create a DMI, you need to have already created a material that holds the parameters that you want to then change when creating the DMI. For a very basic example, here's one I'll use for the purpose of demonstration: As you can see, I've created a material with a TextureObjectParameter. Name this parameter what you wish, it's important to give it a relevant name as this is what you will refer to when modifying your DMI (in this example case I'm naming it "color").

Next, when you want to create a DMI of this material, this is how it's done in blueprints: In this picture, just for your example, I import my texture first, then I create a DMI of class TestMaterial, and I modify whichever parameter that I have created in the original material class to what I want it to be (in this example case the parameter to change is "color" and I want it to be changed to the texture I just imported). You can modify as many parameters as you want, but remember these parameters have to exist in the first place in your original material class. Remember though, DMIs also exist just in runtime and all references to them will be deleted after you exit your game, so use them carefully!

I really hope I understood your question properly and I'm very sorry for not being able to provide actual C++ help. Best of luck going forward with your game development though!

这篇关于(UE4 Assimp) 在运行时加载 mtl 纹理的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆