Esempio n. 1
0
        public void Export(string FilePath, GenericModel Model)
        {
            using (DAEWriter writer = new DAEWriter(FilePath, false))
            {
                var path = System.IO.Path.GetDirectoryName(FilePath) + '\\';

                writer.WriteAsset();

                DialogResult dialogResult = MessageBox.Show("Export Materials and Textures?", "DAE Exporter", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    List <string> TextureNames = new List <string>();

                    foreach (var tex in Model.TextureBank)
                    {
                        TextureNames.Add(tex.Key);

                        if (tex.Value.Mipmaps.Count != 0)
                        {
                            Rendering.RenderTexture Temp = new Rendering.RenderTexture();
                            Temp.LoadGenericTexture(tex.Value);
                            Temp.ExportPNG(new FileInfo(FilePath).Directory.FullName + "/" + tex.Key + ".png");
                            Temp.Delete();
                        }
                    }

                    writer.WriteLibraryImages(TextureNames.ToArray(), ".png");

                    writer.StartMaterialSection();
                    foreach (var mat in Model.MaterialBank)
                    {
                        writer.WriteMaterial(mat.Key);
                    }
                    writer.EndMaterialSection();

                    writer.StartEffectSection();
                    foreach (var mat in Model.MaterialBank)
                    {
                        writer.WriteEffect(mat.Key, mat.Value.TextureDiffuse);
                    }
                    writer.EndEffectSection();
                }
                else
                {
                    writer.WriteLibraryImages();
                }

                var oldIDToNew             = new Dictionary <int, int>();
                var boneIndexToBreathIndex = new Dictionary <GenericBone, int>();
                var breathFirstBones       = Model.Skeleton.BreathFirst;
                var index = 0;
                foreach (var bone in Model.Skeleton.Bones)
                {
                    boneIndexToBreathIndex.Add(bone, index++);
                }
                if (Model.Skeleton.Bones != null)
                {
                    for (int i = 0; i < Model.Skeleton.Bones.Count; i++)
                    {
                        var bone = breathFirstBones[i];
                        oldIDToNew.Add(boneIndexToBreathIndex[bone], i);
                        float[] Transform = new float[] { bone.Transform.M11, bone.Transform.M21, bone.Transform.M31, bone.Transform.M41,
                                                          bone.Transform.M12, bone.Transform.M22, bone.Transform.M32, bone.Transform.M42,
                                                          bone.Transform.M13, bone.Transform.M23, bone.Transform.M33, bone.Transform.M43,
                                                          bone.Transform.M14, bone.Transform.M24, bone.Transform.M34, bone.Transform.M44 };
                        Matrix4 InvWorldTransform = Model.Skeleton.GetWorldTransform(bone).Inverted();
                        float[] InvTransform      = new float[] { InvWorldTransform.M11, InvWorldTransform.M21, InvWorldTransform.M31, InvWorldTransform.M41,
                                                                  InvWorldTransform.M12, InvWorldTransform.M22, InvWorldTransform.M32, InvWorldTransform.M42,
                                                                  InvWorldTransform.M13, InvWorldTransform.M23, InvWorldTransform.M33, InvWorldTransform.M43,
                                                                  InvWorldTransform.M14, InvWorldTransform.M24, InvWorldTransform.M34, InvWorldTransform.M44 };
                        writer.AddJoint(bone.Name, bone.ParentIndex == -1 ? "" : Model.Skeleton.Bones[bone.ParentIndex].Name, Transform, InvTransform);
                    }
                }

                writer.StartGeometrySection();
                foreach (var mesh in Model.Meshes)
                {
                    if (!mesh.Export)
                    {
                        continue;
                    }

                    mesh.MakeTriangles();

                    writer.StartGeometryMesh(mesh.Name);

                    writer.CurrentMaterial = mesh.MaterialName;

                    List <float>   Positions   = new List <float>();
                    List <float>   Normals     = new List <float>();
                    List <float>   UV0s        = new List <float>();
                    List <float>   Colors      = new List <float>();
                    List <float[]> BoneWeights = new List <float[]>();
                    List <int[]>   BoneIndices = new List <int[]>();

                    foreach (var vert in mesh.Vertices)
                    {
                        Positions.AddRange(new float[] { vert.Pos.X, vert.Pos.Y, vert.Pos.Z });
                        Normals.AddRange(new float[] { vert.Nrm.X, vert.Nrm.Y, vert.Nrm.Z });
                        Colors.AddRange(new float[] { vert.Clr.X, vert.Clr.Y, vert.Clr.Z, vert.Clr.W });
                        UV0s.AddRange(new float[] { vert.UV0.X, 1 - vert.UV0.Y });
                        List <float> weights = new List <float>();
                        List <int>   bones   = new List <int>();
                        for (int i = 0; i < 4; i++)
                        {
                            if (vert.Weights[i] > 0 && oldIDToNew.ContainsKey((int)vert.Bones[i]))
                            {
                                weights.Add(vert.Weights[i]);
                                bones.Add(oldIDToNew[(int)vert.Bones[i]]);
                            }
                        }
                        BoneWeights.Add(weights.ToArray());
                        BoneIndices.Add(bones.ToArray());
                    }

                    writer.WriteGeometrySource(mesh.Name, DAEWriter.VERTEX_SEMANTIC.POSITION, Positions.ToArray(), mesh.Triangles.ToArray());

                    writer.WriteGeometrySource(mesh.Name, DAEWriter.VERTEX_SEMANTIC.NORMAL, Normals.ToArray(), mesh.Triangles.ToArray());

                    writer.WriteGeometrySource(mesh.Name, DAEWriter.VERTEX_SEMANTIC.TEXCOORD, UV0s.ToArray(), mesh.Triangles.ToArray());

                    writer.WriteGeometrySource(mesh.Name, DAEWriter.VERTEX_SEMANTIC.COLOR, Colors.ToArray(), mesh.Triangles.ToArray());

                    writer.AttachGeometryController(BoneIndices, BoneWeights);

                    writer.EndGeometryMesh();
                }
                writer.EndGeometrySection();
            }
        }
