如何在Unity运行时加载FBX文件?

6

我正在进行一个Hololens项目。我的上司说,在运行时加载fbx文件(我们使用Unity)。我们的fbx文件会在运行时更改,因此我们必须重新加载它(fbx是从3dMAx转换而来)。

我尝试使用AssetBundle,但我无法在没有Unity或Unity编辑器模式的情况下创建Assetbundle文件。据我所知,只有资源(在“项目”选项卡中)可以将资产插入到Assetbundle中。如果我能够通过fbx文件在运行时构建Assetbundle文件,并且不需要使用Unity项目,那就没问题了。

总之:我需要一种在没有Unity资源的情况下加载fbx的方法。如果我可以将fbx资产在运行时制作为Assetbundle文件(fbx不属于项目选项卡),那就可以了。


编辑)

使用Trilib资产是最好的方式。我尝试制作自己的FBX加载程序,但这太困难且工作量大。我认为Trilib并非完美,但它是最好的。


1
如何在Unity3d构建时运行时导入资源 - Nico Schertler
你可以使用http://www.assimp.org/,但是它在一段时间前出现了一些问题,我最近没有使用过。https://dev59.com/9VcP5IYBdhLWcg3wyc3W - Everts
4个回答

7

1
这是有史以来最好的解决方案,你可以在服务器端使用Node.js C++扩展轻松将FBX转换为gltf https://github.com/cyrillef/FBX-glTF - Yaakov5777

4
你可以阅读以下内容: 如何在运行时轻松导入FBX文件:简而言之:不行。Unity不支持运行时导入/加载任何模型格式,唯一的例外是AssetBundles。你的选项基本上是:
- 使用AssetBundles。 - 为你所需的Unity格式找到一个导入器。 - 自己编写这样的导入器。 - 查找一个功能请求/自己编写一个请求UT添加运行时模型加载程序。 如何将3DS/FBX模型转换为资源包并在下载后运行时转换?:是否有一种工具可以将3D模型转换为资源包?或者可以在运行时进行转换吗?因为每个Unity API都是仅供编辑器插件使用的,所以无法在运行时转换。
我为这个问题搜索了许多网站。最终,我建议您使用此资产。它不是免费的,但很值得。它可以为您节省大量时间。 trilib-unity-model-loader 希望这可以帮到你。

3

Unity无法直接加载fbx文件,但是可以在运行时加载obj文件,您可以将fbx文件转换为obj文件。

  1. 使用命令行脚本将fbx转换为obj文件。
  2. 在Unity运行时加载obj文件。

将fbx / dae转换为obj的一种方法是使用Blender命令行,因此您需要安装Blender(支持平台:Linux,Mac,Windows)。 创建此python文件:

Original Answer翻译成"最初的回答"。
import bpy
import sys
for obj in bpy.data.objects:
    if (obj.name == "Lamp") | (obj.name == "Camera") | (obj.name == "Cube"):
        obj.select = False
    else:
        obj.select = True
argv = sys.argv
argv = argv[argv.index("--") + 1:]
inputpath = argv[0]
outputpath = argv[1]
bpy.ops.import_scene.fbx(filepath=inputpath, axis_forward='-Z', axis_up='Y', directory="", filter_glob="*.fbx", ui_tab='MAIN', use_manual_orientation=False, global_scale=1, bake_space_transform=False, use_custom_normals=True, use_image_search=True, use_alpha_decals=False, decal_offset=0, use_anim=True, anim_offset=1, use_custom_props=True, use_custom_props_enum_as_string=True, ignore_leaf_bones=False, force_connect_children=False, automatic_bone_orientation=False, primary_bone_axis='Y', secondary_bone_axis='X', use_prepost_rot=True)
bpy.ops.export_scene.obj(filepath=outputpath, check_existing=False, axis_forward='-Z', axis_up='Y', filter_glob="*.obj;*.mtl", use_selection=True, use_animation=False, use_mesh_modifiers=True, use_mesh_modifiers_render=False, use_edges=True, use_smooth_groups=False, use_smooth_groups_bitflags=False, use_normals=True, use_uvs=True, use_materials=True, use_triangles=False, use_nurbs=False, use_vertex_groups=False, use_blen_objects=True, group_by_object=False, group_by_material=False, keep_vertex_order=False, global_scale=1, path_mode='COPY')
print("finished!")

Run this file in shell:

/your blender path/blender --background --python /your python path/convert.py -- /your input path/xxx.fbx /your output path/xxx.obj

最初的回答:

最后,使用这个插件在运行时加载obj文件:     https://assetstore.unity.com/packages/tools/modeling/runtime-obj-importer-49547

更多关于使用blender命令行导入和导出文件的信息:https://blender.stackexchange.com/questions/16563/how-can-i-run-blender-from-the-command-line-to-export-and-import-models


0
你可以尝试使用Trilib。这是Unity Asset商店中提供的一个插件,我个人在项目中使用过,非常喜欢它。
它支持各种文件格式(OBJ、FBX、PLY、GLTF),并且可以导入几何形状、材质、纹理、动画甚至混合形状等所有内容。
兼容Android、iOS、WebGL和桌面平台。
以下是链接,以便您更轻松地访问:https://assetstore.unity.com/packages/tools/modeling/trilib-2-model-loading-package-157548

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接