Example #1
0
        public void SaveFromModel(STGenericModel model, string FileName, List <STGenericTexture> Textures, STSkeleton skeleton = null, List <int> NodeArray = null)
        {
            ExtractedTextures.Clear();

            Scene scene = new Scene();

            scene.RootNode = new Node("RootNode");

            progressBar               = new STProgressBar();
            progressBar.Task          = "Exorting Skeleton...";
            progressBar.Value         = 0;
            progressBar.StartPosition = FormStartPosition.CenterScreen;
            progressBar.Show();
            progressBar.Refresh();

            SaveSkeleton(skeleton, scene.RootNode);
            SaveMaterials(scene, model, FileName, Textures);

            progressBar.Task  = "Exorting Meshes...";
            progressBar.Value = 50;

            SaveMeshes(scene, model, skeleton, FileName, NodeArray);

            progressBar.Task  = "Saving File...";
            progressBar.Value = 80;

            SaveScene(FileName, scene, model.GetObjects());

            progressBar.Value = 100;
            progressBar.Close();
            progressBar.Dispose();
        }
Example #2
0
 private static STGenericMaterial GetMaterial(int MaterialIndex, STGenericModel Model)
 {
     if (MaterialIndex < Model.Nodes[1].Nodes.Count)
     {
         return((STGenericMaterial)Model.Nodes[1].Nodes[MaterialIndex]);
     }
     else
     {
         return(null);
     }
 }
Example #3
0
        private static void SaveMeshes(StringBuilder writer, STGenericModel Model, string MtlName)
        {
            writer.AppendLine($"mtllib {MtlName}");

            int VertexCount = 1;

            foreach (STGenericObject mesh in Model.Nodes[0].Nodes)
            {
                var mat = GetMaterial(mesh.MaterialIndex, Model);
                SaveMesh(writer, mesh, mat, VertexCount);
            }
        }
Example #4
0
        private void SaveMaterials(Scene scene, STGenericModel model, string FileName, List <STGenericTexture> Textures)
        {
            string TextureExtension = ".png";
            string TexturePath      = System.IO.Path.GetDirectoryName(FileName);

            for (int i = 0; i < Textures.Count; i++)
            {
                string path = System.IO.Path.Combine(TexturePath, Textures[i].Text + TextureExtension);

                if (!ExtractedTextures.Contains(path))
                {
                    ExtractedTextures.Add(path);

                    progressBar.Task  = $"Exorting Texture {Textures[i].Text}";
                    progressBar.Value = ((i * 100) / Textures.Count);
                    progressBar.Refresh();

                    var bitmap = Textures[i].GetBitmap();
                    bitmap.Save(path);
                    bitmap.Dispose();

                    GC.Collect();
                }
            }

            foreach (var mat in model.Nodes[1].Nodes)
            {
                var genericMat = (STGenericMaterial)mat;

                Material material = new Material();
                material.Name = genericMat.Text;

                foreach (var tex in genericMat.TextureMaps)
                {
                    int index = Textures.FindIndex(r => r.Text.Equals(tex.Name));

                    string path = System.IO.Path.Combine(TexturePath, tex.Name + TextureExtension);

                    if (!File.Exists(path))
                    {
                        continue;
                    }

                    TextureSlot slot2 = new TextureSlot(path, ConvertToAssimpTextureType(tex.Type), 0, TextureMapping.FromUV,
                                                        0, 1.0f, Assimp.TextureOperation.Add, ConvertToAssimpWrapType(tex.wrapModeS), ConvertToAssimpWrapType(tex.wrapModeT), 0);

                    material.AddMaterialTexture(ref slot2);
                }
                scene.Materials.Add(material);
            }
        }
Example #5
0
        public static void ExportModel(string FileName, STGenericModel Model, List <STGenericTexture> Textures)
        {
            string fileNoExt   = Path.GetFileNameWithoutExtension(FileName);
            string fileMtlPath = FileName.Replace("obj", "mtl");

            //Write model
            StringBuilder writer = new StringBuilder();

            SaveMeshes(writer, Model, $"{fileNoExt}.mtl");
            File.WriteAllText(FileName, writer.ToString());

            //Write materials
            StringBuilder writerMtl = new StringBuilder();

            SaveMaterials(writerMtl, Model);
            File.WriteAllText(fileMtlPath, writerMtl.ToString());
        }