Esempio n. 2
0
        public void Export(string FilePath, GenericModel Model)
        {
            using (StreamWriter w = new StreamWriter(new FileStream(FilePath, FileMode.Create)))
            {
                w.WriteLine("version 1");

                if (Model.Skeleton != null)
                {
                    w.WriteLine("nodes");
                    foreach (GenericBone bone in Model.Skeleton.Bones)
                    {
                        w.WriteLine($" {Model.Skeleton.IndexOf(bone)} \"{bone.Name}\" {bone.ParentIndex}");
                    }
                    // meshNodes
                    foreach (var mesh in Model.Meshes)
                    {
                        w.WriteLine($" {Model.Skeleton.Bones.Count + Model.Meshes.IndexOf(mesh)} \"{mesh.Name}\" -1");
                    }
                    w.WriteLine("end");
                    w.WriteLine("skeleton");
                    w.WriteLine("time 0");
                    foreach (GenericBone bone in Model.Skeleton.Bones)
                    {
                        w.WriteLine($" {Model.Skeleton.IndexOf(bone)} {bone.Position.X} {bone.Position.Y} {bone.Position.Z} {bone.Rotation.X} {bone.Rotation.Y} {bone.Rotation.Z}");
                    }
                    foreach (var mesh in Model.Meshes)
                    {
                        w.WriteLine($" {Model.Skeleton.Bones.Count + Model.Meshes.IndexOf(mesh)} 0 0 0 0 0 0");
                    }
                    w.WriteLine("end");
                }

                w.WriteLine("triangles");
                Dictionary <GenericTexture, string> TextureBank = new Dictionary <GenericTexture, string>();
                foreach (GenericMesh m in Model.Meshes)
                {
                    if (!m.Export)
                    {
                        continue;
                    }

                    var meshIndex = Model.Skeleton.Bones.Count + Model.Meshes.IndexOf(m);
                    m.MakeTriangles();
                    string MaterialName = m.Name;
                    if (m.MaterialName != null && Model.MaterialBank.ContainsKey(m.MaterialName))
                    {
                        var material = Model.MaterialBank[m.MaterialName];
                        if (material.TextureDiffuse != null && Model.TextureBank.ContainsKey(material.TextureDiffuse))
                        {
                            var texture = Model.TextureBank[material.TextureDiffuse];
                            if (TextureBank.ContainsKey(texture))
                            {
                                MaterialName = TextureBank[texture];
                            }
                            else
                            {
                                string TextureName = material.TextureDiffuse.Equals("") ? "Texture_" + TextureBank.Count + ".png" : material.TextureDiffuse + ".png";

                                if (texture.Mipmaps.Count != 0)
                                {
                                    Rendering.RenderTexture Temp = new Rendering.RenderTexture();
                                    Temp.LoadGenericTexture(texture);
                                    Temp.ExportPNG(new FileInfo(FilePath).Directory.FullName + "/" + TextureName);
                                    Temp.Delete();
                                }
                                TextureBank.Add(texture, TextureName);
                                MaterialName = TextureName;
                            }
                        }
                    }
                    for (int i = 0; i < m.Triangles.Count; i += 3)
                    {
                        w.WriteLine(MaterialName);
                        {
                            GenericVertex v = m.Vertices[(int)m.Triangles[i]];
                            w.WriteLine($" {meshIndex} {v.Pos.X} {v.Pos.Y} {v.Pos.Z} {v.Nrm.X} {v.Nrm.Y} {v.Nrm.Z} {v.UV0.X} {v.UV0.Y} " + WriteWeights(v));
                        }
                        {
                            GenericVertex v = m.Vertices[(int)m.Triangles[i + 1]];
                            w.WriteLine($" {meshIndex} {v.Pos.X} {v.Pos.Y} {v.Pos.Z} {v.Nrm.X} {v.Nrm.Y} {v.Nrm.Z} {v.UV0.X} {v.UV0.Y} " + WriteWeights(v));
                        }
                        {
                            GenericVertex v = m.Vertices[(int)m.Triangles[i + 2]];
                            w.WriteLine($" {meshIndex} {v.Pos.X} {v.Pos.Y} {v.Pos.Z} {v.Nrm.X} {v.Nrm.Y} {v.Nrm.Z} {v.UV0.X} {v.UV0.Y} " + WriteWeights(v));
                        }
                    }
                }
                w.WriteLine("end");

                w.Close();
            }

            if (Model.HasMorphs)
            {
                WriteVTA(FilePath.Replace(".smd", ".vta"), Model);
            }
        }