Exemplo n.º 1
0
        public bool RemoveSVI(xxFrame meshFrame, int submeshIdx)
        {
            sviParser svi = FindSVI(meshFrame.Name, submeshIdx);

            if (svi == null)
            {
                return(false);
            }

            return(Parser.sections.Remove(svi));
        }
Exemplo n.º 2
0
        public bool RemoveSVI(xxFrame meshFrame)
        {
            bool removed = false;

            for (int i = 0; i < Parser.sections.Count; i++)
            {
                sviParser svi = Parser.sections[i];
                if (svi.meshName == meshFrame.Name)
                {
                    Parser.sections.Remove(svi);
                    i--;
                    removed = true;
                }
            }
            return(removed);
        }
Exemplo n.º 3
0
        public sviexParser(Stream stream)
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                version = reader.ReadInt32();
                if (version != 100)
                {
                    throw new Exception("SVIEX bad version: " + version);
                }

                int numSections = reader.ReadInt32();
                sections = new List<sviParser>(numSections);
                for (int secIdx = 0; secIdx < numSections; secIdx++)
                {
                    sviParser section = new sviParser(reader.BaseStream);
                    sections.Add(section);
                }
            }
        }
Exemplo n.º 4
0
        public sviexParser(Stream stream)
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                version = reader.ReadInt32();
                if (version != 100)
                {
                    throw new Exception("SVIEX bad version: " + version);
                }

                int numSections = reader.ReadInt32();
                sections = new List <sviParser>(numSections);
                for (int secIdx = 0; secIdx < numSections; secIdx++)
                {
                    sviParser section = new sviParser(reader.BaseStream);
                    sections.Add(section);
                }
            }
        }
Exemplo n.º 5
0
 public void Reorder()
 {
     SortedParser = new sviexParser();
     foreach (sviParser section in Parser.sections)
     {
         sviParser sortedSection = new sviParser();
         sortedSection.meshName = section.meshName;
         sortedSection.submeshIdx = section.submeshIdx;
         sortedSection.indices = new ushort[section.indices.Length];
         for (ushort i = 0; i < section.indices.Length; i++)
         {
             sortedSection.indices[i] = i;
         }
         if (section.positionsPresent == 1)
         {
             sortedSection.positionsPresent = 1;
             sortedSection.positions = new Vector3[section.indices.Length];
             for (ushort i = 0; i < section.indices.Length; i++)
             {
                 int dstIdx = section.indices[i];
                 sortedSection.positions[dstIdx] = section.positions[i];
             }
         }
         if (section.bonesPresent == 1)
         {
             sortedSection.bonesPresent = 1;
             sortedSection.boneWeights3 = new float[section.boneWeights3.Length][];
             sortedSection.boneIndices = new byte[section.boneIndices.Length][];
             sortedSection.bones = new sviParser.sviBone[section.bones.Length];
             for (ushort i = 0; i < section.indices.Length; i++)
             {
                 int dstIdx = section.indices[i];
                 sortedSection.boneWeights3[dstIdx] = (float[])section.boneWeights3[i].Clone();
                 sortedSection.boneIndices[dstIdx] = (byte[])section.boneIndices[i].Clone();
                 sortedSection.bones[dstIdx] = new sviParser.sviBone();
                 sortedSection.bones[dstIdx].name = (string)section.bones[i].name.Clone();
                 sortedSection.bones[dstIdx].boneIdx = section.bones[i].boneIdx;
                 sortedSection.bones[dstIdx].matrix = section.bones[i].matrix;
             }
         }
         if (section.normalsPresent == 1)
         {
             sortedSection.normalsPresent = 1;
             sortedSection.normals = new Vector3[section.normals.Length];
             for (ushort i = 0; i < section.indices.Length; i++)
             {
                 int dstIdx = section.indices[i];
                 sortedSection.normals[dstIdx] = section.normals[i];
             }
         }
         if (section.uvsPresent == 1)
         {
             sortedSection.uvsPresent = 1;
             sortedSection.uvs = new Vector2[section.indices.Length];
             for (ushort i = 0; i < section.indices.Length; i++)
             {
                 int dstIdx = section.indices[i];
                 sortedSection.uvs[dstIdx] = section.uvs[i];
             }
         }
         SortedParser.sections.Add(sortedSection);
     }
 }
