Ejemplo n.º 1
0
        private static void SaveMesh(StringBuilder writer, STGenericObject mesh, STGenericMaterial mat, int VertexCount)
        {
            writer.AppendLine($"o {mesh.Text}");
            writer.AppendLine($"g {mesh.Text}");

            foreach (var v in mesh.vertices)
            {
                writer.AppendLine($"v {v.pos.X} {v.pos.Y} {v.pos.Z}");
                writer.AppendLine($"vn {v.nrm.X} {v.nrm.Y} {v.nrm.Z}");
                writer.AppendLine($"vt {v.uv0.X} {v.uv0.Y}");
            }

            if (mat != null)
            {
                writer.AppendLine($"usemtl {mat.Text}");
            }

            for (int i = 0; i < mesh.faces.Count; i++)
            {
                int[] indices = new int[3]
                {
                    mesh.faces[i++],
                    mesh.faces[i++],
                    mesh.faces[i]
                };

                writer.AppendLine($"f {indices[0] + VertexCount}/{indices[0] + VertexCount}/{indices[0] + VertexCount}" +
                                  $" {indices[1] + VertexCount}/{indices[1] + VertexCount}/{indices[1] + VertexCount}" +
                                  $" {indices[2] + VertexCount}/{indices[2] + VertexCount}/{indices[2] + VertexCount}");
            }

            VertexCount += mesh.vertices.Count;
        }
Ejemplo n.º 2
0
        private List <STGenericObject> CreateGenericObjects(string Name, library_geometries Geometries)
        {
            List <STGenericObject> objects = new List <STGenericObject>();

            foreach (var geom in Geometries.geometry)
            {
                var daeMesh = geom.Item as mesh;
                if (daeMesh == null)
                {
                    continue;
                }

                STGenericObject mesh = new STGenericObject();
                mesh.ObjectName = Name;

                foreach (var source in daeMesh.source)
                {
                    var float_array = source.Item as float_array;
                    if (float_array == null)
                    {
                        continue;
                    }
                }
                objects.Add(mesh);
            }
            return(objects);
        }
Ejemplo n.º 3
0
        public List<Vertex> GetVertices(Mesh msh, Matrix4 transform, STGenericObject STobj)
        {

            List<Vertex> vertices = new List<Vertex>();
            for (int v = 0; v < msh.VertexCount; v++)
            {
                Vertex vert = new Vertex();

                if (msh.HasVertices)
                    vert.pos = Vector3.TransformPosition(FromVector(msh.Vertices[v]), transform);
                if (msh.HasNormals)
                    vert.nrm = Vector3.TransformNormal(FromVector(msh.Normals[v]), transform);
                if (msh.HasTextureCoords(0))
                    vert.uv0 = new Vector2(msh.TextureCoordinateChannels[0][v].X, msh.TextureCoordinateChannels[0][v].Y);
                if (msh.HasTextureCoords(1))
                    vert.uv1 = new Vector2(msh.TextureCoordinateChannels[1][v].X, msh.TextureCoordinateChannels[1][v].Y);
                if (msh.HasTextureCoords(2))
                    vert.uv2 = new Vector2(msh.TextureCoordinateChannels[2][v].X, msh.TextureCoordinateChannels[2][v].Y);
                if (msh.HasTangentBasis)
                    vert.tan = new Vector4(msh.Tangents[v].X, msh.Tangents[v].Y, msh.Tangents[v].Z, 1);
                if (msh.HasVertexColors(0))
                    vert.col = new Vector4(msh.VertexColorChannels[0][v].R, msh.VertexColorChannels[0][v].G, msh.VertexColorChannels[0][v].B, msh.VertexColorChannels[0][v].A);
                if (msh.HasTangentBasis)
                    vert.bitan = new Vector4(msh.BiTangents[v].X, msh.BiTangents[v].Y, msh.BiTangents[v].Z, 1);
                if (msh.HasBones)
                {
                    foreach (Bone bn in msh.Bones)
                    {
                  
                    }
                }
                vertices.Add(vert);
            }
            if (msh.HasBones)
            {
                for (int i = 0; i < msh.BoneCount; i++)
                {
                    Bone bn = msh.Bones[i];

                    Console.WriteLine($"Bone Info {bn.VertexWeightCount} {bn.Name}");

                    Vertex.Bone bone = new Vertex.Bone();
                    bone.Name = bn.Name;
                    bone.HasWeights = bn.HasVertexWeights;

                    if (bn.HasVertexWeights)
                    {
                        foreach (VertexWeight w in bn.VertexWeights)
                        {
                            vertices[w.VertexID].pos = Vector3.TransformPosition(vertices[w.VertexID].pos, FromMatrix( bn.OffsetMatrix));
                            vertices[w.VertexID].weights.Add(w.Weight);
                            vertices[w.VertexID].boneNames.Add(bn.Name);
                        }
                    }
                }
            }


            return vertices;
        }
