private static List CreateMeshWithFacets()

in unity/Assets/Scripts/pb_Stl/pb_Stl_Importer.cs [246:308]


        private static List<Mesh> CreateMeshWithFacets(IList<Facet> facets) {
            int facet_count = facets.Count, current_facet = 0, full_mesh_vertices = MAX_FACETS_PER_MESH * 3;
            List<Mesh> meshes = new List<Mesh>();

            Vector3 center = Vector3.zero;
            foreach (Facet facet in facets) {
                center += facet.a + facet.b + facet.c;
            }
            center /= facets.Count * 3.0f;

            for (int i = 0; i < MeshesCount(facets.Count); i++) {
                int len = System.Math.Min(full_mesh_vertices, (facet_count - current_facet) * 3);

                Vector3[] v = new Vector3[len];
                Vector3[] n = new Vector3[len];
                Vector2[] uv = new Vector2[len];
                int[] t = new int[len];

                Dictionary<Vector3, List<Vector3>> normals = new Dictionary<Vector3, List<Vector3>>();

                int facet_index = current_facet;

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

                    AddNormal(normals, facets[facet_index].a, facets[facet_index].normal.normalized);
                    AddNormal(normals, facets[facet_index].b, facets[facet_index].normal.normalized);
                    AddNormal(normals, facets[facet_index].c, facets[facet_index].normal.normalized);
                     
                    t[it] = it;
                    t[it + 1] = it + 1;
                    t[it + 2] = it + 2;

                    uv[it] = GetUv(facets[facet_index].a - center);
                    uv[it + 1] = GetUv(facets[facet_index].b - center);
                    uv[it + 2] = GetUv(facets[facet_index].c - center);

                    facet_index++;
                }

                facet_index = current_facet;

                for (int it = 0; it < len; it += 3) {
                    n[it] = CalculateNormal(facets[facet_index].normal, normals[facets[facet_index].a]);
                    n[it + 1] = CalculateNormal(facets[facet_index].normal, normals[facets[facet_index].b]);
                    n[it + 2] = CalculateNormal(facets[facet_index].normal, normals[facets[facet_index].c]);
                    facet_index++;
                }

                current_facet = facet_index;

                Mesh mesh = new Mesh();
                mesh.vertices = v;
                mesh.normals = n;
                mesh.triangles = t;
                mesh.uv = uv;
                meshes.Add(mesh);
            }

            return meshes;
        }