C# 在运行时分析Unity中超过65k个顶点的网格

C# 在运行时分析Unity中超过65k个顶点的网格,c#,unity3d,runtime,mesh,C#,Unity3d,Runtime,Mesh,我正在尝试使用Unity和C#在运行时加载OBJ模型。我正在使用Unity的Wiki解析器“FastOBJImporter”来解析OBJ文件。 我无法加载超过65534个顶点的网格,因为它是一个单位限制() 我的想法是将一个大网格路径传递给FastOBJImporter,并生成多个顶点少于65k的游戏对象,以便加载更大的模型 有人知道如何安全地修改FastOBJimporter以返回子网格列表而不是大网格吗?欢迎任何其他解决方案/想法。只需使用STL模型文件即可满足您的需求。每次到达65k顶点时

我正在尝试使用Unity和C#在运行时加载OBJ模型。我正在使用Unity的Wiki解析器“FastOBJImporter”来解析OBJ文件。 我无法加载超过65534个顶点的网格,因为它是一个单位限制()

我的想法是将一个大网格路径传递给FastOBJImporter,并生成多个顶点少于65k的游戏对象,以便加载更大的模型

有人知道如何安全地修改FastOBJimporter以返回子网格列表而不是大网格吗?欢迎任何其他解决方案/想法。

只需使用STL模型文件即可满足您的需求。每次到达65k顶点时,它都会将网格拆分为子网格,从而导入模型,无论模型有多大。STL文件可以转换成OBJ文件,所以,我想,使用一个简单的转换器,或者修改脚本,就可以做到这一点

下面的代码(我不接受代码)

#杂注警告禁用0219
使用UnityEngine;
使用系统文本;
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
使用制度;
使用UnityEngine.UI;
使用UnityEngine.EventSystems;
使用系统线程;
名称空间Parabox.STL
{
/*STL文件的导入方法*/
公共类STL_ImportScript:单一行为
{
每个网格的最大面常数=65535/3;
类刻面
{
公共向量3正常;
公共向量3 a、b、c;
公共重写字符串ToString()
{
返回string.Format(“{0:F2}:{1:F2},{2:F2},{3:F2}”,normal,a,b,c);
}
}
/**
*在路径处导入STL文件。
*/
公共静态网格[]导入(字符串路径)
{
尝试
{
返回导入二进制(路径);
}
捕获(System.e例外)
{
UnityEngine.Debug.LogWarning(string.Format(“未能在路径{0}.\n{1}”,路径,例如ToString())导入网格);
返回null;
}
}
私有静态网格[]导入二进制(字符串路径)
{
列表面=新列表();
字节[]头;
单位分面计数;
使用(FileStream fs=newfilestream(路径,FileMode.Open,FileAccess.Read))
{
使用(BinaryReader br=new BinaryReader(fs,new-AscienceODing()))
{
while(br.BaseStream.Position#pragma warning disable 0219

using UnityEngine;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Threading;

namespace Parabox.STL
{
    /*Import methods for STL files*/
    public class STL_ImportScript : MonoBehaviour
    {

        const int MAX_FACETS_PER_MESH = 65535 / 3;

        class Facet
        {
            public Vector3 normal;
            public Vector3 a, b, c;

            public override string ToString()
            {
                return string.Format("{0:F2}: {1:F2}, {2:F2}, {3:F2}", normal, a, b, c);
            }
        }

        /**
         * Import an STL file at path.
         */
        public static Mesh[] Import(string path)
        {
                try
                {
                    return ImportBinary(path);
                }
                catch (System.Exception e)
                {
                    UnityEngine.Debug.LogWarning(string.Format("Failed importing mesh at path {0}.\n{1}", path, e.ToString()));
                    return null;
                }
        }

        private static Mesh[] ImportBinary(string path)
        {
            List<Facet> facets = new List<Facet>();
            byte[] header;
            uint facetCount;

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs, new ASCIIEncoding()))
                {
                    while (br.BaseStream.Position < br.BaseStream.Length)
                    {
                        // read header
                        header = br.ReadBytes(80);
                        facetCount = br.ReadUInt32();

                        for (uint i = 0; i < facetCount; i++)
                        {
                            try
                            {
                                Facet facet = new Facet();

                                facet.normal.x = br.ReadSingle();
                                facet.normal.y = br.ReadSingle();
                                facet.normal.z = br.ReadSingle();

                                facet.a.x = br.ReadSingle();
                                facet.a.y = br.ReadSingle();
                                facet.a.z = br.ReadSingle();

                                facet.b.x = br.ReadSingle();
                                facet.b.y = br.ReadSingle();
                                facet.b.z = br.ReadSingle();

                                facet.c.x = br.ReadSingle();
                                facet.c.y = br.ReadSingle();
                                facet.c.z = br.ReadSingle();

                                facets.Add(facet);


                                // padding
                                br.ReadUInt16();
                            }
                            catch (Exception e)
                            {
                                //Console.WriteLine(e.Message);
                                Debug.Log(e.Message);
                            }
                        }
                    }
                }
            }

            return CreateMeshWithFacets(facets);
        }

        const int SOLID = 1;
        const int FACET = 2;
        const int OUTER = 3;
        const int VERTEX = 4;
        const int ENDLOOP = 5;
        const int ENDFACET = 6;
        const int ENDSOLID = 7;
        const int EMPTY = 0;

        private static int ReadState(string line)
        {
            if (line.StartsWith("solid"))
                return SOLID;
            else if (line.StartsWith("facet"))
                return FACET;
            else if (line.StartsWith("outer"))
                return OUTER;
            else if (line.StartsWith("vertex"))
                return VERTEX;
            else if (line.StartsWith("endloop"))
                return ENDLOOP;
            else if (line.StartsWith("endfacet"))
                return ENDFACET;
            else if (line.StartsWith("endsolid"))
                return ENDSOLID;
            else
                return EMPTY;
        }

        private static Vector3 StringToVec3(string str)
        {
            string[] split = str.Trim().Split(null);
            Vector3 v = new Vector3();

            float.TryParse(split[0], out v.x);
            float.TryParse(split[1], out v.y);
            float.TryParse(split[2], out v.z);

            return v;
        }


        private static Mesh[] CreateMeshWithFacets(IList<Facet> facets)
        {
            int fl = facets.Count, f = 0, mvc = MAX_FACETS_PER_MESH * 3;
            Mesh[] meshes = new Mesh[fl / MAX_FACETS_PER_MESH + 1];

            for (int i = 0; i < meshes.Length; i++)
            {
                int len = System.Math.Min(mvc, (fl - f) * 3);
                Vector3[] v = new Vector3[len];
                Vector3[] n = new Vector3[len];
                int[] t = new int[len];

                for (int it = 0; it < len; it += 3)
                {
                    v[it] = facets[f].a;
                    v[it + 1] = facets[f].b;
                    v[it + 2] = facets[f].c;

                    n[it] = facets[f].normal;
                    n[it + 1] = facets[f].normal;
                    n[it + 2] = facets[f].normal;

                    t[it] = it;
                    t[it + 1] = it + 1;
                    t[it + 2] = it + 2;

                    f++;
                }

                meshes[i] = new Mesh();
                meshes[i].vertices = v;
                meshes[i].normals = n;
                meshes[i].triangles = t;

            }

            return meshes;
        }

    }
}