Exemplo n.º 6
0
        public bool AddSVI(xxFrame meshFrame, int submeshIdx, bool positions, bool bones, bool normals, bool uvs)
        {
            sviParser svi = FindSVI(meshFrame.Name, submeshIdx);
            if (svi != null)
            {
                return false;
            }

            svi = new sviParser();
            svi.meshName = meshFrame.Name;
            svi.submeshIdx = submeshIdx;
            svi.indices = new ushort[meshFrame.Mesh.SubmeshList[submeshIdx].VertexList.Count];
            for (ushort i = 0; i < svi.indices.Length; i++)
            {
                svi.indices[i] = i;
            }
            if (positions)
            {
                svi.positionsPresent = 1;
                svi.positions = new Vector3[svi.indices.Length];
                List<xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList;
                for (int i = 0; i < svi.positions.Length; i++)
                {
                    svi.positions[i] = verts[i].Position;
                }
            }
            if (bones)
            {
                svi.bonesPresent = 1;
                svi.boneWeights3 = new float[svi.indices.Length][];
                svi.boneIndices = new byte[svi.indices.Length][];
                List<xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList;
                for (int i = 0; i < svi.boneWeights3.Length; i++)
                {
                    svi.boneWeights3[i] = (float[])verts[i].Weights3.Clone();
                    svi.boneIndices[i] = (byte[])verts[i].BoneIndices.Clone();
                }
                svi.bones = new sviParser.sviBone[meshFrame.Mesh.BoneList.Count];
                for (ushort i = 0; i < svi.bones.Length; i++)
                {
                    sviParser.sviBone bone = new sviParser.sviBone();
                    bone.name = (string)meshFrame.Mesh.BoneList[i].Name.Clone();
                    bone.boneIdx = meshFrame.Mesh.BoneList[i].Index;
                    bone.matrix = meshFrame.Mesh.BoneList[i].Matrix;
                    svi.bones[i] = bone;
                }
            }
            if (normals)
            {
                svi.normalsPresent = 1;
                svi.normals = new Vector3[svi.indices.Length];
                List<xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList;
                for (int i = 0; i < svi.normals.Length; i++)
                {
                    svi.normals[i] = verts[i].Normal;
                }
            }
            if (uvs)
            {
                svi.uvsPresent = 1;
                svi.uvs = new Vector2[svi.indices.Length];
                List<xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList;
                for (int i = 0; i < svi.uvs.Length; i++)
                {
                    svi.uvs[i].X = verts[i].UV[0];
                    svi.uvs[i].Y = verts[i].UV[1];
                }
            }
            Parser.sections.Add(svi);
            return true;
        }
Exemplo n.º 7
0
 public sviexEditor(sviParser parser)
 {
     Parser = new sviexParser();
     Parser.Name = parser.Name;
     Parser.sections.Add(parser);
 }
