예제 #1
0
        /// <summary>
        /// Converts g3.SimpleMesh to UnityEngine.Mesh
        /// </summary>
        /// <param name="simpleMesh">SimpleMesh</param>
        /// <returns>UnityEngine.Mesh</returns>
        public static Mesh ToMesh(this SimpleMesh simpleMesh)
        {
            Mesh unityMesh = new Mesh();

            MeshTransforms.ConvertZUpToYUp(simpleMesh);
            Vector3[]     vertices = new Vector3[simpleMesh.VertexCount];
            Color[]       colors   = new Color[simpleMesh.VertexCount];
            Vector2[]     uvs      = new Vector2[simpleMesh.VertexCount];
            Vector3[]     normals  = new Vector3[simpleMesh.VertexCount];
            NewVertexInfo data;

            for (int i = 0; i < simpleMesh.VertexCount; i++)
            {
                data        = simpleMesh.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 (simpleMesh.HasVertexColors)
            {
                unityMesh.colors = colors;
            }
            if (simpleMesh.HasVertexUVs)
            {
                unityMesh.uv = uvs;
            }
            if (simpleMesh.HasVertexNormals)
            {
                unityMesh.normals = normals;
            }
            int[] triangles = new int[simpleMesh.TriangleCount * 3];
            int   j         = 0;

            foreach (Index3i tri in simpleMesh.TrianglesItr())
            {
                triangles[j * 3]     = tri.a;
                triangles[j * 3 + 1] = tri.b;
                triangles[j * 3 + 2] = tri.c;
                j++;
            }
            unityMesh.triangles = triangles;
            return(unityMesh);
        }
 /// <summary>
 /// Emit a SimpleMesh as an AsciiMeshStruct
 /// </summary>
 public static void EmitMeshAscii(this SceneSerializer s, SimpleMesh m, IOutputStream o)
 {
     o.BeginStruct(IOStrings.AsciiMeshStruct);
     o.AddAttribute(IOStrings.AMeshVertices3, m.VerticesItr());
     if (m.HasVertexNormals)
     {
         o.AddAttribute(IOStrings.AMeshNormals3, m.NormalsItr());
     }
     if (m.HasVertexColors)
     {
         o.AddAttribute(IOStrings.AMeshColors3, m.ColorsItr());
     }
     if (m.HasVertexUVs)
     {
         o.AddAttribute(IOStrings.AMeshUVs2, m.UVsItr());
     }
     o.AddAttribute(IOStrings.AMeshTriangles, m.TrianglesItr());
     o.EndStruct();
 }