Ejemplo n.º 4
0
        public static void ExportMesh(string FileName, STGenericObject genericMesh)
        {
            string fileNoExt   = Path.GetFileNameWithoutExtension(FileName);
            string fileMtlPath = FileName.Replace("obj", "mtl");

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

            SaveMesh(writer, genericMesh, null, 0);
            File.WriteAllText(FileName, writer.ToString());
        }
Ejemplo n.º 5
0
        public STGenericObject CreateGenericObject(Mesh msh, int Index, Matrix4 transform)
        {
            STGenericObject obj = new STGenericObject();

            Console.WriteLine(msh.MaterialIndex);
            if (msh.MaterialIndex != -1)
            {
                obj.MaterialIndex = msh.MaterialIndex;
            }
            else
            {
                scene.Materials.Add(new Material()
                {
                    Name = msh.Name
                });
            }

            if (scene.Materials[msh.MaterialIndex].Name == "")
            {
                scene.Materials[msh.MaterialIndex].Name = msh.Name;
            }

            obj.HasPos     = msh.HasVertices;
            obj.HasNrm     = msh.HasNormals;
            obj.HasUv0     = msh.HasTextureCoords(0);
            obj.HasUv1     = msh.HasTextureCoords(1);
            obj.HasUv2     = msh.HasTextureCoords(2);
            obj.HasIndices = msh.HasBones;
            if (msh.HasBones)
            {
                obj.HasWeights = msh.Bones[0].HasVertexWeights;
            }

            obj.HasTans               = msh.HasTangentBasis;
            obj.HasBitans             = msh.HasTangentBasis;
            obj.HasVertColors         = msh.HasVertexColors(0);
            obj.ObjectName            = msh.Name;
            obj.boneList              = GetBoneList(msh);
            obj.MaxSkinInfluenceCount = GetVertexSkinCount(msh);

            GenericObject.LOD_Mesh lod = new GenericObject.LOD_Mesh();
            lod.faces         = GetFaces(msh);
            lod.IndexFormat   = STIndexFormat.UInt16;
            lod.PrimitiveType = STPolygonType.Triangle;
            lod.GenerateSubMesh();
            obj.lodMeshes.Add(lod);
            obj.vertices          = GetVertices(msh, transform, obj);
            obj.VertexBufferIndex = Index;

            return(obj);
        }
Ejemplo n.º 6
0
        public void SaveFromObject(STGenericObject genericObject, string FileName)
        {
            Scene scene = new Scene();

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

            var mesh = SaveMesh(genericObject, 0, null, null);

            mesh.MaterialIndex = 0;
            scene.Meshes.Add(mesh);

            Material material = new Material();

            material.Name = "NewMaterial";
            scene.Materials.Add(material);

            SaveScene(FileName, scene, new List <STGenericObject>()
            {
                genericObject
            });
        }
Ejemplo n.º 7
0
        public List <Vertex> GetVertices(Mesh msh, Matrix4 transform, STGenericObject STobj)
        {
            List <Vertex> vertices = new List <Vertex>();

            for (int v = 0; v < msh.VertexCount; v++)
            {
                Vertex vert = new Vertex();

                if (msh.HasVertices)
                {
                    vert.pos = Vector3.TransformPosition(AssimpHelper.FromVector(msh.Vertices[v]), transform);
                }
                if (msh.HasNormals)
                {
                    vert.nrm = Vector3.TransformNormal(AssimpHelper.FromVector(msh.Normals[v]), transform);
                }
                if (msh.HasTextureCoords(0))
                {
                    vert.uv0 = new Vector2(msh.TextureCoordinateChannels[0][v].X, msh.TextureCoordinateChannels[0][v].Y);
                }
                if (msh.HasTextureCoords(1))
                {
                    vert.uv1 = new Vector2(msh.TextureCoordinateChannels[1][v].X, msh.TextureCoordinateChannels[1][v].Y);
                }
                if (msh.HasTextureCoords(2))
                {
                    vert.uv2 = new Vector2(msh.TextureCoordinateChannels[2][v].X, msh.TextureCoordinateChannels[2][v].Y);
                }
                if (msh.HasTangentBasis)
                {
                    vert.tan = new Vector4(msh.Tangents[v].X, msh.Tangents[v].Y, msh.Tangents[v].Z, 1);
                }
                if (msh.HasVertexColors(0))
                {
                    vert.col = new Vector4(msh.VertexColorChannels[0][v].R, msh.VertexColorChannels[0][v].G, msh.VertexColorChannels[0][v].B, msh.VertexColorChannels[0][v].A);
                }
                if (msh.HasTangentBasis)
                {
                    vert.bitan = new Vector4(msh.BiTangents[v].X, msh.BiTangents[v].Y, msh.BiTangents[v].Z, 1);
                }
                vertices.Add(vert);
            }
            if (msh.HasBones)
            {
                for (int i = 0; i < msh.BoneCount; i++)
                {
                    Bone bn = msh.Bones[i];
                    if (bn.HasVertexWeights)
                    {
                        foreach (VertexWeight w in bn.VertexWeights)
                        {
                            //  vertices[w.VertexID].pos = Vector3.TransformPosition(vertices[w.VertexID].pos, AssimpHelper.TKMatrix(bn.OffsetMatrix));
                            vertices[w.VertexID].boneWeights.Add(w.Weight);
                            vertices[w.VertexID].boneNames.Add(bn.Name);
                            BoneNames.Add(bn.Name);
                        }
                    }
                }
            }


            return(vertices);
        }
