Exemple #1
0
        private PolygonGroup GetGroupForTextureIndex(int textureIndex)
        {
            for (int i = 0; i < polygonGroups.Count; i++)
            {
                if (polygonGroups[i].textureIndex == textureIndex)
                {
                    return(polygonGroups[i]);
                }
            }

            PolygonGroup group = new PolygonGroup(textureIndex);

            polygonGroups.Add(group);
            return(group);
        }
Exemple #2
0
        private static Mesh BuildMesh2(List <PolygonGroup> polygonGroups)
        {
            int          triangleCount = 0;
            List <int[]> tris          = new List <int[]>();
            Dictionary <Vector3, List <FinalVertex> > lookupPositions = new Dictionary <Vector3, List <FinalVertex> >();

            List <Vector3> vertices = new List <Vector3>();
            List <Vector3> normals  = new List <Vector3>();
            List <Vector2> uvCoords = new List <Vector2>();

            for (int i = 0; i < polygonGroups.Count; i++)
            {
                PolygonGroup   group     = polygonGroups[i];
                List <Polygon> polygons  = group.polygons;
                int[]          triangles = new int[polygons.Count * 3];
                tris.Add(triangles);
                int count = 0;

                for (int j = 0; j < polygons.Count; j++)
                {
                    for (int k = 0; k < polygons[j].vertices.Length; k++)
                    {
                        FinalVertex vertex = new FinalVertex(polygons[j].vertices[k], -1);

                        FinalVertex        match = null;
                        List <FinalVertex> matchList;

                        if (lookupPositions.TryGetValue(vertex.position, out matchList))
                        {
                            match = FindMatchingVertex(vertex, matchList);
                        }
                        else
                        {
                            matchList = new List <FinalVertex>();
                            matchList.Add(vertex);
                            lookupPositions.Add(vertex.position, matchList);
                        }

                        if (match != null)
                        {
                            triangles[count] = match.triangleIndex;
                        }
                        else
                        {
                            vertex.triangleIndex = triangleCount++;
                            triangles[count]     = vertex.triangleIndex;
                            vertices.Add(vertex.position);
                            normals.Add(vertex.normal);
                            uvCoords.Add(vertex.uvCoord);
                        }

                        count++;
                    }
                }
            }

            Mesh mesh = new Mesh();

            mesh.subMeshCount = polygonGroups.Count;

            mesh.SetVertices(vertices);
            mesh.SetUVs(0, uvCoords);
            mesh.SetNormals(normals);

            for (int i = 0; i < polygonGroups.Count; i++)
            {
                mesh.SetTriangles(tris[i], i);
            }

            return(mesh);
        }
Exemple #3
0
        private void AddPolygonToGroup(Polygon polygon, int textureIndex)
        {
            PolygonGroup group = GetGroupForTextureIndex(textureIndex);

            // quad 1 2 3 4
            //    ==>
            //triangle 1 2 3
            //triangle 3 4 1
            if (polygon.vertices.Length != 3)
            {
                if (polygon.vertices.Length == 4)
                {
                    Polygon p1 = new Polygon();
                    Polygon p2 = new Polygon();
                    p1.vertices     = new Vertex[3];
                    p2.vertices     = new Vertex[3];
                    p1.vertices[0]  = polygon.vertices[0];
                    p1.vertices[1]  = polygon.vertices[1];
                    p1.vertices[2]  = polygon.vertices[2];
                    p2.vertices[0]  = polygon.vertices[2];
                    p2.vertices[1]  = polygon.vertices[3];
                    p2.vertices[2]  = polygon.vertices[0];
                    p1.textureIndex = polygon.textureIndex;
                    p2.textureIndex = polygon.textureIndex;
                    //todo -- recalculate normal if needed
                    group.polygons.Add(p1);
                    group.polygons.Add(p2);
                }
                else
                {
                    Vector3 averageNormal = Vector3.zero;
                    Vector2 averageUVs    = Vector2.zero;

                    for (int i = 0; i < polygon.vertices.Length; i++)
                    {
                        averageUVs    += polygon.vertices[i].uvCoords;
                        averageNormal += polygon.vertices[i].normal;
                    }

                    averageUVs    = averageUVs / polygon.vertices.Length;
                    averageNormal = averageNormal / polygon.vertices.Length;

                    for (int i = 0; i < polygon.vertices.Length; i++)
                    {
                        Polygon p = new Polygon();
                        p.normal               = polygon.normal;
                        p.textureIndex         = polygon.textureIndex;
                        p.vertices             = new Vertex[3];
                        p.vertices[2]          = polygon.vertices[i];
                        p.vertices[1]          = new Vertex();
                        p.vertices[1].point    = polygon.centroid;
                        p.vertices[1].uvCoords = averageUVs;
                        p.vertices[1].normal   = averageNormal;
                        p.vertices[0]          = polygon.vertices[(i + 1) % polygon.vertices.Length];
                        group.polygons.Add(p);
                    }
                }
            }
            else
            {
                group.polygons.Add(polygon);
            }
        }