Example #6
0
        private void SaveMeshes(Scene scene, STGenericModel model, STSkeleton skeleton, string FileName, List <int> NodeArray)
        {
            int MeshIndex = 0;

            foreach (var obj in model.Nodes[0].Nodes)
            {
                var mesh = SaveMesh((STGenericObject)obj, MeshIndex, skeleton, NodeArray);
                scene.Meshes.Add(mesh);
                MeshIndex++;
            }
            Node geomNode = new Node(Path.GetFileNameWithoutExtension(FileName), scene.RootNode);

            for (int ob = 0; ob < scene.MeshCount; ob++)
            {
                geomNode.MeshIndices.Add(ob);
            }
            scene.RootNode.Children.Add(geomNode);
        }
Example #7
0
        public void SaveFromModel(STGenericModel model, string FileName, List <STGenericTexture> Textures, STSkeleton skeleton = null, List <int> NodeArray = null)
        {
            Scene scene = new Scene();

            scene.RootNode = new Node("RootNode");

            SaveSkeleton(skeleton, scene.RootNode);
            SaveMaterials(scene, model, FileName, Textures);
            SaveMeshes(scene, model, skeleton, FileName, NodeArray);

            using (var v = new AssimpContext())
            {
                string ext = System.IO.Path.GetExtension(FileName);

                string formatID = "collada";
                if (ext == ".obj")
                {
                    formatID = "obj";
                }
                if (ext == ".3ds")
                {
                    formatID = "3ds";
                }
                if (ext == ".dae")
                {
                    formatID = "collada";
                }
                if (ext == ".ply")
                {
                    formatID = "ply";
                }

                if (v.ExportFile(scene, FileName, formatID, PostProcessSteps.ValidateDataStructure))
                {
                    MessageBox.Show($"Exported {FileName} Successfuly!");
                }
                else
                {
                    MessageBox.Show($"Failed to export {FileName}!");
                }
            }
        }
Example #8
0
        private static void SaveMaterials(StringBuilder writer, STGenericModel Model)
        {
            foreach (STGenericMaterial mat in Model.Nodes[1].Nodes)
            {
                writer.AppendLine($"newmtl {mat.Text}");
                writer.AppendLine($"Ns {Ns}");
                writer.AppendLine($"Ka {Ka.X} {Ka.Y} {Ka.Z}");
                writer.AppendLine($"Kd {Kd.X} {Kd.Y} {Kd.Z}");
                writer.AppendLine($"Ks {Ks.X} {Ks.Y} {Ks.Z}");
                writer.AppendLine($"Ke {Ke.X} {Ke.Y} {Ke.Z}");
                writer.AppendLine($"Ni {Ni}");
                writer.AppendLine($"d {d}");
                writer.AppendLine($"illum {illum}");

                foreach (var tex in mat.TextureMaps)
                {
                    if (tex.Type == STGenericMatTexture.TextureType.Diffuse)
                    {
                        writer.AppendLine($"map_Kd {tex.Name}.png");
                    }
                }
            }
        }
