コード例 #1
0
        public DenseUVMesh            UVs;              // separate UV layer (just one for now)
        // assumption is that # of triangles in this UV mesh is same as in Mesh

        public WriteMesh(IMesh mesh, string name = "")
        {
            Mesh             = mesh;
            Name             = name;
            UVs              = null;
            Materials        = null;
            TriToMaterialMap = null;
        }
コード例 #2
0
        // write triangles of mesh with re-ordering to minimize group changes
        // (note: this may mean lots of material changes, depending on mesh...)
        void write_triangles_bygroup(TextWriter writer, IMesh mesh, int[] mapV, DenseUVMesh uvSet, IIndexMap mapUV, bool bNormals)
        {
            // This makes N passes over mesh indices, but doesn't use much extra memory.
            // would there be a faster way? could construct integer-pointer-list during initial
            // scan, this would need O(N) memory but then write is effectively O(N) instead of O(N*k)

            bool bUVs = (mapUV != null);

            HashSet <int> vGroups = new HashSet <int>();

            foreach (int ti in mesh.TriangleIndices())
            {
                vGroups.Add(mesh.GetTriangleGroup(ti));
            }

            List <int> sortedGroups = new List <int>(vGroups);

            sortedGroups.Sort();
            foreach (int g in sortedGroups)
            {
                string group_name = GroupNamePrefix;
                if (GroupNameF != null)
                {
                    group_name = GroupNameF(g);
                }
                else
                {
                    group_name = string.Format("{0}{1}", GroupNamePrefix, g);
                }
                writer.WriteLine("g " + group_name);

                foreach (int ti in mesh.TriangleIndices())
                {
                    if (mesh.GetTriangleGroup(ti) != g)
                    {
                        continue;
                    }

                    Index3i t = mesh.GetTriangle(ti);
                    t[0] = mapV[t[0]];
                    t[1] = mapV[t[1]];
                    t[2] = mapV[t[2]];

                    if (bUVs)
                    {
                        Index3i tuv = (uvSet != null) ? uvSet.TriangleUVs[ti] : t;
                        tuv[0] = mapUV[tuv[0]];
                        tuv[1] = mapUV[tuv[1]];
                        tuv[2] = mapUV[tuv[2]];
                        write_tri(writer, ref t, bNormals, true, ref tuv);
                    }
                    else
                    {
                        write_tri(writer, ref t, bNormals, false, ref t);
                    }
                }
            }
        }
