Exemple #1
0
        public void SaveFromModel(List <STGenericObject> Meshes, List <STGenericMaterial> Materials, 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          = "Exporting Skeleton...";
            progressBar.Value         = 0;
            progressBar.StartPosition = FormStartPosition.CenterScreen;
            progressBar.Show();
            progressBar.Refresh();

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

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

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

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

            SaveScene(FileName, scene, Meshes);

            progressBar.Value = 100;
            progressBar.Close();
            progressBar.Dispose();
        }
Exemple #2
0
        private void SaveMaterials(Scene scene, List <STGenericMaterial> Materials, 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  = $"Exporting Texture {Textures[i].Text}";
                    progressBar.Value = ((i * 100) / Textures.Count);
                    progressBar.Refresh();

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

                    GC.Collect();
                }
            }

            if (Materials.Count == 0)
            {
                Material material = new Material();
                material.Name = "New Material";
                scene.Materials.Add(material);
                return;
            }

            foreach (var mat in Materials)
            {
                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);
            }
        }
Exemple #3
0
        private void RepackAction(object sender, EventArgs args)
        {
            FolderSelectDialog dlg = new FolderSelectDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string FolderPath = dlg.SelectedPath;

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

                var ProccessedFiles = TreeHelper.ReadFiles(FolderPath);

                progressBar.Task = "Repacking Files...";
                progressBar.Refresh();

                ArchiveFile.ClearFiles();

                for (int i = 0; i < ProccessedFiles.Count; i++)
                {
                    progressBar.Value = (i * 100) / ProccessedFiles.Count;
                    progressBar.Task  = $"Packing {ProccessedFiles[i].Item1}";
                    progressBar.Refresh();

                    ArchiveFile.AddFile(new ArchiveFileInfo()
                    {
                        FileName = ProccessedFiles[i].Item1,
                        FileData = File.ReadAllBytes(ProccessedFiles[i].Item2),
                    });
                }

                progressBar.Close();
                progressBar.Dispose();
                ProccessedFiles.Clear();

                GC.Collect();

                FillTreeNodes();
            }
        }
Exemple #4
0
        //jakeroo123's animation adding code.
        public static void ExportAnimation(string FileName, Toolbox.Library.Animations.Animation anim = null, STSkeleton skeleton = null, List <int> NodeArray = null)
        {
            //Always use the same settings
            ExportSettings settings = new ExportSettings();

            settings.SuppressConfirmDialog = true;
            settings.UseMatrixTransform    = false;

            List <string> failedTextureExport = new List <string>();

            STProgressBar progressBar = new STProgressBar();

            progressBar.Task          = "Exporting Animation \"" + anim.Text + "\"...";
            progressBar.Value         = 0;
            progressBar.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            progressBar.Show();
            progressBar.Refresh();

            /* I don't see any way to use this
             * if (settings.UseOldExporter)
             * {
             *  AssimpSaver saver = new AssimpSaver();
             *  STGenericModel model = new STGenericModel();
             *  model.Objects = Meshes;
             *  model.Materials = Materials;
             *  saver.SaveFromModel(model, FileName, Textures, skeleton, NodeArray);
             *  return;
             * }
             */

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

            using (ColladaWriter writer = new ColladaWriter(FileName, settings))
            {
                writer.WriteAsset();

                //This is where the magic happens - Iterate through bones, and add animations.
                if (anim != null && skeleton != null)
                {
                    writer.StartLibraryAnimations();

                    float[] frames = new float[anim.FrameCount + 1];

                    for (int i = 0; i <= anim.FrameCount; i++)
                    {
                        frames[i] = i / 30f; //DAE appears to save frame times - This is saving the animation as 30 FPS, regardless of anything else.
                    }

                    string[] interp = new string[anim.FrameCount + 1];

                    for (int i = 0; i <= anim.FrameCount; i++)
                    {
                        interp[i] = "LINEAR"; //Just use linear.
                    }

                    foreach (STBone bone in skeleton.bones)
                    {
                        skeleton.reset();

                        string aid = anim.Text + "_" + bone.Text; //The prefix for animation

                        string bid = "Armature_" + bone.Text;

                        Vector3 defaultRotation = bone.EulerRotation;

                        //Three arrays, for
                        float[]   rotateX   = new float[anim.FrameCount + 1];
                        float[]   rotateY   = new float[anim.FrameCount + 1];
                        float[]   rotateZ   = new float[anim.FrameCount + 1];
                        Vector3[] scale     = new Vector3[anim.FrameCount + 1];
                        Vector3[] translate = new Vector3[anim.FrameCount + 1];

                        anim.SetFrame(0);
                        for (int i = 0; i <= anim.FrameCount; i++)
                        {
                            anim.NextFrame(skeleton, false, true, bone.Text);

                            //Rotation
                            Vector3 eul = STMath.ToEulerAngles(bone.rot);
                            rotateX[i] = eul.X * Rad2Deg; //- defaultRotation.X;
                            rotateY[i] = eul.Y * Rad2Deg; //- defaultRotation.Y;
                            rotateZ[i] = eul.Z * Rad2Deg; //- defaultRotation.Z;

                            //Scaling and translation
                            scale[i]     = bone.GetScale();
                            translate[i] = bone.GetPosition();
                        }

                        writer.WriteAnimationVector(aid + "_translate", frames, interp, translate, bid + "/location");

                        writer.WriteAnimationAngle(aid + "_rotateX", frames, interp, rotateX, bid + "/rotationX.ANGLE");

                        writer.WriteAnimationAngle(aid + "_rotateY", frames, interp, rotateY, bid + "/rotationY.ANGLE");

                        writer.WriteAnimationAngle(aid + "_rotateZ", frames, interp, rotateZ, bid + "/rotationZ.ANGLE");

                        writer.WriteAnimationVector(aid + "_scale", frames, interp, scale, bid + "/scale");
                    }

                    writer.EndLibraryAnimations();
                }

                skeleton.reset();

                //Don't bother exporting any textures - It's not really necessary, at this point.
                writer.WriteLibraryImages();

                if (skeleton != null)
                {
                    //Don't bother searching for rigging, because the mesh will simply be empty.

                    foreach (var bone in skeleton.bones)
                    {
                        //Set the inverse matrix
                        var inverse   = skeleton.GetBoneTransform(bone).Inverted();
                        var transform = bone.GetTransform();

                        float[] Transform = new float[] {
                            transform.M11, transform.M21, transform.M31, transform.M41,
                            transform.M12, transform.M22, transform.M32, transform.M42,
                            transform.M13, transform.M23, transform.M33, transform.M43,
                            transform.M14, transform.M24, transform.M34, transform.M44
                        };

                        float[] InvTransform = new float[] {
                            inverse.M11, inverse.M21, inverse.M31, inverse.M41,
                            inverse.M12, inverse.M22, inverse.M32, inverse.M42,
                            inverse.M13, inverse.M23, inverse.M33, inverse.M43,
                            inverse.M14, inverse.M24, inverse.M34, inverse.M44
                        };

                        writer.AddJoint(bone.Text, bone.parentIndex == -1 ? "" :
                                        skeleton.bones[bone.parentIndex].Text, Transform, InvTransform,
                                        new float[3] {
                            bone.Position.X, bone.Position.Y, bone.Position.Z
                        },
                                        new float[3] {
                            bone.EulerRotation.X *Rad2Deg, bone.EulerRotation.Y *Rad2Deg, bone.EulerRotation.Z *Rad2Deg
                        },
                                        new float[3] {
                            bone.Scale.X, bone.Scale.Y, bone.Scale.Z
                        });
                    }
                }


                writer.StartLibraryGeometries();
                //Try to not write meshes? Let's hope this works.
                writer.EndGeometrySection();
            }

            progressBar?.Close();
        }