Example #9
0
        public void SaveFromModel(STGenericModel model, string FileName, List <STGenericTexture> Textures)
        {
            Scene scene = new Scene();

            scene.RootNode = new Node("RootNode");


            int MeshIndex = 0;

            foreach (var obj in model.Nodes[0].Nodes)
            {
                var genericObj = (STGenericObject)obj;

                Mesh mesh = new Mesh(genericObj.Text, PrimitiveType.Triangle);
                mesh.MaterialIndex = genericObj.MaterialIndex;

                List <Vector3D> textureCoords0 = new List <Vector3D>();
                List <Vector3D> textureCoords1 = new List <Vector3D>();
                List <Vector3D> textureCoords2 = new List <Vector3D>();
                List <Color4D>  vertexColors   = new List <Color4D>();
                foreach (Vertex v in genericObj.vertices)
                {
                    mesh.Vertices.Add(new Vector3D(v.pos.X, v.pos.Y, v.pos.Z));
                    mesh.Normals.Add(new Vector3D(v.nrm.X, v.nrm.Y, v.nrm.Z));
                    textureCoords0.Add(new Vector3D(v.uv0.X, v.uv0.Y, 0));
                    textureCoords1.Add(new Vector3D(v.uv1.X, v.uv1.Y, 0));
                    textureCoords2.Add(new Vector3D(v.uv2.X, v.uv2.Y, 0));
                    vertexColors.Add(new Color4D(v.col.X, v.col.Y, v.col.Z, v.col.W));
                    mesh.TextureCoordinateChannels[0] = textureCoords0;
                    mesh.TextureCoordinateChannels[1] = textureCoords1;
                    mesh.TextureCoordinateChannels[2] = textureCoords2;
                    mesh.VertexColorChannels[0]       = vertexColors;
                }
                List <int> faces = genericObj.lodMeshes[genericObj.DisplayLODIndex].faces;
                for (int f = 0; f < faces.Count; f++)
                {
                    mesh.Faces.Add(new Face(new int[] { faces[f++], faces[f++], faces[f] }));
                }

                mesh.TextureCoordinateChannels.SetValue(textureCoords0, 0);

                scene.Meshes.Add(mesh);

                MeshIndex++;
            }

            string TextureExtension = ".png";
            string TexturePath      = System.IO.Path.GetDirectoryName(FileName);

            foreach (var mat in model.Nodes[1].Nodes)
            {
                var genericMat = (STGenericMaterial)mat;

                Material material = new Material();
                material.Name = genericMat.Text;

                foreach (var tex in genericMat.TextureMaps)
                {
                    TextureSlot slot = new TextureSlot();
                    string      path = System.IO.Path.Combine(TexturePath, tex.Name + TextureExtension);
                    slot.FilePath     = path;
                    slot.UVIndex      = 0;
                    slot.Flags        = 0;
                    slot.TextureIndex = 0;
                    slot.BlendFactor  = 1.0f;
                    slot.Mapping      = TextureMapping.FromUV;
                    slot.Operation    = TextureOperation.Add;

                    if (tex.Type == STGenericMatTexture.TextureType.Diffuse)
                    {
                        slot.TextureType = TextureType.Diffuse;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Normal)
                    {
                        slot.TextureType = TextureType.Normals;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Specular)
                    {
                        slot.TextureType = TextureType.Specular;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Emission)
                    {
                        slot.TextureType = TextureType.Emissive;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Light)
                    {
                        slot.TextureType = TextureType.Lightmap;
                        slot.UVIndex     = 2;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Shadow)
                    {
                        slot.TextureType = TextureType.Ambient;
                        slot.UVIndex     = 1;
                    }
                    else
                    {
                        slot.TextureType = TextureType.Unknown;
                    }

                    if (tex.wrapModeS == 0)
                    {
                        slot.WrapModeU = TextureWrapMode.Wrap;
                    }
                    if (tex.wrapModeS == 1)
                    {
                        slot.WrapModeU = TextureWrapMode.Mirror;
                    }
                    if (tex.wrapModeS == 2)
                    {
                        slot.WrapModeU = TextureWrapMode.Clamp;
                    }
                    if (tex.wrapModeT == 0)
                    {
                        slot.WrapModeV = TextureWrapMode.Wrap;
                    }
                    if (tex.wrapModeT == 1)
                    {
                        slot.WrapModeV = TextureWrapMode.Mirror;
                    }
                    if (tex.wrapModeT == 2)
                    {
                        slot.WrapModeV = TextureWrapMode.Clamp;
                    }
                    else
                    {
                        slot.WrapModeU = TextureWrapMode.Wrap;
                        slot.WrapModeV = TextureWrapMode.Wrap;
                    }

                    material.AddMaterialTexture(ref slot);
                }
                scene.Materials.Add(material);
            }
            foreach (var tex in Textures)
            {
                string path = System.IO.Path.Combine(TexturePath, tex.Name + TextureExtension);
                if (!System.IO.File.Exists(path))
                {
                    tex.GetBitmap().Save(path);
                }
            }

            using (var v = new AssimpContext())
            {
                string ext = System.IO.Path.GetExtension(FileName);

                string formatID = "obj";
                if (ext == ".obj")
                {
                    formatID = "obj";
                }
                if (ext == ".fbx")
                {
                    formatID = "collada";
                }
                if (ext == ".dae")
                {
                    formatID = "collada";
                }

                if (v.ExportFile(scene, FileName, formatID, PostProcessSteps.ValidateDataStructure))
                {
                    System.Windows.Forms.MessageBox.Show($"Exported {FileName} Successfuly!");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show($"Failed to export {FileName}!");
                }
            }
        }
