コード例 #1
0
        private static void CreatePolys(ref GameObject faceObject, Face f, Material m)
        {
            List <Vector3> verts = new List <Vector3>();
            List <Vector2> uvs   = new List <Vector2>();
            List <int>     tris  = new List <int>();

            faceObject.AddComponent <MeshFilter>();
            faceObject.AddComponent <MeshRenderer>();

            Mesh faceMesh = faceObject.GetComponent <MeshFilter>().mesh;

            faceMesh.Clear();

            // go through and generate the mesh vertex by vertex
            for (int polyIndex = 0; polyIndex < f.Polys.Length; polyIndex++)
            {
                Poly p = f.Polys[polyIndex];

                for (int vertIndex = 0; vertIndex < p.NumberOfVertices; vertIndex++)
                {
                    Vertex v = p.Verts[vertIndex];
                    verts.Add(Vec3ToUnity(v.P));
                }

                // generate triangles
                for (int triIndex = 0; triIndex < p.NumberOfVertices - 2; triIndex++)
                {
                    tris.Add(0);
                    tris.Add(triIndex + 1);
                    tris.Add(triIndex + 2);
                }

                p.CalculateTextureCoordinates(m.GetTexture("_MainTexA").width, m.GetTexture("_MainTexA").height, f.TexAxis, f.TexScale);

                // generate UVs
                for (int uvIndex = 0; uvIndex < p.NumberOfVertices; uvIndex++)
                {
                    Vector2 uv = new Vector2(p.Verts[uvIndex].Tex[0], p.Verts[uvIndex].Tex[1]);
                    uvs.Add(uv);
                }
            }

            // set up the polygons for the mesh and calculate normals
            faceMesh.vertices  = verts.ToArray();
            faceMesh.triangles = tris.ToArray();
            faceMesh.uv        = uvs.ToArray();
            faceMesh.RecalculateNormals();

            faceObject.GetComponent <Renderer>().sharedMaterial = m;
        }