Exemple #5
0
        public static void Export(string FileName, ExportSettings settings,
                                  List <STGenericObject> Meshes, List <STGenericMaterial> Materials,
                                  List <STGenericTexture> Textures, STSkeleton skeleton = null, List <int> NodeArray = null)
        {
            if (Materials == null)
            {
                Materials = new List <STGenericMaterial>();
            }
            if (skeleton != null && skeleton.BoneIndices != null)
            {
                NodeArray = skeleton.BoneIndices.ToList();
            }

            List <string> failedTextureExport = new List <string>();

            STProgressBar progressBar = new STProgressBar();

            progressBar.Task          = "Exporting Model...";
            progressBar.Value         = 0;
            progressBar.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            progressBar.Show();
            progressBar.Refresh();

            if (settings.UseOldExporter)
            {
                AssimpSaver    saver = new AssimpSaver();
                STGenericModel model = new STGenericModel();
                model.Objects   = Meshes;
                model.Materials = Materials;
                saver.SaveFromModel(model, FileName, Textures, skeleton, NodeArray);
                return;
            }

            string TexturePath = System.IO.Path.GetDirectoryName(FileName);
            Dictionary <string, STGenericMaterial> MaterialRemapper = new Dictionary <string, STGenericMaterial>();

            using (ColladaWriter writer = new ColladaWriter(FileName, settings))
            {
                writer.WriteAsset();

                if (Materials.Count > 0)
                {
                    List <string> textureNames = new List <string>();
                    for (int i = 0; i < Textures?.Count; i++)
                    {
                        if (!textureNames.Contains(Textures[i].Text))
                        {
                            textureNames.Add(Textures[i].Text);
                        }

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

                            try
                            {
                                var bitmap = Textures[i].GetBitmap();
                                if (bitmap != null)
                                {
                                    if (settings.UseTextureChannelComponents)
                                    {
                                        bitmap = Textures[i].GetComponentBitmap(bitmap);
                                    }
                                    string textureName = Textures[i].Text;
                                    if (textureName.RemoveIllegaleFileNameCharacters() != textureName)
                                    {
                                        string properName = textureName.RemoveIllegaleFileNameCharacters();
                                        for (int m = 0; m < Materials?.Count; m++)
                                        {
                                            foreach (var tex in Materials[m].TextureMaps)
                                            {
                                                if (tex.Name == textureName)
                                                {
                                                    tex.Name = properName;
                                                }
                                            }
                                        }

                                        textureName = properName;
                                    }

                                    bitmap.Save($"{TexturePath}/{textureName}.png");
                                    bitmap.Dispose();

                                    GC.Collect();
                                }
                            }
                            catch (Exception ex) {
                                failedTextureExport.Add(Textures[i].Text);
                            }
                        }
                    }

                    List <Material> materials = new List <Material>();
                    foreach (var mat in Materials)
                    {
                        Material material = new Material();
                        material.Name = mat.Text;

                        if (!MaterialRemapper.ContainsKey(mat.Text))
                        {
                            MaterialRemapper.Add(mat.Text, mat);
                        }
                        else
                        {
                            string name = Utils.RenameDuplicateString(MaterialRemapper.Keys.ToList(), mat.Text);
                            MaterialRemapper.Add(name, mat);
                            material.Name = name;
                        }

                        materials.Add(material);

                        foreach (var tex in mat.TextureMaps)
                        {
                            TextureMap texMap = new TextureMap();
                            texMap.Name = tex.Name;
                            if (tex.Type == STGenericMatTexture.TextureType.Diffuse)
                            {
                                texMap.Type = PhongTextureType.diffuse;
                            }
                            else if (tex.Type == STGenericMatTexture.TextureType.Normal)
                            {
                                texMap.Type = PhongTextureType.bump;
                            }
                            else if (tex.Type == STGenericMatTexture.TextureType.Specular)
                            {
                                texMap.Type = PhongTextureType.specular;
                            }
                            else if (tex.Type == STGenericMatTexture.TextureType.Emission)
                            {
                                texMap.Type = PhongTextureType.emission;
                            }
                            else
                            {
                                continue; //Skip adding unknown types
                            }
                            if (tex.WrapModeS == STTextureWrapMode.Repeat)
                            {
                                texMap.WrapModeS = SamplerWrapMode.WRAP;
                            }
                            else if (tex.WrapModeS == STTextureWrapMode.Mirror)
                            {
                                texMap.WrapModeS = SamplerWrapMode.MIRROR;
                            }
                            else if (tex.WrapModeS == STTextureWrapMode.Clamp)
                            {
                                texMap.WrapModeS = SamplerWrapMode.CLAMP;
                            }


                            if (tex.WrapModeT == STTextureWrapMode.Repeat)
                            {
                                texMap.WrapModeT = SamplerWrapMode.WRAP;
                            }
                            else if (tex.WrapModeT == STTextureWrapMode.Mirror)
                            {
                                texMap.WrapModeT = SamplerWrapMode.MIRROR;
                            }
                            else if (tex.WrapModeT == STTextureWrapMode.Clamp)
                            {
                                texMap.WrapModeT = SamplerWrapMode.CLAMP;
                            }


                            //If no textures are saved, still keep images references
                            //So the user can still dump textures after
                            if (Textures?.Count == 0 && !textureNames.Contains(texMap.Name))
                            {
                                textureNames.Add($"{texMap.Name}");
                            }

                            material.Textures.Add(texMap);
                        }
                    }

                    writer.WriteLibraryImages(textureNames.ToArray());

                    writer.WriteLibraryMaterials(materials);
                    writer.WriteLibraryEffects(materials);
                }
                else
                {
                    writer.WriteLibraryImages();
                }

                if (skeleton != null)
                {
                    //Search for bones with rigging first
                    List <string> riggedBones = new List <string>();
                    if (settings.OnlyExportRiggedBones)
                    {
                        for (int i = 0; i < Meshes.Count; i++)
                        {
                            for (int v = 0; v < Meshes[i].vertices.Count; v++)
                            {
                                var vertex = Meshes[i].vertices[v];
                                for (int j = 0; j < vertex.boneIds.Count; j++)
                                {
                                    int id = -1;
                                    if (NodeArray != null && NodeArray.Count > vertex.boneIds[j])
                                    {
                                        id = NodeArray[vertex.boneIds[j]];
                                    }
                                    else
                                    {
                                        id = vertex.boneIds[j];
                                    }

                                    if (id < skeleton.bones.Count && id != -1)
                                    {
                                        riggedBones.Add(skeleton.bones[id].Text);
                                    }
                                }
                            }
                        }
                    }

                    foreach (var bone in skeleton.bones)
                    {
                        if (settings.OnlyExportRiggedBones && !riggedBones.Contains(bone.Text))
                        {
                            Console.WriteLine("Skipping " + bone.Text);
                            continue;
                        }

                        //Set the inverse matrix
                        var inverse   = skeleton.GetBoneTransform(bone).Inverted();
                        var transform = bone.GetTransform();

                        float[] Transform = new float[] {
                            transform.M11, transform.M21, transform.M31, transform.M41,
                            transform.M12, transform.M22, transform.M32, transform.M42,
                            transform.M13, transform.M23, transform.M33, transform.M43,
                            transform.M14, transform.M24, transform.M34, transform.M44
                        };

                        float[] InvTransform = new float[] {
                            inverse.M11, inverse.M21, inverse.M31, inverse.M41,
                            inverse.M12, inverse.M22, inverse.M32, inverse.M42,
                            inverse.M13, inverse.M23, inverse.M33, inverse.M43,
                            inverse.M14, inverse.M24, inverse.M34, inverse.M44
                        };

                        writer.AddJoint(bone.Text, bone.parentIndex == -1 ? "" :
                                        skeleton.bones[bone.parentIndex].Text, Transform, InvTransform,
                                        new float[3] {
                            bone.Position.X, bone.Position.Y, bone.Position.Z
                        },
                                        new float[3] {
                            bone.EulerRotation.X *Rad2Deg, bone.EulerRotation.Y *Rad2Deg, bone.EulerRotation.Z *Rad2Deg
                        },
                                        new float[3] {
                            bone.Scale.X, bone.Scale.Y, bone.Scale.Z
                        });
                    }
                }

                int meshIndex = 0;

                writer.StartLibraryGeometries();
                foreach (var mesh in Meshes)
                {
                    progressBar.Task  = $"Exporting Mesh {mesh.Text}";
                    progressBar.Value = ((meshIndex++ *100) / Meshes.Count);
                    progressBar.Refresh();

                    int[] IndexTable = null;
                    if (NodeArray != null)
                    {
                        IndexTable = NodeArray.ToArray();
                    }

                    writer.StartGeometry(mesh.Text);

                    if (mesh.MaterialIndex != -1 && Materials.Count > mesh.MaterialIndex)
                    {
                        writer.CurrentMaterial = Materials[mesh.MaterialIndex].Text;
                        Console.WriteLine($"MaterialIndex {mesh.MaterialIndex } {Materials[mesh.MaterialIndex].Text}");
                    }


                    if (settings.TransformColorUVs)
                    {
                        List <Vertex> transformedVertices = new List <Vertex>();
                        foreach (var poly in mesh.PolygonGroups)
                        {
                            var mat = poly.Material;
                            if (mat == null)
                            {
                                continue;
                            }

                            var faces = poly.GetDisplayFace();
                            for (int v = 0; v < poly.displayFaceSize; v += 3)
                            {
                                if (faces.Count < v + 2)
                                {
                                    break;
                                }

                                var diffuse = mat.TextureMaps.FirstOrDefault(x => x.Type == STGenericMatTexture.TextureType.Diffuse);
                                STTextureTransform transform = new STTextureTransform();
                                if (diffuse != null)
                                {
                                    transform = diffuse.Transform;
                                }

                                var vertexA = mesh.vertices[faces[v]];
                                var vertexB = mesh.vertices[faces[v + 1]];
                                var vertexC = mesh.vertices[faces[v + 2]];

                                if (!transformedVertices.Contains(vertexA))
                                {
                                    vertexA.uv0 = (vertexA.uv0 * transform.Scale) + transform.Translate;
                                    transformedVertices.Add(vertexA);
                                }
                                if (!transformedVertices.Contains(vertexB))
                                {
                                    vertexB.uv0 = (vertexB.uv0 * transform.Scale) + transform.Translate;
                                    transformedVertices.Add(vertexB);
                                }
                                if (!transformedVertices.Contains(vertexC))
                                {
                                    vertexC.uv0 = (vertexC.uv0 * transform.Scale) + transform.Translate;
                                    transformedVertices.Add(vertexC);
                                }
                            }
                        }
                    }

                    // collect sources
                    List <float>   Position    = new List <float>();
                    List <float>   Normal      = new List <float>();
                    List <float>   UV0         = new List <float>();
                    List <float>   UV1         = new List <float>();
                    List <float>   UV2         = new List <float>();
                    List <float>   UV3         = new List <float>();
                    List <float>   Color       = new List <float>();
                    List <float>   Color2      = new List <float>();
                    List <int[]>   BoneIndices = new List <int[]>();
                    List <float[]> BoneWeights = new List <float[]>();

                    bool HasNormals = false;
                    bool HasColors  = false;
                    bool HasColors2 = false;
                    bool HasUV0     = false;
                    bool HasUV1     = false;
                    bool HasUV2     = false;
                    bool HasBoneIds = false;

                    foreach (var vertex in mesh.vertices)
                    {
                        if (vertex.nrm != Vector3.Zero)
                        {
                            HasNormals = true;
                        }
                        if (vertex.col != Vector4.One && settings.UseVertexColors)
                        {
                            HasColors = true;
                        }
                        if (vertex.col2 != Vector4.One && settings.UseVertexColors)
                        {
                            HasColors2 = true;
                        }
                        if (vertex.uv0 != Vector2.Zero)
                        {
                            HasUV0 = true;
                        }
                        if (vertex.uv1 != Vector2.Zero)
                        {
                            HasUV1 = true;
                        }
                        if (vertex.uv2 != Vector2.Zero)
                        {
                            HasUV2 = true;
                        }
                        if (vertex.boneIds.Count > 0)
                        {
                            HasBoneIds = true;
                        }

                        Position.Add(vertex.pos.X); Position.Add(vertex.pos.Y); Position.Add(vertex.pos.Z);
                        Normal.Add(vertex.nrm.X); Normal.Add(vertex.nrm.Y); Normal.Add(vertex.nrm.Z);

                        if (settings.FlipTexCoordsVertical)
                        {
                            UV0.Add(vertex.uv0.X); UV0.Add(1 - vertex.uv0.Y);
                            UV1.Add(vertex.uv1.X); UV1.Add(1 - vertex.uv1.Y);
                            UV2.Add(vertex.uv2.X); UV2.Add(1 - vertex.uv2.Y);
                        }
                        else
                        {
                            UV0.Add(vertex.uv0.X); UV0.Add(vertex.uv0.Y);
                            UV1.Add(vertex.uv1.X); UV1.Add(vertex.uv1.Y);
                            UV2.Add(vertex.uv2.X); UV2.Add(vertex.uv2.Y);
                        }

                        Color.AddRange(new float[] { vertex.col.X, vertex.col.Y, vertex.col.Z, vertex.col.W });
                        Color2.AddRange(new float[] { vertex.col2.X, vertex.col2.Y, vertex.col2.Z, vertex.col2.W });

                        List <int>   bIndices = new List <int>();
                        List <float> bWeights = new List <float>();
                        for (int b = 0; b < vertex.boneIds.Count; b++)
                        {
                            if (b > mesh.VertexSkinCount - 1)
                            {
                                continue;
                            }

                            //Skip 0 weights
                            if (vertex.boneWeights.Count > b)
                            {
                                if (vertex.boneWeights[b] == 0)
                                {
                                    continue;
                                }
                            }

                            int index = -1;
                            if (IndexTable != null)
                            {
                                index = (int)IndexTable[vertex.boneIds[b]];
                            }
                            else
                            {
                                index = (int)vertex.boneIds[b];
                            }

                            //Only map for valid weights/indices
                            bool hasValidIndex  = index != -1 && index < skeleton?.bones.Count;
                            bool hasValidWeight = vertex.boneWeights.Count > b;
                            if (hasValidIndex)
                            {
                                bIndices.Add(index);
                            }

                            if (hasValidWeight && hasValidIndex)
                            {
                                bWeights.Add(vertex.boneWeights[b]);
                            }
                        }
                        //Rigid bodies with no direct bone indices
                        if (bIndices.Count == 0 && mesh.BoneIndex != -1)
                        {
                            HasBoneIds = true;
                            bIndices.Add(mesh.BoneIndex);
                            bWeights.Add(1);
                        }
                        //Bone indices with no weights directly mapped
                        if (bWeights.Count == 0 && bIndices.Count > 0)
                        {
                            bWeights.Add(1.0f);
                        }

                        BoneIndices.Add(bIndices.ToArray());
                        BoneWeights.Add(bWeights.ToArray());
                    }

                    List <TriangleList> triangleLists = new List <TriangleList>();
                    if (mesh.lodMeshes.Count > 0)
                    {
                        TriangleList triangleList = new TriangleList();
                        triangleLists.Add(triangleList);

                        var lodMesh = mesh.lodMeshes[mesh.DisplayLODIndex];

                        List <int> faces = new List <int>();
                        if (lodMesh.PrimativeType == STPrimitiveType.TrangleStrips)
                        {
                            faces = STGenericObject.ConvertTriangleStripsToTriangles(lodMesh.faces);
                        }
                        else
                        {
                            faces = lodMesh.faces;
                        }

                        for (int i = 0; i < faces.Count; i++)
                        {
                            triangleList.Indices.Add((uint)faces[i]);
                        }
                    }
                    if (mesh.PolygonGroups.Count > 0)
                    {
                        foreach (var group in mesh.PolygonGroups)
                        {
                            TriangleList triangleList = new TriangleList();

                            triangleLists.Add(triangleList);

                            STGenericMaterial material = new STGenericMaterial();

                            if (group.MaterialIndex != -1 && Materials.Count > group.MaterialIndex)
                            {
                                material = Materials[group.MaterialIndex];
                            }

                            if (group.Material != null)
                            {
                                material = group.Material;
                            }

                            if (MaterialRemapper.Values.Any(x => x == material))
                            {
                                var key = MaterialRemapper.FirstOrDefault(x => x.Value == material).Key;
                                triangleList.Material = key;
                            }
                            else if (material.Text != string.Empty)
                            {
                                triangleList.Material = material.Text;
                            }

                            List <int> faces = new List <int>();
                            if (group.PrimativeType == STPrimitiveType.TrangleStrips)
                            {
                                faces = STGenericObject.ConvertTriangleStripsToTriangles(group.faces);
                            }
                            else
                            {
                                faces = group.faces;
                            }

                            for (int i = 0; i < faces.Count; i++)
                            {
                                triangleList.Indices.Add((uint)faces[i]);
                            }
                        }
                    }

                    // write sources
                    writer.WriteGeometrySource(mesh.Text, SemanticType.POSITION, Position.ToArray(), triangleLists.ToArray());

                    if (HasNormals)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.NORMAL, Normal.ToArray(), triangleLists.ToArray());
                    }

                    if (HasColors)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.COLOR, Color.ToArray(), triangleLists.ToArray(), 0);
                    }

                    if (HasColors2)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.COLOR, Color2.ToArray(), triangleLists.ToArray(), 1);
                    }

                    if (HasUV0)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.TEXCOORD, UV0.ToArray(), triangleLists.ToArray(), 0);
                    }

                    if (HasUV1)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.TEXCOORD, UV1.ToArray(), triangleLists.ToArray(), 1);
                    }

                    if (HasUV2)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.TEXCOORD, UV2.ToArray(), triangleLists.ToArray(), 2);
                    }

                    if (HasBoneIds)
                    {
                        writer.AttachGeometryController(BoneIndices, BoneWeights);
                    }

                    writer.EndGeometryMesh();
                }
                writer.EndGeometrySection();
            }

            progressBar?.Close();

            if (!settings.SuppressConfirmDialog)
            {
                System.Windows.Forms.MessageBox.Show($"Exported {FileName} Successfuly!");
            }
        }