Example #10
0
        private void SaveMeshes(Scene scene, STGenericModel model, STSkeleton skeleton, string FileName, List <int> NodeArray)
        {
            int MeshIndex = 0;

            foreach (var obj in model.Nodes[0].Nodes)
            {
                var genericObj = (STGenericObject)obj;

                Mesh mesh = new Mesh(genericObj.Text, PrimitiveType.Triangle);
                mesh.MaterialIndex = genericObj.MaterialIndex;

                List <Vector3D> textureCoords0 = new List <Vector3D>();
                List <Vector3D> textureCoords1 = new List <Vector3D>();
                List <Vector3D> textureCoords2 = new List <Vector3D>();
                List <Color4D>  vertexColors   = new List <Color4D>();

                int vertexID = 0;
                foreach (Vertex v in genericObj.vertices)
                {
                    mesh.Vertices.Add(new Vector3D(v.pos.X, v.pos.Y, v.pos.Z));
                    mesh.Normals.Add(new Vector3D(v.nrm.X, v.nrm.Y, v.nrm.Z));
                    textureCoords0.Add(new Vector3D(v.uv0.X, v.uv0.Y, 0));
                    textureCoords1.Add(new Vector3D(v.uv1.X, v.uv1.Y, 0));
                    textureCoords2.Add(new Vector3D(v.uv2.X, v.uv2.Y, 0));
                    vertexColors.Add(new Color4D(v.col.X, v.col.Y, v.col.Z, v.col.W));
                    mesh.TextureCoordinateChannels[0] = textureCoords0;
                    mesh.TextureCoordinateChannels[1] = textureCoords1;
                    mesh.TextureCoordinateChannels[2] = textureCoords2;
                    mesh.VertexColorChannels[0]       = vertexColors;

                    for (int j = 0; j < v.boneIds.Count; j++)
                    {
                        if (j < genericObj.VertexSkinCount)
                        {
                            //Get the bone via the node array and bone index from the vertex
                            STBone STbone = skeleton.bones[NodeArray[v.boneIds[j]]];

                            //Find the index of a bone. If it doesn't exist then we add it
                            int boneInd = mesh.Bones.FindIndex(x => x.Name == STbone.Text);

                            if (boneInd == -1)
                            {
                                var matrices = Switch_Toolbox.Library.IO.MatrixExenstion.CalculateInverseMatrix(STbone);

                                //Set the inverse matrix
                                Matrix4x4 transform = matrices.inverse.FromNumerics();

                                //Create a new assimp bone
                                Bone bone = new Bone();
                                bone.Name         = STbone.Text;
                                bone.OffsetMatrix = transform;

                                mesh.Bones.Add(bone);
                                BoneNames.Add(bone.Name);

                                boneInd = mesh.Bones.IndexOf(bone); //Set the index of the bone for the vertex weight
                            }

                            //Check if the max amount of weights is higher than the current bone id
                            if (v.boneWeights.Count > j)
                            {
                                if (v.boneWeights[j] <= 1)
                                {
                                    mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, v.boneWeights[j]));
                                }
                                else
                                {
                                    mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, 1));
                                }
                            }
                            else
                            {
                                mesh.Bones[boneInd].VertexWeights.Add(new VertexWeight(vertexID, 1));
                            }
                        }
                    }
                    vertexID++;
                }
                List <int> faces = genericObj.lodMeshes[genericObj.DisplayLODIndex].faces;
                for (int f = 0; f < faces.Count; f++)
                {
                    mesh.Faces.Add(new Face(new int[] { faces[f++], faces[f++], faces[f] }));
                }

                mesh.TextureCoordinateChannels.SetValue(textureCoords0, 0);

                scene.Meshes.Add(mesh);

                MeshIndex++;
            }
            Node geomNode = new Node(Path.GetFileNameWithoutExtension(FileName), scene.RootNode);

            for (int ob = 0; ob < scene.MeshCount; ob++)
            {
                geomNode.MeshIndices.Add(ob);
            }
            scene.RootNode.Children.Add(geomNode);
        }