コード例 #3
0
        public IOWriteResult Write(TextWriter writer, List <WriteMesh> vMeshes, WriteOptions options)
        {
            if (options.groupNamePrefix != null)
            {
                GroupNamePrefix = options.groupNamePrefix;
            }
            if (options.GroupNameF != null)
            {
                GroupNameF = options.GroupNameF;
            }

            int nAccumCountV  = 1;      // OBJ indices always start at 1
            int nAccumCountUV = 1;

            // collect materials
            string sMaterialLib   = "";
            int    nHaveMaterials = 0;

            if (options.bWriteMaterials && options.MaterialFilePath.Length > 0)
            {
                List <GenericMaterial> vMaterials = MeshIOUtil.FindUniqueMaterialList(vMeshes);
                IOWriteResult          ok         = write_materials(vMaterials, options);
                if (ok.code == IOCode.Ok)
                {
                    sMaterialLib   = Path.GetFileName(options.MaterialFilePath);
                    nHaveMaterials = vMeshes.Count;
                }
            }


            if (options.AsciiHeaderFunc != null)
            {
                writer.WriteLine(options.AsciiHeaderFunc());
            }

            if (sMaterialLib != "")
            {
                writer.WriteLine("mtllib {0}", sMaterialLib);
            }

            for (int mi = 0; mi < vMeshes.Count; ++mi)
            {
                IMesh mesh = vMeshes[mi].Mesh;

                if (options.ProgressFunc != null)
                {
                    options.ProgressFunc(mi, vMeshes.Count - 1);
                }

                bool bVtxColors = options.bPerVertexColors && mesh.HasVertexColors;
                bool bNormals   = options.bPerVertexNormals && mesh.HasVertexNormals;

                // use separate UV set if we have it, otherwise write per-vertex UVs if we have those
                bool bVtxUVs = options.bPerVertexUVs && mesh.HasVertexUVs;
                if (vMeshes[mi].UVs != null)
                {
                    bVtxUVs = false;
                }

                int[] mapV = new int[mesh.MaxVertexID];

                // write vertices for this mesh
                foreach (int vi in mesh.VertexIndices())
                {
                    mapV[vi] = nAccumCountV++;
                    Vector3d v = mesh.GetVertex(vi);
                    if (bVtxColors)
                    {
                        Vector3d c = mesh.GetVertexColor(vi);
                        writer.WriteLine("v {0} {1} {2} {3:F8} {4:F8} {5:F8}", v[0], v[1], v[2], c[0], c[1], c[2]);
                    }
                    else
                    {
                        writer.WriteLine("v {0} {1} {2}", v[0], v[1], v[2]);
                    }

                    if (bNormals)
                    {
                        Vector3d n = mesh.GetVertexNormal(vi);
                        writer.WriteLine("vn {0:F10} {1:F10} {2:F10}", n[0], n[1], n[2]);
                    }

                    if (bVtxUVs)
                    {
                        Vector2f uv = mesh.GetVertexUV(vi);
                        writer.WriteLine("vt {0:F10} {1:F10}", uv.x, uv.y);
                    }
                }

                // write independent UVs for this mesh, if we have them
                IIndexMap   mapUV = (bVtxUVs) ? new IdentityIndexMap() : null;
                DenseUVMesh uvSet = null;
                if (vMeshes[mi].UVs != null)
                {
                    uvSet = vMeshes[mi].UVs;
                    int      nUV     = uvSet.UVs.Length;
                    IndexMap fullMap = new IndexMap(false, nUV);                       // [TODO] do we really need a map here? is just integer shift, no?
                    for (int ui = 0; ui < nUV; ++ui)
                    {
                        writer.WriteLine("vt {0:F8} {1:F8}", uvSet.UVs[ui].x, uvSet.UVs[ui].y);
                        fullMap[ui] = nAccumCountUV++;
                    }
                    mapUV = fullMap;
                }

                // check if we need to write usemtl lines for this mesh
                bool bWriteMaterials = nHaveMaterials > 0 &&
                                       vMeshes[mi].TriToMaterialMap != null &&
                                       vMeshes[mi].Materials != null;

                // various ways we can write triangles to minimize state changes...
                // [TODO] support writing materials when mesh has groups!!
                if (options.bWriteGroups && mesh.HasTriangleGroups)
                {
                    write_triangles_bygroup(writer, mesh, mapV, uvSet, mapUV, bNormals);
                }
                else
                {
                    write_triangles_flat(writer, vMeshes[mi], mapV, uvSet, mapUV, bNormals, bWriteMaterials);
                }
            }


            return(new IOWriteResult(IOCode.Ok, ""));
        }
コード例 #4
0
        // sequential write of input mesh triangles. preserves triangle IDs up to constant shift.
        void write_triangles_flat(TextWriter writer, WriteMesh write_mesh, int[] mapV, DenseUVMesh uvSet, IIndexMap mapUV, bool bNormals, bool bMaterials)
        {
            bool bUVs = (mapUV != null);

            int cur_material = -1;

            IMesh mesh = write_mesh.Mesh;

            foreach (int ti in mesh.TriangleIndices())
            {
                if (bMaterials)
                {
                    set_current_material(writer, ti, write_mesh, ref cur_material);
                }

                Index3i t = mesh.GetTriangle(ti);
                t[0] = mapV[t[0]];
                t[1] = mapV[t[1]];
                t[2] = mapV[t[2]];

                if (bUVs)
                {
                    Index3i tuv = (uvSet != null) ? uvSet.TriangleUVs[ti] : t;
                    tuv[0] = mapUV[tuv[0]];
                    tuv[1] = mapUV[tuv[1]];
                    tuv[2] = mapUV[tuv[2]];
                    write_tri(writer, ref t, bNormals, true, ref tuv);
                }
                else
                {
                    write_tri(writer, ref t, bNormals, false, ref t);
                }
            }
        }