示例#1
0
        /// <summary>
        /// Converts g3.DMesh3 to UnityEngine.Mesh.
        /// The DMesh3 must be compact. If neccesary - run Compactify first.
        /// </summary>
        /// <param name="mesh">Dmesh3</param>
        /// <param name="project"> Should the mesh be projected into virgis projection DEFAULT true</param>
        /// <returns>UnityEngine.Mesh</returns>
        public static Mesh ToMesh(this DMesh3 mesh, Boolean project = true)
        {
            Mesh unityMesh = new Mesh();

            unityMesh.MarkDynamic();
            unityMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
            if (project && !mesh.Transform(AppState.instance.mapProj))
            {
                throw new Exception("Mesh Projection Failed");
            }
            Vector3[]     vertices = new Vector3[mesh.VertexCount];
            Color[]       colors   = new Color[mesh.VertexCount];
            Vector2[]     uvs      = new Vector2[mesh.VertexCount];
            Vector3[]     normals  = new Vector3[mesh.VertexCount];
            NewVertexInfo data;

            for (int i = 0; i < mesh.VertexCount; i++)
            {
                if (mesh.IsVertex(i))
                {
                    data        = mesh.GetVertexAll(i);
                    vertices[i] = (Vector3)data.v;
                    if (data.bHaveC)
                    {
                        colors[i] = (Color)data.c;
                    }
                    if (data.bHaveUV)
                    {
                        uvs[i] = (Vector2)data.uv;
                    }
                    if (data.bHaveN)
                    {
                        normals[i] = (Vector3)data.n;
                    }
                }
            }
            unityMesh.vertices = vertices;
            if (mesh.HasVertexColors)
            {
                unityMesh.SetColors(colors);
            }
            if (mesh.HasVertexUVs)
            {
                unityMesh.SetUVs(0, uvs);
            }
            if (mesh.HasVertexNormals)
            {
                unityMesh.SetNormals(normals);
            }
            int[] triangles = new int[mesh.TriangleCount * 3];
            int   j         = 0;

            foreach (Index3i tri in mesh.Triangles())
            {
                triangles[j * 3]     = tri.a;
                triangles[j * 3 + 1] = tri.b;
                triangles[j * 3 + 2] = tri.c;
                j++;
            }
            unityMesh.triangles = triangles;
            return(unityMesh);
        }