Example #11
0
        private void SaveMaterials(Scene scene, STGenericModel model, string FileName, List <STGenericTexture> Textures)
        {
            string TextureExtension = ".png";
            string TexturePath      = System.IO.Path.GetDirectoryName(FileName);

            foreach (var tex in Textures)
            {
                string path = System.IO.Path.Combine(TexturePath, tex.Text + TextureExtension);

                var bitmap = tex.GetBitmap();
                bitmap.Save(path);
                bitmap.Dispose();
            }

            foreach (var mat in model.Nodes[1].Nodes)
            {
                var genericMat = (STGenericMaterial)mat;

                Material material = new Material();
                material.Name = genericMat.Text;

                foreach (var tex in genericMat.TextureMaps)
                {
                    TextureSlot slot = new TextureSlot();
                    string      path = System.IO.Path.Combine(TexturePath, tex.Name + TextureExtension);

                    slot.FilePath     = path;
                    slot.UVIndex      = 0;
                    slot.Flags        = 0;
                    slot.TextureIndex = 0;
                    slot.BlendFactor  = 1.0f;
                    slot.Mapping      = TextureMapping.FromUV;
                    slot.Operation    = TextureOperation.Add;

                    if (tex.Type == STGenericMatTexture.TextureType.Diffuse)
                    {
                        slot.TextureType = TextureType.Diffuse;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Normal)
                    {
                        slot.TextureType = TextureType.Normals;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Specular)
                    {
                        slot.TextureType = TextureType.Specular;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Emission)
                    {
                        slot.TextureType = TextureType.Emissive;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Light)
                    {
                        slot.TextureType = TextureType.Lightmap;
                        slot.UVIndex     = 2;
                    }
                    else if (tex.Type == STGenericMatTexture.TextureType.Shadow)
                    {
                        slot.TextureType = TextureType.Ambient;
                        slot.UVIndex     = 1;
                    }
                    else
                    {
                        slot.TextureType = TextureType.Unknown;
                    }

                    if (tex.wrapModeS == 0)
                    {
                        slot.WrapModeU = TextureWrapMode.Wrap;
                    }
                    if (tex.wrapModeS == 1)
                    {
                        slot.WrapModeU = TextureWrapMode.Mirror;
                    }
                    if (tex.wrapModeS == 2)
                    {
                        slot.WrapModeU = TextureWrapMode.Clamp;
                    }
                    if (tex.wrapModeT == 0)
                    {
                        slot.WrapModeV = TextureWrapMode.Wrap;
                    }
                    if (tex.wrapModeT == 1)
                    {
                        slot.WrapModeV = TextureWrapMode.Mirror;
                    }
                    if (tex.wrapModeT == 2)
                    {
                        slot.WrapModeV = TextureWrapMode.Clamp;
                    }
                    else
                    {
                        slot.WrapModeU = TextureWrapMode.Wrap;
                        slot.WrapModeV = TextureWrapMode.Wrap;
                    }

                    material.AddMaterialTexture(ref slot);
                }
                scene.Materials.Add(material);
            }
        }