在运行时将网格转换为 stl/obj/fbx

Convert mesh to stl/obj/fbx in runtime

我正在寻找一种在运行时将网格导出为 stl/obj/fbx 格式并将其保存在 Android phone.

的本地文件中的方法

我该怎么做?如果有插件(free/paid),我愿意使用。

这真的很复杂,因为您必须阅读每种格式的规范 (stl/obj/fbx) 并理解它们才能自己制作。幸运的是,已经有许多插件可用于将 Unity 网格导出为 stl、obj 和 fbx。

FBX:

UnityFBXExporter 用于在 运行 时间内将 Unity 网格导出到 fbx。

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel"+ ".fbx");

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }

    FBXExporter.ExportGameObjToFBX(objMeshToExport, path, true, true);
}

OBJ:

对于obj,使用了ObjExporter

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel" + ".obj");

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }

    MeshFilter meshFilter = objMeshToExport.GetComponent<MeshFilter>();
    ObjExporter.MeshToFile(meshFilter, path);
}

STL:

您可以使用 pb_Stl STL 格式插件。

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel" + ".stl");

    Mesh mesh = objMeshToExport.GetComponent<MeshFilter>().mesh;

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }


    pb_Stl.WriteFile(path, mesh, FileType.Ascii);

    //OR
    pb_Stl_Exporter.Export(path, new GameObject[] { objMeshToExport }, FileType.Ascii);
}