Exemple #6
0
        public static string[] ExtractAllFiles(string ParentPath, TreeNodeCollection Nodes, string overridePath = "")
        {
            List <string> filesExtracted = new List <string>();

            if (overridePath == string.Empty)
            {
                FolderSelectDialog folderDialog = new FolderSelectDialog();
                if (folderDialog.ShowDialog() != DialogResult.OK)
                {
                    return(new string[0]);
                }

                overridePath = folderDialog.SelectedPath;
            }

            STProgressBar progressBar = new STProgressBar();

            progressBar.Task = "Extracing Files...";
            progressBar.Refresh();
            progressBar.Value         = 0;
            progressBar.StartPosition = FormStartPosition.CenterScreen;
            progressBar.Show();

            var Collection = TreeViewExtensions.Collect(Nodes);

            int Curfile = 0;

            foreach (TreeNode file in Collection)
            {
                if (file is ArchiveFileWrapper)
                {
                    string FilePath      = ((ArchiveFileWrapper)file).ArchiveFileInfo.FileName;
                    string FolderPath    = Path.GetDirectoryName(FilePath.RemoveIllegaleFolderNameCharacters());
                    string FolderPathDir = Path.Combine(overridePath, FolderPath);

                    if (!Directory.Exists(FolderPathDir))
                    {
                        Directory.CreateDirectory(FolderPathDir);
                    }

                    string FileName = file.Text.RemoveIllegaleFileNameCharacters();

                    FilePath = Path.Combine(FolderPath, FileName);

                    if (ParentPath != string.Empty)
                    {
                        FilePath = FilePath.Replace(ParentPath, string.Empty);
                    }

                    var path = Path.Combine(overridePath, FilePath);

                    progressBar.Task  = $"Extracting File {file}";
                    progressBar.Value = (Curfile++ *100) / Collection.Count();
                    progressBar.Refresh();
                    CreateDirectoryIfExists($"{path}");

                    if (file is ArchiveFileWrapper)
                    {
                        filesExtracted.Add($"{path}");

                        File.WriteAllBytes($"{path}",
                                           ((ArchiveFileWrapper)file).ArchiveFileInfo.FileData);
                    }
                }
            }

            progressBar.Value = 100;
            progressBar.Refresh();
            progressBar.Close();

            return(filesExtracted.ToArray());
        }
