示例#1
0
 public static void WriteMesh(Stream stream, MeshFile mesh)
 {
     try
     {
         var meshFile = BuildMeshFile(mesh);
         WriteMeshFile(stream, meshFile);
     }
     catch
     {
         throw;
     }
 }
示例#2
0
        private static MESH_FILE BuildMeshFile(MeshFile mesh)
        {
            if (!mesh.Cullings.Any(x => x.Type == MeshCullingType.MainModel))
            {
                if (Debugger.IsAttached)
                {
                    throw new InvalidOperationException("Mesh does not contain culling information");
                }
                else
                {
                    Debug.WriteLine("Mesh has no culling information!");
                }
            }

            var file = new MESH_FILE
            {
                Header = MESH_HEADER.Create(mesh)
            };

            var avgNormals = mesh.GetAverageNormals().DistinctValues().ToList();
            var outlines   = mesh.GetRoundEdgeShaderData().EqualsDistinct().Select(x => x.Clone()).ToList();

            for (int i = 20; i < outlines.Count; i += 21)
            {
                outlines[i].PackData();
            }
            var shaderData = new ShaderDataHelper(avgNormals, outlines);

            file.AverageNormals      = avgNormals.ToArray();
            file.RoundEdgeShaderData = outlines.Select(x => new ROUNDEDGE_SHADER_DATA(x.Coords)).ToArray();

            file.Geometry = SerializeMeshGeometry(file, shaderData, mesh.Geometry);
            file.Cullings = new MESH_CULLING[mesh.Cullings.Count];

            for (int i = 0; i < mesh.Cullings.Count; i++)
            {
                file.Cullings[i] = SerializeMeshCulling(file, shaderData, mesh.Cullings[i]);
            }

            return(file);
        }
示例#3
0
        public static MeshFile ReadMesh(Stream stream)
        {
            try
            {
                var  meshFile   = ReadMeshFile(stream);
                var  meshType   = (MeshType)meshFile.Header.MeshType;
                bool isTextured = meshType == MeshType.StandardTextured || meshType == MeshType.FlexibleTextured;
                bool isFlexible = meshType == MeshType.Flexible || meshType == MeshType.FlexibleTextured;

                var mainMesh = MeshGeometry.Create(meshFile.Geometry);

                SetShaderData(meshFile, meshFile.Geometry, mainMesh);

                var mesh = new MeshFile(mainMesh);
                if (stream is FileStream fs)
                {
                    mesh.Filename = fs.Name;
                }

                mesh.SetGeometry(mainMesh);

                for (int i = 0; i < meshFile.Cullings.Length; i++)
                {
                    var data    = meshFile.Cullings[i];
                    var culling = new MeshCulling((MeshCullingType)data.Type)
                    {
                        FromIndex   = data.FromIndex,
                        IndexCount  = data.IndexCount,
                        FromVertex  = data.FromVertex,
                        VertexCount = data.VertexCount,
                    };

                    if (data.Studs != null && data.Studs.Length > 0)
                    {
                        for (int j = 0; j < data.Studs.Length; j++)
                        {
                            culling.Studs.Add(new Custom2DFieldReference(data.Studs[j]));
                        }
                    }

                    if (data.AdjacentStuds != null && data.AdjacentStuds.Length > 0)
                    {
                        for (int j = 0; j < data.AdjacentStuds.Length; j++)
                        {
                            culling.AdjacentStuds.Add(new Custom2DFieldReference(data.AdjacentStuds[j]));
                        }
                    }

                    if (data.AlternateMesh.HasValue)
                    {
                        var geom = MeshGeometry.Create(data.AlternateMesh.Value);
                        SetShaderData(meshFile, data.AlternateMesh.Value, geom);
                        culling.ReplacementMesh = geom;
                    }

                    mesh.Cullings.Add(culling);
                }
                return(mesh);
            }
            catch
            {
                throw;
            }
        }