Exemplo n.º 1
0
        private static MESH_CULLING SerializeMeshCulling(MESH_FILE file, ShaderDataHelper shaderData, MeshCulling meshCulling)
        {
            var culling = new MESH_CULLING
            {
                Type        = (int)meshCulling.Type,
                FromVertex  = meshCulling.FromVertex,
                VertexCount = meshCulling.VertexCount,
                FromIndex   = meshCulling.FromIndex,
                IndexCount  = meshCulling.IndexCount
            };

            if (meshCulling.ReplacementMesh != null)
            {
                culling.AlternateMesh = SerializeMeshGeometry(file, shaderData, meshCulling.ReplacementMesh);
            }

            if (meshCulling.Studs != null && meshCulling.Studs.Any())
            {
                culling.Studs = meshCulling.Studs.Select(x => x.Serialize()).ToArray();
            }

            if (meshCulling.AdjacentStuds != null && meshCulling.AdjacentStuds.Any())
            {
                culling.AdjacentStuds = meshCulling.AdjacentStuds.Select(x => x.Serialize()).ToArray();
            }

            return(culling);
        }
Exemplo n.º 2
0
        private static MESH_DATA SerializeMeshGeometry(MESH_FILE file, ShaderDataHelper shaderData, MeshGeometry meshGeometry)
        {
            var meshData = MESH_DATA.Create(meshGeometry.VertexCount, meshGeometry.IndexCount, file.IsTextured, file.IsFlexible);

            var vertIndexer = new ListIndexer <Vertex>(meshGeometry.Vertices);

            bool isTextured = meshGeometry.IsTextured;

            for (int i = 0; i < meshGeometry.Vertices.Count; i++)
            {
                var vert = meshGeometry.Vertices[i];
                meshData.Positions[i] = vert.Position;
                meshData.Normals[i]   = vert.Normal;

                if (file.IsTextured)
                {
                    meshData.UVs[i] = isTextured ? vert.TexCoord : new Vector2(0f);
                }

                if (file.IsFlexible && vert.BoneWeights.Any())
                {
                    var boneWeights = vert.BoneWeights.Select(x => new MESH_BONE_WEIGHT {
                        BoneID = x.BoneID, Weight = x.Weight
                    });
                    meshData.Bones[i] = new MESH_BONE_MAPPING(boneWeights);
                }
            }

            for (int i = 0; i < meshGeometry.Indices.Count; i++)
            {
                var idx = meshGeometry.Indices[i];
                meshData.Indices[i].VertexIndex = vertIndexer.IndexOf(idx.Vertex);

                meshData.Indices[i].AverageNormalIndex = shaderData.AvgNormals.IndexOf(idx.AverageNormal);

                int reIdx = shaderData.RoundEdgeData.IndexOf(idx.RoundEdgeData);

                int reOffset = reIdx * 12 + ((int)Math.Floor(reIdx / 21d) * 4);

                meshData.Indices[i].REShaderOffset = reOffset;
            }

            return(meshData);
        }
Exemplo n.º 3
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);
        }