Exemple #7
0
        public static void Export(string FileName, ExportSettings settings,
                                  List <STGenericObject> Meshes, List <STGenericMaterial> Materials,
                                  List <STGenericTexture> Textures, STSkeleton skeleton = null, List <int> NodeArray = null)
        {
            if (Materials == null)
            {
                Materials = new List <STGenericMaterial>();
            }

            List <string> failedTextureExport = new List <string>();

            STProgressBar progressBar = new STProgressBar();

            progressBar.Task          = "Exporting Model...";
            progressBar.Value         = 0;
            progressBar.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            progressBar.Show();
            progressBar.Refresh();

            if (settings.UseOldExporter)
            {
                AssimpSaver    saver = new AssimpSaver();
                STGenericModel model = new STGenericModel();
                model.Objects   = Meshes;
                model.Materials = Materials;
                saver.SaveFromModel(model, FileName, Textures, skeleton, NodeArray);
                return;
            }

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

            using (ColladaWriter writer = new ColladaWriter(FileName, settings))
            {
                writer.WriteAsset();

                if (Materials.Count > 0)
                {
                    List <string> textureNames = new List <string>();
                    for (int i = 0; i < Textures?.Count; i++)
                    {
                        textureNames.Add(Textures[i].Text);

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

                            try
                            {
                                var bitmap = Textures[i].GetBitmap();
                                if (bitmap != null)
                                {
                                    string textureName = Textures[i].Text;
                                    if (textureName.RemoveIllegaleFileNameCharacters() != textureName)
                                    {
                                        string properName = textureName.RemoveIllegaleFileNameCharacters();
                                        for (int m = 0; m < Materials?.Count; m++)
                                        {
                                            foreach (var tex in Materials[m].TextureMaps)
                                            {
                                                if (tex.Name == textureName)
                                                {
                                                    tex.Name = properName;
                                                }
                                            }
                                        }

                                        textureName = properName;
                                    }

                                    bitmap.Save($"{TexturePath}/{textureName}.png");
                                    bitmap.Dispose();

                                    GC.Collect();
                                }
                            }
                            catch (Exception ex) {
                                failedTextureExport.Add(Textures[i].Text);
                            }
                        }
                    }

                    List <Material> materials = new List <Material>();
                    foreach (var mat in Materials)
                    {
                        Material material = new Material();
                        material.Name = mat.Text;
                        materials.Add(material);

                        foreach (var tex in mat.TextureMaps)
                        {
                            TextureMap texMap = new TextureMap();
                            texMap.Name = tex.Name;
                            if (tex.Type == STGenericMatTexture.TextureType.Diffuse)
                            {
                                texMap.Type = PhongTextureType.diffuse;
                            }
                            else if (tex.Type == STGenericMatTexture.TextureType.Normal)
                            {
                                texMap.Type = PhongTextureType.bump;
                            }
                            else if (tex.Type == STGenericMatTexture.TextureType.Specular)
                            {
                                texMap.Type = PhongTextureType.specular;
                            }
                            else if (tex.Type == STGenericMatTexture.TextureType.Emission)
                            {
                                texMap.Type = PhongTextureType.emission;
                            }
                            else
                            {
                                continue; //Skip adding unknown types
                            }
                            if (tex.WrapModeS == STTextureWrapMode.Repeat)
                            {
                                texMap.WrapModeS = SamplerWrapMode.WRAP;
                            }
                            else if (tex.WrapModeS == STTextureWrapMode.Mirror)
                            {
                                texMap.WrapModeS = SamplerWrapMode.MIRROR;
                            }
                            else if (tex.WrapModeS == STTextureWrapMode.Clamp)
                            {
                                texMap.WrapModeS = SamplerWrapMode.CLAMP;
                            }


                            if (tex.WrapModeT == STTextureWrapMode.Repeat)
                            {
                                texMap.WrapModeT = SamplerWrapMode.WRAP;
                            }
                            else if (tex.WrapModeT == STTextureWrapMode.Mirror)
                            {
                                texMap.WrapModeT = SamplerWrapMode.MIRROR;
                            }
                            else if (tex.WrapModeT == STTextureWrapMode.Clamp)
                            {
                                texMap.WrapModeT = SamplerWrapMode.CLAMP;
                            }


                            //If no textures are saved, still keep images references
                            //So the user can still dump textures after
                            if (Textures?.Count == 0)
                            {
                                textureNames.Add($"{texMap.Name}");
                            }

                            material.Textures.Add(texMap);
                        }
                    }

                    writer.WriteLibraryImages(textureNames.ToArray());

                    writer.WriteLibraryMaterials(materials);
                    writer.WriteLibraryEffects(materials);
                }
                else
                {
                    writer.WriteLibraryImages();
                }

                if (skeleton != null)
                {
                    foreach (var bone in skeleton.bones)
                    {
                        //Set the inverse matrix
                        var inverse   = skeleton.GetBoneTransform(bone).Inverted();
                        var transform = bone.GetTransform();

                        float[] Transform = new float[] {
                            transform.M11, transform.M21, transform.M31, transform.M41,
                            transform.M12, transform.M22, transform.M32, transform.M42,
                            transform.M13, transform.M23, transform.M33, transform.M43,
                            transform.M14, transform.M24, transform.M34, transform.M44
                        };

                        float[] InvTransform = new float[] {
                            inverse.M11, inverse.M21, inverse.M31, inverse.M41,
                            inverse.M12, inverse.M22, inverse.M32, inverse.M42,
                            inverse.M13, inverse.M23, inverse.M33, inverse.M43,
                            inverse.M14, inverse.M24, inverse.M34, inverse.M44
                        };

                        writer.AddJoint(bone.Text, bone.parentIndex == -1 ? "" :
                                        skeleton.bones[bone.parentIndex].Text, Transform, InvTransform);
                    }
                }

                int meshIndex = 0;

                writer.StartLibraryGeometries();
                foreach (var mesh in Meshes)
                {
                    progressBar.Task  = $"Exporting Mesh {mesh.Text}";
                    progressBar.Value = ((meshIndex++ *100) / Meshes.Count);
                    progressBar.Refresh();

                    int[] IndexTable = null;
                    if (NodeArray != null)
                    {
                        IndexTable = NodeArray.ToArray();
                    }

                    writer.StartGeometry(mesh.Text);

                    if (mesh.MaterialIndex != -1 && Materials.Count > mesh.MaterialIndex)
                    {
                        writer.CurrentMaterial = Materials[mesh.MaterialIndex].Text;
                    }

                    // collect sources
                    List <float>   Position    = new List <float>();
                    List <float>   Normal      = new List <float>();
                    List <float>   UV0         = new List <float>();
                    List <float>   UV1         = new List <float>();
                    List <float>   UV2         = new List <float>();
                    List <float>   UV3         = new List <float>();
                    List <float>   Color       = new List <float>();
                    List <int[]>   BoneIndices = new List <int[]>();
                    List <float[]> BoneWeights = new List <float[]>();

                    bool HasNormals = false;
                    bool HasColors  = false;
                    bool HasUV0     = false;
                    bool HasUV1     = false;
                    bool HasUV2     = false;
                    bool HasBoneIds = false;

                    foreach (var vertex in mesh.vertices)
                    {
                        //Remove zero weights
                        if (settings.OptmizeZeroWeights)
                        {
                            float MaxWeight = 1;
                            for (int i = 0; i < 4; i++)
                            {
                                if (vertex.boneWeights.Count <= i)
                                {
                                    continue;
                                }

                                if (vertex.boneIds.Count < i + 1)
                                {
                                    vertex.boneWeights[i] = 0;
                                    MaxWeight             = 0;
                                }
                                else
                                {
                                    float weight = vertex.boneWeights[i];
                                    if (vertex.boneWeights.Count == i + 1)
                                    {
                                        weight = MaxWeight;
                                    }

                                    if (weight >= MaxWeight)
                                    {
                                        weight    = MaxWeight;
                                        MaxWeight = 0;
                                    }
                                    else
                                    {
                                        MaxWeight -= weight;
                                    }

                                    vertex.boneWeights[i] = weight;
                                }
                            }
                        }


                        if (vertex.nrm != Vector3.Zero)
                        {
                            HasNormals = true;
                        }
                        if (vertex.col != Vector4.One && settings.UseVertexColors)
                        {
                            HasColors = true;
                        }
                        if (vertex.uv0 != Vector2.Zero)
                        {
                            HasUV0 = true;
                        }
                        if (vertex.uv1 != Vector2.Zero)
                        {
                            HasUV1 = true;
                        }
                        if (vertex.uv2 != Vector2.Zero)
                        {
                            HasUV2 = true;
                        }
                        if (vertex.boneIds.Count > 0)
                        {
                            HasBoneIds = true;
                        }

                        Position.Add(vertex.pos.X); Position.Add(vertex.pos.Y); Position.Add(vertex.pos.Z);
                        Normal.Add(vertex.nrm.X); Normal.Add(vertex.nrm.Y); Normal.Add(vertex.nrm.Z);

                        if (settings.FlipTexCoordsVertical)
                        {
                            UV0.Add(vertex.uv0.X); UV0.Add(1 - vertex.uv0.Y);
                            UV1.Add(vertex.uv1.X); UV1.Add(1 - vertex.uv1.Y);
                            UV2.Add(vertex.uv2.X); UV2.Add(1 - vertex.uv2.Y);
                        }
                        else
                        {
                            UV0.Add(vertex.uv0.X); UV0.Add(vertex.uv0.Y);
                            UV1.Add(vertex.uv1.X); UV1.Add(vertex.uv1.Y);
                            UV2.Add(vertex.uv2.X); UV2.Add(vertex.uv2.Y);
                        }

                        Color.AddRange(new float[] { vertex.col.X, vertex.col.Y, vertex.col.Z, vertex.col.W });

                        List <int>   bIndices = new List <int>();
                        List <float> bWeights = new List <float>();
                        for (int b = 0; b < vertex.boneIds.Count; b++)
                        {
                            if (b > mesh.VertexSkinCount - 1)
                            {
                                continue;
                            }

                            if (vertex.boneWeights.Count > b)
                            {
                                if (vertex.boneWeights[b] == 0)
                                {
                                    continue;
                                }
                            }

                            int index = -1;
                            if (IndexTable != null)
                            {
                                index = (int)IndexTable[vertex.boneIds[b]];
                            }
                            else
                            {
                                index = (int)vertex.boneIds[b];
                            }

                            if (index != -1 && index < skeleton?.bones.Count)
                            {
                                bIndices.Add(index);
                            }

                            //Some models may only use indices (single bind, rigid skin)
                            if (vertex.boneWeights.Count > b)
                            {
                                bWeights.Add(vertex.boneWeights[b]);
                            }
                            else
                            {
                                bWeights.Add(1);
                            }
                        }

                        if (bIndices.Count == 0 && mesh.BoneIndex != -1)
                        {
                            HasBoneIds = true;
                            bIndices.Add(mesh.BoneIndex);
                            bWeights.Add(1);
                        }

                        BoneIndices.Add(bIndices.ToArray());
                        BoneWeights.Add(bWeights.ToArray());
                    }

                    List <TriangleList> triangleLists = new List <TriangleList>();
                    if (mesh.lodMeshes.Count > 0)
                    {
                        TriangleList triangleList = new TriangleList();
                        triangleLists.Add(triangleList);

                        var lodMesh = mesh.lodMeshes[mesh.DisplayLODIndex];

                        List <int> faces = new List <int>();
                        if (lodMesh.PrimativeType == STPrimitiveType.TrangleStrips)
                        {
                            faces = STGenericObject.ConvertTriangleStripsToTriangles(lodMesh.faces);
                        }
                        else
                        {
                            faces = lodMesh.faces;
                        }

                        for (int i = 0; i < faces.Count; i++)
                        {
                            triangleList.Indices.Add((uint)faces[i]);
                        }
                    }
                    if (mesh.PolygonGroups.Count > 0)
                    {
                        foreach (var group in mesh.PolygonGroups)
                        {
                            TriangleList triangleList = new TriangleList();

                            triangleLists.Add(triangleList);

                            if (group.MaterialIndex != -1 && Materials.Count > group.MaterialIndex)
                            {
                                triangleList.Material = Materials[group.MaterialIndex].Text;
                            }

                            List <int> faces = new List <int>();
                            if (group.PrimativeType == STPrimitiveType.TrangleStrips)
                            {
                                faces = STGenericObject.ConvertTriangleStripsToTriangles(group.faces);
                            }
                            else
                            {
                                faces = group.faces;
                            }

                            for (int i = 0; i < faces.Count; i++)
                            {
                                triangleList.Indices.Add((uint)faces[i]);
                            }
                        }
                    }

                    // write sources
                    writer.WriteGeometrySource(mesh.Text, SemanticType.POSITION, Position.ToArray(), triangleLists.ToArray());

                    if (HasNormals)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.NORMAL, Normal.ToArray(), triangleLists.ToArray());
                    }

                    if (HasColors)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.COLOR, Color.ToArray(), triangleLists.ToArray());
                    }

                    if (HasUV0)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.TEXCOORD, UV0.ToArray(), triangleLists.ToArray(), 0);
                    }

                    if (HasUV1)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.TEXCOORD, UV1.ToArray(), triangleLists.ToArray(), 1);
                    }

                    if (HasUV2)
                    {
                        writer.WriteGeometrySource(mesh.Text, SemanticType.TEXCOORD, UV2.ToArray(), triangleLists.ToArray(), 2);
                    }

                    if (HasBoneIds)
                    {
                        writer.AttachGeometryController(BoneIndices, BoneWeights);
                    }

                    writer.EndGeometryMesh();
                }
                writer.EndGeometrySection();
            }

            progressBar?.Close();

            if (!settings.SuppressConfirmDialog)
            {
                System.Windows.Forms.MessageBox.Show($"Exported {FileName} Successfuly!");
            }
        }