Ejemplo n.º 8
0
        private Mesh SaveMesh(STGenericObject genericObj, int index, STSkeleton skeleton, List <int> NodeArray)
        {
            //Assimp is weird so use mesh_# for the name. We'll change it back after save
            Mesh mesh = new Mesh($"mesh_{ index }", 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;

                if (skeleton != null)
                {
                    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 = STbone.invert.ToMatrix4x4();
                                mesh.Bones.Add(bone);
                                BoneNames.Add(bone.Name);

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

                            int MinWeightAmount = 0;

                            //Check if the max amount of weights is higher than the current bone id
                            if (v.boneWeights.Count > j && v.boneWeights[j] > MinWeightAmount)
                            {
                                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 if (v.boneWeights.Count == 0 || v.boneWeights[j] > MinWeightAmount)
                            {
                                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);

            return(mesh);
        }
Ejemplo n.º 9
0
        public List <Vertex> GetVertices(Mesh msh, Matrix4 transform, STGenericObject STobj)
        {
            Matrix4 NormalsTransform = Matrix4.CreateFromQuaternion(transform.ExtractRotation());

            List <Vertex> vertices = new List <Vertex>();

            for (int v = 0; v < msh.VertexCount; v++)
            {
                Vertex vert = new Vertex();

                if (msh.HasVertices)
                {
                    vert.pos = Vector3.TransformPosition(AssimpHelper.FromVector(msh.Vertices[v]), transform);
                }
                if (msh.HasNormals)
                {
                    vert.nrm = Vector3.TransformNormal(AssimpHelper.FromVector(msh.Normals[v]), NormalsTransform);
                }
                if (msh.HasTextureCoords(0))
                {
                    vert.uv0 = new Vector2(msh.TextureCoordinateChannels[0][v].X, msh.TextureCoordinateChannels[0][v].Y);
                }
                if (msh.HasTextureCoords(1))
                {
                    vert.uv1 = new Vector2(msh.TextureCoordinateChannels[1][v].X, msh.TextureCoordinateChannels[1][v].Y);
                }
                if (msh.HasTextureCoords(2))
                {
                    vert.uv2 = new Vector2(msh.TextureCoordinateChannels[2][v].X, msh.TextureCoordinateChannels[2][v].Y);
                }
                if (msh.HasTangentBasis)
                {
                    vert.tan = new Vector4(msh.Tangents[v].X, msh.Tangents[v].Y, msh.Tangents[v].Z, 1);
                }
                if (msh.HasVertexColors(0))
                {
                    vert.col = new Vector4(msh.VertexColorChannels[0][v].R, msh.VertexColorChannels[0][v].G, msh.VertexColorChannels[0][v].B, msh.VertexColorChannels[0][v].A);
                    Console.WriteLine("VTX Col " + vert.col);
                }
                if (msh.HasTangentBasis)
                {
                    vert.bitan = new Vector4(msh.BiTangents[v].X, msh.BiTangents[v].Y, msh.BiTangents[v].Z, 1);
                }
                vertices.Add(vert);
            }
            if (msh.HasBones)
            {
                STConsole.WriteLine(msh.Name + " HasBones " + msh.HasBones);
                STConsole.WriteLine(msh.Name + " BoneCount " + msh.BoneCount);

                for (int i = 0; i < msh.BoneCount; i++)
                {
                    Bone bn = msh.Bones[i];
                    if (bn.HasVertexWeights)
                    {
                        foreach (VertexWeight w in bn.VertexWeights)
                        {
                            vertices[w.VertexID].boneWeights.Add(w.Weight);
                            vertices[w.VertexID].boneNames.Add(bn.Name);
                            BoneNames.Add(bn.Name);
                        }
                    }
                }
            }


            return(vertices);
        }