Convert mesh to stl/obj/fbx in runtime [closed]

时光毁灭记忆、已成空白 提交于 2019-12-19 11:26:17

问题


I am looking for a way to export a mesh into stl/obj/fbx format at runtime and save it on local files of an Android phone.

How do I do this? I am willing to use a plugin(free/paid) if it exist.


回答1:


This is really complicated since you have to read the specifications for each each format (stl/obj/fbx) and understand them in order to make one yourself. Luckily, there are many plugins out there already that can be used to export Unity mesh to stl, obj and fbx.

FBX:

UnityFBXExporter is used to export Unity mesh to fbx during run-time.

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:

For obj, ObjExporter is used.

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:

You can use the pb_Stl plugin for STL format.

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);
}


来源:https://stackoverflow.com/questions/46733430/convert-mesh-to-stl-obj-fbx-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!