Exemplo n.º 1
0
        //Current, best version can load FBX, OBJ, DAE etc.
        //Use assimp library
        static void importFBX()
        {
            AssimpContext importer = new AssimpContext();

            // importer.SetConfig(new NormalSmoothingAngleConfig(66.0f));

            //m_model.Meshes[0].Bones[0].VertexWeights[0].
            var    openFileDialog2 = new OpenFileDialog();
            string res             = "";

            if (openFileDialog2.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            res = openFileDialog2.FileName;


            //Prepare bone name convertion table:
            string assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string convertStr   = File.ReadAllText(assemblyPath + "\\boneConvertion.ini");

            //Console.WriteLine("Test reading" + convertStr);
            string[] convertStrlines = convertStr.Split(
                new[] { "\r\n", "\r", "\n" },
                StringSplitOptions.None
                );
            Dictionary <string, string> convertionTable = new Dictionary <string, string>();

            for (int i2 = 0; i2 + 1 < convertStrlines.Length; i2++)
            {
                string target = convertStrlines[i2];
                if (target == null)
                {
                    continue;
                }
                if (target.IndexOf('#') == 0)
                {
                    continue;
                }
                Console.WriteLine(target + "->" + convertStrlines[i2 + 1]);
                convertionTable.Add(target, convertStrlines[i2 + 1]);
                i2++;
            }

            //Table prepartion finished

            Scene md = importer.ImportFile(res, PostProcessSteps.CalculateTangentSpace);// PostProcessPreset.TargetRealTimeMaximumQuality

            MessageBox.Show("Meshes count:" + md.Meshes.Count + "Material count:" + md.MaterialCount + "");

            boneParentList = new Dictionary <String, String>();
            //Build the parent list of bones.

            printNodeStruct(md.RootNode);

            //First, added a custom default layout.
            int layoutCount = targetFlver.BufferLayouts.Count;

            FLVER.BufferLayout newBL = new FLVER.BufferLayout();

            newBL.Add(new FLVER.BufferLayout.Member(0, 0, FLVER.BufferLayout.MemberType.Float3, FLVER.BufferLayout.MemberSemantic.Position, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 12, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Normal, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 16, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Tangent, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 20, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Tangent, 1));

            newBL.Add(new FLVER.BufferLayout.Member(0, 24, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.BoneIndices, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 28, FLVER.BufferLayout.MemberType.Byte4C, FLVER.BufferLayout.MemberSemantic.BoneWeights, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 32, FLVER.BufferLayout.MemberType.Byte4C, FLVER.BufferLayout.MemberSemantic.VertexColor, 1));
            newBL.Add(new FLVER.BufferLayout.Member(0, 36, FLVER.BufferLayout.MemberType.UVPair, FLVER.BufferLayout.MemberSemantic.UV, 0));

            targetFlver.BufferLayouts.Add(newBL);

            int     materialCount       = targetFlver.Materials.Count;
            Boolean flipYZ              = false;
            Boolean setTexture          = false;
            Boolean setAmbientAsDiffuse = false;
            Boolean setLOD              = false;

            var confirmResult = MessageBox.Show("Do you want to switch YZ axis values? \n It may help importing some fbx files.",
                                                "Set",
                                                MessageBoxButtons.YesNo);

            if (confirmResult == DialogResult.Yes)
            {
                flipYZ = true;
            }

            var confirmResult2 = MessageBox.Show("Auto set texture pathes?",
                                                 "Set",
                                                 MessageBoxButtons.YesNo);

            if (confirmResult2 == DialogResult.Yes)
            {
                setTexture = true;


                /* var res3 = MessageBox.Show("Auto ambient texture as diffuse texture?",
                 *                  "Set",
                 *                  MessageBoxButtons.YesNo);
                 *
                 * if (res3 == DialogResult.Yes)
                 * {
                 *   setAmbientAsDiffuse = true;
                 * }*/
            }

            var confirmResult3 = MessageBox.Show("Set LOD level? (Only neccssary if this model need to be viewed far away)",
                                                 "Set",
                                                 MessageBoxButtons.YesNo);

            if (confirmResult3 == DialogResult.Yes)
            {
                setLOD = true;
            }



            foreach (var mat in md.Materials)
            {
                FLVER.Material matnew = new JavaScriptSerializer().Deserialize <FLVER.Material>(new JavaScriptSerializer().Serialize(targetFlver.Materials[0]));
                matnew.Name = res.Substring(res.LastIndexOf('\\') + 1) + "_" + mat.Name;
                // mat.HasTextureDiffuse

                if (setTexture)
                {
                    if (setAmbientAsDiffuse)
                    {
                        if (mat.HasTextureEmissive)
                        {
                            SetFlverMatPath(matnew, "g_DiffuseTexture", FindFileName(mat.TextureEmissive.FilePath) + ".tif");
                        }
                    }
                    else
                    if (mat.HasTextureDiffuse) //g_DiffuseTexture
                    {
                        //  MessageBox.Show("Diffuse mat is" + FindFileName( mat.TextureDiffuse.FilePath));
                        SetFlverMatPath(matnew, "g_DiffuseTexture", FindFileName(mat.TextureDiffuse.FilePath) + ".tif");
                    }
                    if (mat.HasTextureNormal)//g_BumpmapTexture
                    {
                        //MessageBox.Show("Diffuse mat is" + FindFileName(mat.TextureNormal.FilePath));
                        SetFlverMatPath(matnew, "g_BumpmapTexture", FindFileName(mat.TextureNormal.FilePath) + ".tif");
                    }
                    if (mat.HasTextureSpecular)//g_SpecularTexture
                    {
                        /// MessageBox.Show("Specualr mat is" + FindFileName(mat.TextureSpecular.FilePath));
                        SetFlverMatPath(matnew, "g_SpecularTexture", FindFileName(mat.TextureSpecular.FilePath) + ".tif");
                    }
                }
                targetFlver.Materials.Add(matnew);
            }


            //mn.MaterialIndex = materialCount;

            foreach (var m in md.Meshes)
            {
                /* MessageBox.Show("Name:" + m.Name + "\nHas bones:" + m.HasBones + "\nHas normal:" + m.HasNormals + "\nHas tangent" + m.HasTangentBasis +
                 *   "\nVrtices count: " + m.VertexCount
                 *   );*/


                FLVER.Mesh mn = new FLVER.Mesh();
                mn.MaterialIndex = 0;
                mn.BoneIndices   = new List <int>();
                mn.BoneIndices.Add(0);
                mn.BoneIndices.Add(1);
                mn.BoundingBoxMax   = new Vector3(1, 1, 1);
                mn.BoundingBoxMin   = new Vector3(-1, -1, -1);
                mn.BoundingBoxUnk   = new Vector3();
                mn.Unk1             = 0;
                mn.DefaultBoneIndex = 0;
                mn.Dynamic          = true;
                mn.VertexBuffers    = new List <FLVER.VertexBuffer>();
                mn.VertexBuffers.Add(new FLVER.VertexBuffer(0, layoutCount, -1));
                mn.Vertices = new List <FLVER.Vertex>();

                List <List <int> >   verticesBoneIndices = new List <List <int> >();
                List <List <float> > verticesBoneWeights = new List <List <float> >();


                //If it has bones, then record the bone weight info
                if (m.HasBones)
                {
                    for (int i2 = 0; i2 < m.VertexCount; i2++)
                    {
                        verticesBoneIndices.Add(new List <int>());
                        verticesBoneWeights.Add(new List <float>());
                    }

                    for (int i2 = 0; i2 < m.BoneCount; i2++)
                    {
                        string boneName  = m.Bones[i2].Name;
                        int    boneIndex = 0;

                        if (convertionTable.ContainsKey(m.Bones[i2].Name))
                        {
                            boneName = convertionTable[boneName];
                            // m.Bones[i2].Name = convertionTable[m.Bones[i2].Name];
                            boneIndex = findFLVER_Bone(targetFlver, boneName);
                        }
                        else
                        {
                            Console.WriteLine("Cannot find ->" + boneName);
                            //If cannot find a corresponding boneName in convertion.ini then
                            //Try to find org bone's parent, check if it
                            boneIndex = findFLVER_Bone(targetFlver, boneName);


                            //if such bone can not be found in flver, then check its parent to see if it can be convert to its parent bone.
                            //check up to 5th grand parent.
                            for (int bp = 0; bp < boneFindParentTimes; bp++)
                            {
                                if (boneIndex == -1)
                                {
                                    if (boneParentList.ContainsValue(boneName))
                                    {
                                        if (boneParentList[boneName] != null)
                                        {
                                            boneName = boneParentList[boneName];
                                            if (convertionTable.ContainsKey(boneName))
                                            {
                                                boneName = convertionTable[boneName];
                                            }
                                            boneIndex = findFLVER_Bone(targetFlver, boneName);
                                        }
                                    }
                                }
                            }
                        }


                        if (boneIndex == -1)
                        {
                            boneIndex = 0;
                        }
                        for (int i3 = 0; i3 < m.Bones[i2].VertexWeightCount; i3++)
                        {
                            var vw = m.Bones[i2].VertexWeights[i3];

                            verticesBoneIndices[vw.VertexID].Add(boneIndex);
                            verticesBoneWeights[vw.VertexID].Add(vw.Weight);
                        }
                    }
                }

                // m.Bones[0].VertexWeights[0].
                for (int i = 0; i < m.Vertices.Count; i++)
                {
                    var vit = m.Vertices[i];
                    //m.TextureCoordinateChannels[0]
                    var channels = m.TextureCoordinateChannels[0];

                    var uv1 = new Vector3D();
                    var uv2 = new Vector3D();

                    if (channels != null && m.TextureCoordinateChannelCount > 0)
                    {
                        uv1   = getMyV3D(channels[i]);
                        uv1.Y = 1 - uv1.Y;
                        uv2   = getMyV3D(channels[i]);
                        uv2.Y = 1 - uv2.Y;
                        if (m.TextureCoordinateChannelCount > 1)
                        {
                            // uv2 = getMyV3D((m.TextureCoordinateChannels[1])[i]);
                        }
                    }

                    var normal = new Vector3D(0, 1, 0);
                    if (m.HasNormals && m.Normals.Count > i)
                    {
                        normal = getMyV3D(m.Normals[i]).normalize();
                    }



                    //Vector3D tangent = new Vector3D( crossPorduct( getMyV3D(m.Tangents[i]).normalize().toXnaV3() , normal.toXnaV3())).normalize();
                    //var tangent = RotatePoint(normal.toNumV3(), 0, (float)Math.PI / 2, 0);
                    var tangent = new Vector3D(1, 0, 0);
                    if (m.Tangents.Count > i)
                    {
                        tangent = getMyV3D(m.Tangents[i]).normalize();
                    }
                    else
                    {
                        //Calculate tanget instead
                        if (m.HasNormals && m.Normals.Count > i)
                        {
                            tangent = new Vector3D(crossPorduct(getMyV3D(m.Normals[i]).normalize().toXnaV3(), normal.toXnaV3())).normalize();
                        }
                    }

                    FLVER.Vertex v = generateVertex(new Vector3(vit.X, vit.Y, vit.Z), uv1.toNumV3(), uv2.toNumV3(), normal.toNumV3(), tangent.toNumV3(), 1);

                    if (flipYZ)
                    {
                        v = generateVertex(new Vector3(vit.X, vit.Z, vit.Y), uv1.toNumV3(), uv2.toNumV3(),
                                           new Vector3(normal.X, normal.Z, normal.Y), new Vector3(tangent.X, tangent.Z, tangent.Y), 1);
                    }


                    if (m.HasBones)
                    {
                        for (int j = 0; j < verticesBoneIndices[i].Count && j < 4; j++)
                        {
                            v.BoneIndices[j] = (verticesBoneIndices[i])[j];
                            v.BoneWeights[j] = (verticesBoneWeights[i])[j];
                        }
                    }
                    mn.Vertices.Add(v);
                }



                List <uint> faceIndexs = new List <uint>();
                for (int i = 0; i < m.FaceCount; i++)
                {
                    if (flipYZ)
                    {
                        if (m.Faces[i].Indices.Count == 3)
                        {
                            faceIndexs.Add((uint)m.Faces[i].Indices[0]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[1]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[2]);
                        }
                        else if (m.Faces[i].Indices.Count == 4)
                        {
                            faceIndexs.Add((uint)m.Faces[i].Indices[0]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[1]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[2]);

                            faceIndexs.Add((uint)m.Faces[i].Indices[2]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[3]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[0]);
                        }
                    }
                    else
                    {
                        if (m.Faces[i].Indices.Count == 3)
                        {
                            faceIndexs.Add((uint)m.Faces[i].Indices[0]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[2]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[1]);
                        }
                        else if (m.Faces[i].Indices.Count == 4)
                        {
                            faceIndexs.Add((uint)m.Faces[i].Indices[0]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[2]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[1]);

                            faceIndexs.Add((uint)m.Faces[i].Indices[2]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[0]);
                            faceIndexs.Add((uint)m.Faces[i].Indices[3]);
                        }
                    }
                }
                //
                mn.FaceSets = new List <FLVER.FaceSet>();
                //FLVER.Vertex myv = new FLVER.Vertex();
                //myv.Colors = new List<FLVER.Vertex.Color>();

                mn.FaceSets.Add(generateBasicFaceSet());
                mn.FaceSets[0].Vertices = faceIndexs.ToArray();
                if (mn.FaceSets[0].Vertices.Length > 65534)
                {
                    MessageBox.Show("There are more than 65535 vertices in a mesh , switch to 32 bits index size mode.");
                    mn.FaceSets[0].IndexSize = 32;
                }


                if (setLOD == true)
                {
                    //Special thanks to Meowmaritus
                    {
                        FLVER.FaceSet fs = generateBasicFaceSet();
                        fs.Flags     = SoulsFormats.FLVER.FaceSet.FSFlags.LodLevel1;
                        fs.IndexSize = mn.FaceSets[0].IndexSize;
                        fs.Vertices  = (uint[])(mn.FaceSets[0].Vertices.Clone());
                        mn.FaceSets.Add(fs);
                    }

                    {
                        FLVER.FaceSet fs = generateBasicFaceSet();
                        fs.Flags     = SoulsFormats.FLVER.FaceSet.FSFlags.LodLevel2;
                        fs.IndexSize = mn.FaceSets[0].IndexSize;
                        fs.Vertices  = (uint[])(mn.FaceSets[0].Vertices.Clone());
                        mn.FaceSets.Add(fs);
                    }
                    //unk8000000000 is the motion blur
                    {
                        FLVER.FaceSet fs = generateBasicFaceSet();
                        fs.Flags     = SoulsFormats.FLVER.FaceSet.FSFlags.Unk80000000;
                        fs.IndexSize = mn.FaceSets[0].IndexSize;
                        fs.Vertices  = (uint[])(mn.FaceSets[0].Vertices.Clone());
                        mn.FaceSets.Add(fs);
                    }

                    {
                        FLVER.FaceSet fs = generateBasicFaceSet();
                        fs.Flags     = SoulsFormats.FLVER.FaceSet.FSFlags.LodLevel1 | SoulsFormats.FLVER.FaceSet.FSFlags.Unk80000000;
                        fs.IndexSize = mn.FaceSets[0].IndexSize;
                        fs.Vertices  = (uint[])(mn.FaceSets[0].Vertices.Clone());
                        mn.FaceSets.Add(fs);
                    }

                    {
                        FLVER.FaceSet fs = generateBasicFaceSet();
                        fs.Flags     = SoulsFormats.FLVER.FaceSet.FSFlags.LodLevel2 | SoulsFormats.FLVER.FaceSet.FSFlags.Unk80000000;
                        fs.IndexSize = mn.FaceSets[0].IndexSize;
                        fs.Vertices  = (uint[])(mn.FaceSets[0].Vertices.Clone());
                        mn.FaceSets.Add(fs);
                    }
                }


                mn.MaterialIndex = materialCount + m.MaterialIndex;


                targetFlver.Meshes.Add(mn);
            }

            MessageBox.Show("Added a custom mesh! PLease click modify to save it!");
            updateVertices();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deprecated, cannot solve tangent properly.
        /// </summary>
        static void importObj()
        {
            var    openFileDialog2 = new OpenFileDialog();
            string res             = "";

            if (openFileDialog2.ShowDialog() == DialogResult.No)
            {
                return;
            }
            res = openFileDialog2.FileName;
            var objLoaderFactory       = new ObjLoaderFactory();
            MaterialStreamProvider msp = new MaterialStreamProvider();
            var openFileDialog3        = new OpenFileDialog();

            openFileDialog3.Title = "Choose MTL file:";
            if (openFileDialog3.ShowDialog() == DialogResult.No)
            {
                return;
            }

            msp.Open(openFileDialog3.FileName);
            var        objLoader  = objLoaderFactory.Create(msp);
            FileStream fileStream = new FileStream(res, FileMode.Open);
            LoadResult result     = objLoader.Load(fileStream);



            // ObjLoader.Loader.Data.Elements.Face f = result.Groups[0].Faces[0];
            // ObjLoader.Loader.Data.Elements.FaceVertex[] fv =getVertices(f);

            // string groups = new JavaScriptSerializer().Serialize(fv);
            //string vertices = new JavaScriptSerializer().Serialize(result.Vertices);

            //MessageBox.Show(groups,"Group info");
            // MessageBox.Show(vertices, "V info");
            fileStream.Close();

            //Step 1 add a new buffer layout for my program:
            int layoutCount = targetFlver.BufferLayouts.Count;

            FLVER.BufferLayout newBL = new FLVER.BufferLayout();

            newBL.Add(new FLVER.BufferLayout.Member(0, 0, FLVER.BufferLayout.MemberType.Float3, FLVER.BufferLayout.MemberSemantic.Position, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 12, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Normal, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 16, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Tangent, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 20, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Tangent, 1));

            newBL.Add(new FLVER.BufferLayout.Member(0, 24, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.BoneIndices, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 28, FLVER.BufferLayout.MemberType.Byte4C, FLVER.BufferLayout.MemberSemantic.BoneWeights, 0));
            newBL.Add(new FLVER.BufferLayout.Member(0, 32, FLVER.BufferLayout.MemberType.Byte4C, FLVER.BufferLayout.MemberSemantic.VertexColor, 1));
            newBL.Add(new FLVER.BufferLayout.Member(0, 36, FLVER.BufferLayout.MemberType.UVPair, FLVER.BufferLayout.MemberSemantic.UV, 0));

            targetFlver.BufferLayouts.Add(newBL);

            int materialCount = targetFlver.Materials.Count;



            FLVER.Mesh mn = new FLVER.Mesh();
            mn.MaterialIndex = 0;
            mn.BoneIndices   = new List <int>();
            mn.BoneIndices.Add(0);
            mn.BoneIndices.Add(1);
            mn.BoundingBoxMax   = new Vector3(1, 1, 1);
            mn.BoundingBoxMin   = new Vector3(-1, -1, -1);
            mn.BoundingBoxUnk   = new Vector3();
            mn.Unk1             = 0;
            mn.DefaultBoneIndex = 0;
            mn.Dynamic          = false;
            mn.VertexBuffers    = new List <FLVER.VertexBuffer>();
            mn.VertexBuffers.Add(new FLVER.VertexBuffer(0, layoutCount, -1));
            mn.Vertices = new List <FLVER.Vertex>();
            // mn.Vertices.Add(generateVertex(new Vector3(1,0,0),new Vector3(0,0,0),new Vector3(0,0,0),new Vector3(0,1,0),new Vector3(1,0,0)));
            //mn.Vertices.Add(generateVertex(new Vector3(0, 1, 0), new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 0, 0)));
            //mn.Vertices.Add(generateVertex(new Vector3(0, 0, 1), new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 0, 0)));
            if (result.Groups.Count == 0)
            {
                MessageBox.Show("You imported nothing!");
                return;
            }
            MessageBox.Show("Vertice number:" + result.Vertices.Count + "Texture V number:" + result.Textures.Count + "Normal number:" + result.Normals.Count + "Face groups:" + result.Groups[0].Faces.Count);

            VertexNormalList[] vnlist = new VertexNormalList[result.Vertices.Count + 1];
            for (int i = 0; i < vnlist.Length; i++)
            {
                vnlist[i] = new VertexNormalList();
            }

            List <uint> faceIndexs = new List <uint>();

            uint[] textureIndexs = new uint[result.Vertices.Count + 1];
            foreach (var gr in result.Groups)
            {
                foreach (var faces in gr.Faces)
                {
                    var vList = getVertices(faces);

                    /*for (int i3 = 0; i3 < vList.Length - 2; i3++)
                     * {
                     *  faceIndexs.Add((uint)(vList[i3].VertexIndex)-1);
                     *  faceIndexs.Add((uint)(vList[i3+1].VertexIndex)-1);
                     *  faceIndexs.Add((uint)(vList[i3+2].VertexIndex)-1);
                     * }*/
                    if (vList.Length == 4)
                    {
                        faceIndexs.Add((uint)(vList[0].VertexIndex) - 1);
                        faceIndexs.Add((uint)(vList[2].VertexIndex) - 1);
                        faceIndexs.Add((uint)(vList[1].VertexIndex) - 1);

                        //record normal to help calculate vertex normals
                        int helperI = 0;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);

                        helperI = 2;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);

                        helperI = 1;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);


                        faceIndexs.Add((uint)(vList[2].VertexIndex) - 1);
                        faceIndexs.Add((uint)(vList[0].VertexIndex) - 1);
                        faceIndexs.Add((uint)(vList[3].VertexIndex) - 1);

                        helperI = 2;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);

                        helperI = 0;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex].Y, result.Normals[vList[helperI].NormalIndex].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);

                        helperI = 3;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex].X, result.Normals[vList[helperI].NormalIndex].Y, result.Normals[vList[helperI].NormalIndex].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);
                    }
                    else if (vList.Length == 3)
                    {
                        faceIndexs.Add((uint)(vList[0].VertexIndex) - 1);
                        faceIndexs.Add((uint)(vList[2].VertexIndex) - 1);
                        faceIndexs.Add((uint)(vList[1].VertexIndex) - 1);


                        int helperI = 2;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);

                        helperI = 0;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);

                        helperI = 1;
                        vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z));
                        textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1);
                    }
                }
            }
            //mn.FaceSets[0].Vertices = new uint [3]{0,1,2 };


            mn.FaceSets = new List <FLVER.FaceSet>();
            //FLVER.Vertex myv = new FLVER.Vertex();
            //myv.Colors = new List<FLVER.Vertex.Color>();
            mn.FaceSets.Add(generateBasicFaceSet());
            mn.FaceSets[0].Vertices = faceIndexs.ToArray();



            //Set all the vertices.
            for (int iv = 0; iv < result.Vertices.Count; iv++)
            {
                var v = result.Vertices[iv];

                Vector3 uv1     = new Vector3();
                Vector3 uv2     = new Vector3();
                Vector3 normal  = new Vector3(0, 1, 0);
                Vector3 tangent = new Vector3(1, 0, 0);
                if (result.Textures != null)
                {
                    if (iv < result.Textures.Count)
                    {
                        var vm = result.Textures[(int)textureIndexs[iv]];
                        uv1 = new Vector3(vm.X, vm.Y, 0);
                        uv2 = new Vector3(vm.X, vm.Y, 0);
                    }
                }
                normal  = vnlist[iv].calculateAvgNormal().toNumV3();
                tangent = RotatePoint(normal, 0, (float)Math.PI / 2, 0);
                mn.Vertices.Add(generateVertex(new Vector3(v.X, v.Y, v.Z), uv1, uv2, normal, tangent));
            }
            FLVER.Material matnew = new JavaScriptSerializer().Deserialize <FLVER.Material>(new JavaScriptSerializer().Serialize(targetFlver.Materials[0]));
            matnew.Name = res.Substring(res.LastIndexOf('\\') + 1);
            targetFlver.Materials.Add(matnew);
            mn.MaterialIndex = materialCount;


            targetFlver.Meshes.Add(mn);
            MessageBox.Show("Added a custom mesh! PLease click modify to save it!");
            updateVertices();
            //mn.Vertices.Add();
        }