Exemplo n.º 8
0
        public void CopyNearestNormals(object[] srcMeshes, object[] srcSubmeshes, object[] dstMeshes, object[] dstSubmeshes, sviexParser dstParser, double nearVertexThreshold, bool nearestNormal, bool automatic)
        {
            xxFrame[] srcMeshArr = Utility.Convert<xxFrame>(srcMeshes);
            double[] srcSubmeshDoubleIndices = Utility.Convert<double>(srcSubmeshes);
            HashSet<xxSubmesh> srcSubmeshSet = new HashSet<xxSubmesh>();
            int srcSubmeshIdxIdx = 0;
            int srcTotalSubmeshes = 0;
            foreach (xxFrame meshFrame in srcMeshArr)
            {
                int numSubmeshes = (int)srcSubmeshDoubleIndices[srcSubmeshIdxIdx++];
                srcTotalSubmeshes += numSubmeshes;
                for (int i = 0; i < numSubmeshes; i++)
                {
                    int srcSubmeshIdx = (int)srcSubmeshDoubleIndices[srcSubmeshIdxIdx++];
                    foreach (sviParser section in SortedParser.sections)
                    {
                        if (section.meshName == meshFrame.Name && section.submeshIdx == srcSubmeshIdx)
                        {
                            xxSubmesh submesh = meshFrame.Mesh.SubmeshList[srcSubmeshIdx];
                            srcSubmeshSet.Add(submesh);
                            break;
                        }
                    }
                }
            }
            if (srcTotalSubmeshes != srcSubmeshSet.Count)
            {
                Report.ReportLog("Not all source submeshes exist in " + SortedParser.Name + ". Using only " + srcSubmeshSet.Count + ".");
            }

            xxFrame[] dstMeshArr = Utility.Convert<xxFrame>(dstMeshes);
            double[] dstSubmeshDoubleIndices = Utility.Convert<double>(dstSubmeshes);
            List<xxSubmesh> dstSubmeshList = new List<xxSubmesh>();
            int dstSubmeshIdxIdx = 0;
            foreach (xxFrame meshFrame in dstMeshArr)
            {
                int numSubmeshes = (int)dstSubmeshDoubleIndices[dstSubmeshIdxIdx++];
                if (numSubmeshes == -1)
                {
                    dstSubmeshList.AddRange(meshFrame.Mesh.SubmeshList);
                }
                else
                {
                    for (int i = 0; i < numSubmeshes; i++)
                    {
                        int dstSubmeshIdx = (int)dstSubmeshDoubleIndices[dstSubmeshIdxIdx++];
                        xxSubmesh submesh = meshFrame.Mesh.SubmeshList[dstSubmeshIdx];
                        dstSubmeshList.Add(submesh);
                    }
                }
            }

            sviexParser newParser = new sviexParser();
            foreach (xxFrame dstMeshFrame in dstMeshArr)
            {
                foreach (xxSubmesh dstSubmesh in dstMeshFrame.Mesh.SubmeshList)
                {
                    if (!dstSubmeshList.Contains(dstSubmesh))
                    {
                        continue;
                    }

                    if (progressBar != null)
                    {
                        progressBar.Maximum += dstSubmesh.VertexList.Count;
                    }
                    sviParser newSection = new sviParser();
                    newSection.meshName = dstMeshFrame.Name;
                    newSection.submeshIdx = dstMeshFrame.Mesh.SubmeshList.IndexOf(dstSubmesh);
                    newSection.indices = new ushort[dstSubmesh.VertexList.Count];
                    newSection.normalsPresent = 1;
                    newSection.normals = new Vector3[dstSubmesh.VertexList.Count];
                    for (ushort i = 0; i < dstSubmesh.VertexList.Count; i++)
                    {
                        xxVertex dstVertex = dstSubmesh.VertexList[i];
                        if (automatic)
                        {
                            nearestNormal = false;
                            for (int j = 0; j < dstSubmesh.VertexList.Count; j++)
                            {
                                if (j != i)
                                {
                                    xxVertex vert = dstSubmesh.VertexList[j];
                                    double distSquare = (vert.Position.X - dstVertex.Position.X) * (vert.Position.X - dstVertex.Position.X)
                                        + (vert.Position.Y - dstVertex.Position.Y) * (vert.Position.Y - dstVertex.Position.Y)
                                        + (vert.Position.Z - dstVertex.Position.Z) * (vert.Position.Z - dstVertex.Position.Z);
                                    if (distSquare <= nearVertexThreshold)
                                    {
                                        nearestNormal = true;
                                        break;
                                    }
                                }
                            }
                        }

                        Dictionary<xxFrame, Dictionary<xxSubmesh, List<int>>> bestFindings = new Dictionary<xxFrame, Dictionary<xxSubmesh, List<int>>>();
                        int totalFindings = 0;
                        xxFrame bestMeshFrame = null;
                        xxSubmesh bestSubmesh = null;
                        int bestIdx = -1;
                        double bestDist = double.MaxValue;
                        foreach (xxFrame srcMeshFrame in srcMeshArr)
                        {
                            Dictionary<xxSubmesh, List<int>> bestSubmeshFindings = new Dictionary<xxSubmesh, List<int>>();
                            foreach (xxSubmesh srcSubmesh in srcMeshFrame.Mesh.SubmeshList)
                            {
                                if (!srcSubmeshSet.Contains(srcSubmesh))
                                {
                                    continue;
                                }

                                List<int> bestIndexFindings = new List<int>(srcSubmesh.VertexList.Count);
                                for (int j = 0; j < srcSubmesh.VertexList.Count; j++)
                                {
                                    xxVertex srcVertex = srcSubmesh.VertexList[j];
                                    double distSquare = (srcVertex.Position.X - dstVertex.Position.X) * (srcVertex.Position.X - dstVertex.Position.X)
                                        + (srcVertex.Position.Y - dstVertex.Position.Y) * (srcVertex.Position.Y - dstVertex.Position.Y)
                                        + (srcVertex.Position.Z - dstVertex.Position.Z) * (srcVertex.Position.Z - dstVertex.Position.Z);
                                    if (distSquare <= nearVertexThreshold)
                                    {
                                        bestIndexFindings.Add(j);
                                        totalFindings++;
                                        continue;
                                    }
                                    if (totalFindings == 0 && distSquare < bestDist)
                                    {
                                        bestMeshFrame = srcMeshFrame;
                                        bestSubmesh = srcSubmesh;
                                        bestIdx = j;
                                        bestDist = distSquare;
                                    }
                                }
                                if (bestIndexFindings.Count > 0)
                                {
                                    bestSubmeshFindings.Add(srcSubmesh, bestIndexFindings);
                                }
                            }
                            if (bestSubmeshFindings.Count > 0)
                            {
                                bestFindings.Add(srcMeshFrame, bestSubmeshFindings);
                            }
                        }
                        if (totalFindings > 0)
                        {
                            Vector3 normalSummed = new Vector3();
                            Vector3 normalNearest = new Vector3();
                            double nearestDist = Double.MaxValue;
                            foreach (var finding in bestFindings)
                            {
                                foreach (sviParser srcSection in SortedParser.sections)
                                {
                                    if (srcSection.meshName == finding.Key.Name)
                                    {
                                        foreach (var submeshFinding in finding.Value)
                                        {
                                            if (srcSection.submeshIdx == finding.Key.Mesh.SubmeshList.IndexOf(submeshFinding.Key))
                                            {
                                                foreach (int j in submeshFinding.Value)
                                                {
                                                    if (nearestNormal)
                                                    {
                                                        double distSquare = (srcSection.normals[j].X - dstVertex.Normal.X) * (srcSection.normals[j].X - dstVertex.Normal.X)
                                                            + (srcSection.normals[j].Y - dstVertex.Normal.Y) * (srcSection.normals[j].Y - dstVertex.Normal.Y)
                                                            + (srcSection.normals[j].Z - dstVertex.Normal.Z) * (srcSection.normals[j].Z - dstVertex.Normal.Z);
                                                        if (distSquare < nearestDist)
                                                        {
                                                            normalNearest = srcSection.normals[j];
                                                            nearestDist = distSquare;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        normalSummed += srcSection.normals[j];
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (totalFindings > 1)
                            {
                                normalSummed.Normalize();
                            }

                            newSection.indices[i] = i;
                            newSection.normals[i] = nearestNormal ? normalNearest : normalSummed;
                        }
                        else
                        {
                            int bestSubmeshIdx = bestMeshFrame.Mesh.SubmeshList.IndexOf(bestSubmesh);
                            foreach (sviParser srcSection in SortedParser.sections)
                            {
                                if (srcSection.meshName == bestMeshFrame.Name && srcSection.submeshIdx == bestSubmeshIdx)
                                {
                                    newSection.indices[i] = i;
                                    newSection.normals[i] = srcSection.normals[bestIdx];
                                    break;
                                }
                            }
                        }

                        if (progressBar != null)
                        {
                            progressBar.PerformStep();
                        }
                    }
                    newParser.sections.Add(newSection);
                }
            }

            dstParser.sections = newParser.sections;
        }
Exemplo n.º 9
0
        public bool AddSVI(xxFrame meshFrame, int submeshIdx, bool positions, bool bones, bool normals, bool uvs)
        {
            sviParser svi = FindSVI(meshFrame.Name, submeshIdx);

            if (svi != null)
            {
                return(false);
            }

            svi            = new sviParser();
            svi.meshName   = meshFrame.Name;
            svi.submeshIdx = submeshIdx;
            svi.indices    = new ushort[meshFrame.Mesh.SubmeshList[submeshIdx].VertexList.Count];
            for (ushort i = 0; i < svi.indices.Length; i++)
            {
                svi.indices[i] = i;
            }
            if (positions)
            {
                svi.positionsPresent = 1;
                svi.positions        = new Vector3[svi.indices.Length];
                List <xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList;
                for (int i = 0; i < svi.positions.Length; i++)
                {
                    svi.positions[i] = verts[i].Position;
                }
            }
            if (bones)
            {
                svi.bonesPresent = 1;
                svi.boneWeights3 = new float[svi.indices.Length][];
                svi.boneIndices  = new byte[svi.indices.Length][];
                List <xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList;
                for (int i = 0; i < svi.boneWeights3.Length; i++)
                {
                    svi.boneWeights3[i] = (float[])verts[i].Weights3.Clone();
                    svi.boneIndices[i]  = (byte[])verts[i].BoneIndices.Clone();
                }
                svi.bones = new sviParser.sviBone[meshFrame.Mesh.BoneList.Count];
                for (ushort i = 0; i < svi.bones.Length; i++)
                {
                    sviParser.sviBone bone = new sviParser.sviBone();
                    bone.name    = (string)meshFrame.Mesh.BoneList[i].Name.Clone();
                    bone.boneIdx = meshFrame.Mesh.BoneList[i].Index;
                    bone.matrix  = meshFrame.Mesh.BoneList[i].Matrix;
                    svi.bones[i] = bone;
                }
            }
            if (normals)
            {
                svi.normalsPresent = 1;
                svi.normals        = new Vector3[svi.indices.Length];
                List <xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList;
                for (int i = 0; i < svi.normals.Length; i++)
                {
                    svi.normals[i] = verts[i].Normal;
                }
            }
            if (uvs)
            {
                svi.uvsPresent = 1;
                svi.uvs        = new Vector2[svi.indices.Length];
                List <xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList;
                for (int i = 0; i < svi.uvs.Length; i++)
                {
                    svi.uvs[i].X = verts[i].UV[0];
                    svi.uvs[i].Y = verts[i].UV[1];
                }
            }
            Parser.sections.Add(svi);
            return(true);
        }
Exemplo n.º 10
0
 public void Reorder()
 {
     SortedParser = new sviexParser();
     foreach (sviParser section in Parser.sections)
     {
         sviParser sortedSection = new sviParser();
         sortedSection.meshName   = section.meshName;
         sortedSection.submeshIdx = section.submeshIdx;
         sortedSection.indices    = new ushort[section.indices.Length];
         for (ushort i = 0; i < section.indices.Length; i++)
         {
             sortedSection.indices[i] = i;
         }
         if (section.positionsPresent == 1)
         {
             sortedSection.positionsPresent = 1;
             sortedSection.positions        = new Vector3[section.indices.Length];
             for (ushort i = 0; i < section.indices.Length; i++)
             {
                 int dstIdx = section.indices[i];
                 sortedSection.positions[dstIdx] = section.positions[i];
             }
         }
         if (section.bonesPresent == 1)
         {
             sortedSection.bonesPresent = 1;
             sortedSection.boneWeights3 = new float[section.boneWeights3.Length][];
             sortedSection.boneIndices  = new byte[section.boneIndices.Length][];
             sortedSection.bones        = new sviParser.sviBone[section.bones.Length];
             for (ushort i = 0; i < section.indices.Length; i++)
             {
                 int dstIdx = section.indices[i];
                 sortedSection.boneWeights3[dstIdx]  = (float[])section.boneWeights3[i].Clone();
                 sortedSection.boneIndices[dstIdx]   = (byte[])section.boneIndices[i].Clone();
                 sortedSection.bones[dstIdx]         = new sviParser.sviBone();
                 sortedSection.bones[dstIdx].name    = (string)section.bones[i].name.Clone();
                 sortedSection.bones[dstIdx].boneIdx = section.bones[i].boneIdx;
                 sortedSection.bones[dstIdx].matrix  = section.bones[i].matrix;
             }
         }
         if (section.normalsPresent == 1)
         {
             sortedSection.normalsPresent = 1;
             sortedSection.normals        = new Vector3[section.normals.Length];
             for (ushort i = 0; i < section.indices.Length; i++)
             {
                 int dstIdx = section.indices[i];
                 sortedSection.normals[dstIdx] = section.normals[i];
             }
         }
         if (section.uvsPresent == 1)
         {
             sortedSection.uvsPresent = 1;
             sortedSection.uvs        = new Vector2[section.indices.Length];
             for (ushort i = 0; i < section.indices.Length; i++)
             {
                 int dstIdx = section.indices[i];
                 sortedSection.uvs[dstIdx] = section.uvs[i];
             }
         }
         SortedParser.sections.Add(sortedSection);
     }
 }
Exemplo n.º 11
0
 public sviexEditor(sviParser parser)
 {
     Parser      = new sviexParser();
     Parser.Name = parser.Name;
     Parser.sections.Add(parser);
 }
Exemplo n.º 12
0
        public void CopyNearestNormals(object[] srcMeshes, object[] srcSubmeshes, object[] dstMeshes, object[] dstSubmeshes, sviexParser dstParser, double nearVertexThreshold, bool nearestNormal, bool automatic)
        {
            xxFrame[]           srcMeshArr = Utility.Convert <xxFrame>(srcMeshes);
            double[]            srcSubmeshDoubleIndices = Utility.Convert <double>(srcSubmeshes);
            HashSet <xxSubmesh> srcSubmeshSet           = new HashSet <xxSubmesh>();
            int srcSubmeshIdxIdx  = 0;
            int srcTotalSubmeshes = 0;

            foreach (xxFrame meshFrame in srcMeshArr)
            {
                int numSubmeshes = (int)srcSubmeshDoubleIndices[srcSubmeshIdxIdx++];
                srcTotalSubmeshes += numSubmeshes;
                for (int i = 0; i < numSubmeshes; i++)
                {
                    int srcSubmeshIdx = (int)srcSubmeshDoubleIndices[srcSubmeshIdxIdx++];
                    foreach (sviParser section in SortedParser.sections)
                    {
                        if (section.meshName == meshFrame.Name && section.submeshIdx == srcSubmeshIdx)
                        {
                            xxSubmesh submesh = meshFrame.Mesh.SubmeshList[srcSubmeshIdx];
                            srcSubmeshSet.Add(submesh);
                            break;
                        }
                    }
                }
            }
            if (srcTotalSubmeshes != srcSubmeshSet.Count)
            {
                Report.ReportLog("Not all source submeshes exist in " + SortedParser.Name + ". Using only " + srcSubmeshSet.Count + ".");
            }

            xxFrame[]        dstMeshArr = Utility.Convert <xxFrame>(dstMeshes);
            double[]         dstSubmeshDoubleIndices = Utility.Convert <double>(dstSubmeshes);
            List <xxSubmesh> dstSubmeshList          = new List <xxSubmesh>();
            int dstSubmeshIdxIdx = 0;

            foreach (xxFrame meshFrame in dstMeshArr)
            {
                int numSubmeshes = (int)dstSubmeshDoubleIndices[dstSubmeshIdxIdx++];
                if (numSubmeshes == -1)
                {
                    dstSubmeshList.AddRange(meshFrame.Mesh.SubmeshList);
                }
                else
                {
                    for (int i = 0; i < numSubmeshes; i++)
                    {
                        int       dstSubmeshIdx = (int)dstSubmeshDoubleIndices[dstSubmeshIdxIdx++];
                        xxSubmesh submesh       = meshFrame.Mesh.SubmeshList[dstSubmeshIdx];
                        dstSubmeshList.Add(submesh);
                    }
                }
            }

            sviexParser newParser = new sviexParser();

            foreach (xxFrame dstMeshFrame in dstMeshArr)
            {
                foreach (xxSubmesh dstSubmesh in dstMeshFrame.Mesh.SubmeshList)
                {
                    if (!dstSubmeshList.Contains(dstSubmesh))
                    {
                        continue;
                    }

                    if (progressBar != null)
                    {
                        progressBar.Maximum += dstSubmesh.VertexList.Count;
                    }
                    sviParser newSection = new sviParser();
                    newSection.meshName       = dstMeshFrame.Name;
                    newSection.submeshIdx     = dstMeshFrame.Mesh.SubmeshList.IndexOf(dstSubmesh);
                    newSection.indices        = new ushort[dstSubmesh.VertexList.Count];
                    newSection.normalsPresent = 1;
                    newSection.normals        = new Vector3[dstSubmesh.VertexList.Count];
                    for (ushort i = 0; i < dstSubmesh.VertexList.Count; i++)
                    {
                        xxVertex dstVertex = dstSubmesh.VertexList[i];
                        if (automatic)
                        {
                            nearestNormal = false;
                            for (int j = 0; j < dstSubmesh.VertexList.Count; j++)
                            {
                                if (j != i)
                                {
                                    xxVertex vert       = dstSubmesh.VertexList[j];
                                    double   distSquare = (vert.Position.X - dstVertex.Position.X) * (vert.Position.X - dstVertex.Position.X)
                                                          + (vert.Position.Y - dstVertex.Position.Y) * (vert.Position.Y - dstVertex.Position.Y)
                                                          + (vert.Position.Z - dstVertex.Position.Z) * (vert.Position.Z - dstVertex.Position.Z);
                                    if (distSquare <= nearVertexThreshold)
                                    {
                                        nearestNormal = true;
                                        break;
                                    }
                                }
                            }
                        }

                        Dictionary <xxFrame, Dictionary <xxSubmesh, List <int> > > bestFindings = new Dictionary <xxFrame, Dictionary <xxSubmesh, List <int> > >();
                        int       totalFindings = 0;
                        xxFrame   bestMeshFrame = null;
                        xxSubmesh bestSubmesh   = null;
                        int       bestIdx       = -1;
                        double    bestDist      = double.MaxValue;
                        foreach (xxFrame srcMeshFrame in srcMeshArr)
                        {
                            Dictionary <xxSubmesh, List <int> > bestSubmeshFindings = new Dictionary <xxSubmesh, List <int> >();
                            foreach (xxSubmesh srcSubmesh in srcMeshFrame.Mesh.SubmeshList)
                            {
                                if (!srcSubmeshSet.Contains(srcSubmesh))
                                {
                                    continue;
                                }

                                List <int> bestIndexFindings = new List <int>(srcSubmesh.VertexList.Count);
                                for (int j = 0; j < srcSubmesh.VertexList.Count; j++)
                                {
                                    xxVertex srcVertex  = srcSubmesh.VertexList[j];
                                    double   distSquare = (srcVertex.Position.X - dstVertex.Position.X) * (srcVertex.Position.X - dstVertex.Position.X)
                                                          + (srcVertex.Position.Y - dstVertex.Position.Y) * (srcVertex.Position.Y - dstVertex.Position.Y)
                                                          + (srcVertex.Position.Z - dstVertex.Position.Z) * (srcVertex.Position.Z - dstVertex.Position.Z);
                                    if (distSquare <= nearVertexThreshold)
                                    {
                                        bestIndexFindings.Add(j);
                                        totalFindings++;
                                        continue;
                                    }
                                    if (totalFindings == 0 && distSquare < bestDist)
                                    {
                                        bestMeshFrame = srcMeshFrame;
                                        bestSubmesh   = srcSubmesh;
                                        bestIdx       = j;
                                        bestDist      = distSquare;
                                    }
                                }
                                if (bestIndexFindings.Count > 0)
                                {
                                    bestSubmeshFindings.Add(srcSubmesh, bestIndexFindings);
                                }
                            }
                            if (bestSubmeshFindings.Count > 0)
                            {
                                bestFindings.Add(srcMeshFrame, bestSubmeshFindings);
                            }
                        }
                        if (totalFindings > 0)
                        {
                            Vector3 normalSummed  = new Vector3();
                            Vector3 normalNearest = new Vector3();
                            double  nearestDist   = Double.MaxValue;
                            foreach (var finding in bestFindings)
                            {
                                foreach (sviParser srcSection in SortedParser.sections)
                                {
                                    if (srcSection.meshName == finding.Key.Name)
                                    {
                                        foreach (var submeshFinding in finding.Value)
                                        {
                                            if (srcSection.submeshIdx == finding.Key.Mesh.SubmeshList.IndexOf(submeshFinding.Key))
                                            {
                                                foreach (int j in submeshFinding.Value)
                                                {
                                                    if (nearestNormal)
                                                    {
                                                        double distSquare = (srcSection.normals[j].X - dstVertex.Normal.X) * (srcSection.normals[j].X - dstVertex.Normal.X)
                                                                            + (srcSection.normals[j].Y - dstVertex.Normal.Y) * (srcSection.normals[j].Y - dstVertex.Normal.Y)
                                                                            + (srcSection.normals[j].Z - dstVertex.Normal.Z) * (srcSection.normals[j].Z - dstVertex.Normal.Z);
                                                        if (distSquare < nearestDist)
                                                        {
                                                            normalNearest = srcSection.normals[j];
                                                            nearestDist   = distSquare;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        normalSummed += srcSection.normals[j];
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (totalFindings > 1)
                            {
                                normalSummed.Normalize();
                            }

                            newSection.indices[i] = i;
                            newSection.normals[i] = nearestNormal ? normalNearest : normalSummed;
                        }
                        else
                        {
                            int bestSubmeshIdx = bestMeshFrame.Mesh.SubmeshList.IndexOf(bestSubmesh);
                            foreach (sviParser srcSection in SortedParser.sections)
                            {
                                if (srcSection.meshName == bestMeshFrame.Name && srcSection.submeshIdx == bestSubmeshIdx)
                                {
                                    newSection.indices[i] = i;
                                    newSection.normals[i] = srcSection.normals[bestIdx];
                                    break;
                                }
                            }
                        }

                        if (progressBar != null)
                        {
                            progressBar.PerformStep();
                        }
                    }
                    newParser.sections.Add(newSection);
                }
            }

            dstParser.sections = newParser.sections;
        }
Exemplo n.º 13
0
        private void buttonCopyToSVIEXes_Click(object sender, EventArgs e)
        {
            List <DockContent> formXXList;

            if (!Gui.Docking.DockContents.TryGetValue(typeof(FormXX), out formXXList))
            {
                return;
            }
            List <DockContent> formPPList;

            if (!Gui.Docking.DockContents.TryGetValue(typeof(FormPP), out formPPList))
            {
                return;
            }

            groupBoxAA2SVIEXJuggler.Enabled = false;
            try
            {
                List <string> srcParserVarList = new List <string>();
                List <string> srcEditorVarList = new List <string>();
                foreach (FormPP form in formPPList)
                {
                    foreach (ListViewItem item in form.otherSubfilesList.SelectedItems)
                    {
                        if (item.Text.ToLower().EndsWith(".sviex"))
                        {
                            string srcParserVar = Gui.Scripting.GetNextVariable("sviexParser");
                            srcParserVarList.Add(srcParserVar);
                            string parserCommand = srcParserVar + " = OpenSVIEX(parser=" + form.ParserVar + ", name=\"" + item.Text + "\")";
                            Gui.Scripting.RunScript(parserCommand);
                            string srcEditorVar = Gui.Scripting.GetNextVariable("sviexEditor");
                            srcEditorVarList.Add(srcEditorVar);
                            Gui.Scripting.RunScript(srcEditorVar + " = sviexEditor(parser=" + srcParserVar + ")");
                        }
                        else if (item.Text.ToLower().EndsWith(".svi"))
                        {
                            string srcParserVar = Gui.Scripting.GetNextVariable("sviParser");
                            srcParserVarList.Add(srcParserVar);
                            string parserCommand = srcParserVar + " = OpenSVI(parser=" + form.ParserVar + ", name=\"" + item.Text + "\")";
                            Gui.Scripting.RunScript(parserCommand);
                            string srcEditorVar = Gui.Scripting.GetNextVariable("sviexEditor");
                            srcEditorVarList.Add(srcEditorVar);
                            Gui.Scripting.RunScript(srcEditorVar + " = sviexEditor(parser=" + srcParserVar + ")");
                        }
                    }
                }
                foreach (string srcEditorVar in srcEditorVarList)
                {
                    sviexEditor editor       = (sviexEditor)Gui.Scripting.Variables[srcEditorVar];
                    bool        sviexChanged = false;
                    for (ushort j = 0; j < editor.Parser.sections.Count; j++)
                    {
                        sviParser submeshSection = editor.Parser.sections[j];
                        foreach (FormXX xxForm in formXXList)
                        {
                            foreach (ListViewItem item in xxForm.listViewMesh.SelectedItems)
                            {
                                xxFrame meshFrame = xxForm.Editor.Meshes[(int)item.Tag];
                                if (submeshSection.meshName == meshFrame.Name)
                                {
                                    for (ushort i = 0; i < meshFrame.Mesh.SubmeshList.Count; i++)
                                    {
                                        if (radioButtonSubmeshesAll.Checked ||
                                            radioButtonSubmeshesSelected.Checked && xxForm.textBoxMeshName.Text == meshFrame.Name &&
                                            i < xxForm.dataGridViewMesh.Rows.Count && xxForm.dataGridViewMesh.Rows[i].Selected)
                                        {
                                            if (submeshSection.submeshIdx == i)
                                            {
                                                bool copied = (bool)Gui.Scripting.RunScript(srcEditorVar + ".CopyIntoSVI(meshFrame=" + xxForm.EditorVar + ".Meshes[" + (int)item.Tag + "], submeshIdx=" + i
                                                                                            + ", positions=" + checkBoxElementsPositions.Checked + ", bones=" + checkBoxElementsBonesWeights.Checked + ", normals=" + checkBoxElementsNormals.Checked + ", uvs=" + checkBoxElementsUVs.Checked
                                                                                            + ", unrestricted=" + checkBoxUnrestricted.Checked + ", nearestBones=" + checkBoxNearestBones.Checked + ", nearestNormals=" + checkBoxNearestNormals.Checked + ", nearestUVs=" + checkBoxNearestUVs.Checked + ")");
                                                if (copied)
                                                {
                                                    sviexChanged = true;
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    if (sviexChanged)
                    {
                        foreach (FormPP form in formPPList)
                        {
                            foreach (ListViewItem item in form.otherSubfilesList.SelectedItems)
                            {
                                if (item.Text == editor.Parser.Name)
                                {
                                    int    index        = srcEditorVarList.IndexOf(srcEditorVar);
                                    string srcParserVar = srcParserVarList[index];
                                    Gui.Scripting.RunScript(form.EditorVar + ".ReplaceSubfile(file=" + srcParserVar + ")");

                                    form.Changed = true;
                                    item.Font    = new Font(form.otherSubfilesList.Font, FontStyle.Bold);
                                    break;
                                }
                            }
                        }
                    }
                }

                foreach (string parserVar in srcParserVarList)
                {
                    Gui.Scripting.RunScript(parserVar + "=null");
                }
                foreach (string editorVar in srcEditorVarList)
                {
                    Gui.Scripting.RunScript(editorVar + "=null");
                }
            }
            catch (Exception ex)
            {
                Utility.ReportException(ex);
            }
            groupBoxAA2SVIEXJuggler.Enabled = true;
        }