Exemple #8
0
        public static string[] ExtractAllFiles(string ParentPath, TreeNodeCollection Nodes, string overridePath = "")
        {
            List <string> filesExtracted = new List <string>();

            if (overridePath == string.Empty)
            {
                FolderSelectDialog folderDialog = new FolderSelectDialog();
                if (folderDialog.ShowDialog() != DialogResult.OK)
                {
                    return(new string[0]);
                }

                overridePath = folderDialog.SelectedPath;
            }

            STProgressBar progressBar = new STProgressBar();

            progressBar.Task = "Extracing Files...";
            progressBar.Refresh();
            progressBar.Value         = 0;
            progressBar.StartPosition = FormStartPosition.CenterScreen;
            progressBar.Show();

            Thread Thread = new Thread((ThreadStart)(() =>
            {
                var Collection = TreeViewExtensions.Collect(Nodes).ToList();
                Console.WriteLine($"Collection {Collection.Count}");

                int Curfile = 0;
                foreach (TreeNode node in Collection)
                {
                    if (progressBar.IsDisposed || progressBar.Disposing)
                    {
                        break;
                    }

                    ArchiveFileInfo file = null;

                    if (node.Tag != null && node.Tag is ArchiveFileInfo)
                    {
                        file = (ArchiveFileInfo)node.Tag;
                    }
                    else if (node is ArchiveFileWrapper)
                    {
                        file = ((ArchiveFileWrapper)node).ArchiveFileInfo;
                    }

                    if (file != null)
                    {
                        string FilePath = file.FileName;
                        string FolderPath = Path.GetDirectoryName(FilePath.RemoveIllegaleFolderNameCharacters());
                        string FolderPathDir = Path.Combine(overridePath, FolderPath);

                        if (!Directory.Exists(FolderPathDir))
                        {
                            Directory.CreateDirectory(FolderPathDir);
                        }

                        string FileName = Path.GetFileName(file.FileName).RemoveIllegaleFileNameCharacters();

                        FilePath = Path.Combine(FolderPath, FileName);

                        if (ParentPath != string.Empty)
                        {
                            FilePath = FilePath.Replace(ParentPath, string.Empty);
                        }

                        var path = $"{overridePath}/{FilePath}";

                        if (progressBar.InvokeRequired)
                        {
                            progressBar.Invoke((MethodInvoker) delegate {
                                // Running on the UI thread
                                progressBar.Task = $"Extracting File {FileName}";
                                progressBar.Value = (Curfile++ *100) / Collection.Count;
                                progressBar.Refresh();
                            });
                        }

                        CreateDirectoryIfExists($"{path}");

                        filesExtracted.Add($"{path}");

                        if (file.FileFormat != null && file.FileFormat.CanSave)
                        {
                            file.SaveFileFormat();
                        }

                        if (file.FileDataStream != null)
                        {
                            file.FileDataStream.ExportToFile(path);
                        }
                        else
                        {
                            File.WriteAllBytes($"{path}", file.FileData);
                        }
                    }
                }

                if (progressBar.InvokeRequired)
                {
                    progressBar.Invoke((MethodInvoker) delegate {
                        progressBar.Value = 100;
                        progressBar.Refresh();
                        progressBar.Close();
                    });
                }
                else
                {
                    progressBar.Value = 100;
                    progressBar.Refresh();
                    progressBar.Close();
                }
            }));

            Thread.Start();

            return(filesExtracted.ToArray());
        }