Example #1
0
        private void comboBoxCorrectlyLitXX_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxCorrectlyLitXX.SelectedItem == null)
                {
                    return;
                }
                ComboBoxItemXX cbItem = (ComboBoxItemXX)comboBoxCorrectlyLitXX.SelectedItem;

                comboBoxSourceSVIEXunits.Items.Clear();
                String sviex = Path.GetFileNameWithoutExtension(cbItem.xxParser.Name) + "_";
                foreach (IWriteFile file in cbItem.ppForm.Editor.Parser.Subfiles)
                {
                    if (file.Name.StartsWith(sviex, StringComparison.InvariantCultureIgnoreCase) && file.Name.EndsWith(".sviex", StringComparison.InvariantCultureIgnoreCase))
                    {
                        comboBoxSourceSVIEXunits.Items.Add(file);
                    }
                }
                comboBoxCorrectlyLitMeshes.Items.Clear();
                if (comboBoxSourceSVIEXunits.Items.Count > 0)
                {
                    sviexParser srcParser = PluginsPPD.OpenSVIEX((ppParser)Gui.Scripting.Variables[cbItem.ppForm.ParserVar], ((IWriteFile)comboBoxSourceSVIEXunits.Items[0]).Name);
                    Dictionary <string, ComboBoxItemMesh> meshFrameDic = new Dictionary <string, ComboBoxItemMesh>();
                    foreach (sviParser section in srcParser.sections)
                    {
                        ComboBoxItemMesh meshItem;
                        if (!meshFrameDic.TryGetValue(section.meshName, out meshItem))
                        {
                            foreach (xxFrame meshFrame in cbItem.xxForm.Editor.Meshes)
                            {
                                if (meshFrame.Name == section.meshName)
                                {
                                    meshItem = new ComboBoxItemMesh(meshFrame, section.submeshIdx.ToString());
                                    comboBoxCorrectlyLitMeshes.Items.Add(meshItem);
                                    meshFrameDic.Add(section.meshName, meshItem);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            meshItem.submeshes += ", " + section.submeshIdx;
                        }
                    }
                    comboBoxCorrectlyLitMeshes.SelectedIndex = 0;
                    buttonApproximateNormals.Enabled         = comboBoxCorrectlyLitXX.SelectedItem != comboBoxTargetXX.SelectedItem;
                }
                else
                {
                    comboBoxSourceSVIEXunits.Items.Add("No SVIEX present");
                    buttonApproximateNormals.Enabled = false;
                }
                comboBoxSourceSVIEXunits.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                Utility.ReportException(ex);
            }
        }
Example #2
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);
     }
 }
Example #3
0
 public void Dispose()
 {
     SortedParser = null;
 }
Example #4
0
 public sviexEditor(sviParser parser)
 {
     Parser = new sviexParser();
     Parser.Name = parser.Name;
     Parser.sections.Add(parser);
 }
Example #5
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;
        }
Example #6
0
 public sviexEditor(sviexParser parser)
 {
     Parser = parser;
 }
Example #7
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);
     }
 }
Example #8
0
 public void Dispose()
 {
     SortedParser = null;
 }
Example #9
0
 public sviexEditor(sviParser parser)
 {
     Parser      = new sviexParser();
     Parser.Name = parser.Name;
     Parser.sections.Add(parser);
 }
Example #10
0
 public sviexEditor(sviexParser parser)
 {
     Parser = parser;
 }
Example #11
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;
        }
Example #12
0
        private static void SwapNormals(ComboBoxItemXX cbItem, string sviexName)
        {
            Dictionary <xxVertex, Vector3> originalNormals = new Dictionary <xxVertex, Vector3>();
            sviexParser targetParser = PluginsPPD.OpenSVIEX((ppParser)Gui.Scripting.Variables[cbItem.ppForm.ParserVar], sviexName);

            foreach (sviParser section in targetParser.sections)
            {
                bool meshFound = false;
                foreach (ComboBoxItemMesh itemMesh in cbItem.meshes)
                {
                    if (section.meshName == itemMesh.meshFrame.Name)
                    {
                        meshFound = true;
                        xxSubmesh submesh = itemMesh.meshFrame.Mesh.SubmeshList[section.submeshIdx];
                        if (section.indices.Length != submesh.VertexList.Count)
                        {
                            Report.ReportLog("Unmatching SVIEX mesh=" + section.meshName + " submeshIdx=" + section.submeshIdx + " has " + section.indices.Length + " indices.");
                            break;
                        }
                        for (int i = 0; i < section.indices.Length; i++)
                        {
                            ushort   vIdx = section.indices[i];
                            Vector3  norm = section.normals[i];
                            xxVertex vert = submesh.VertexList[vIdx];
                            originalNormals.Add(vert, vert.Normal);
                            vert.Normal = norm;
                        }
                        break;
                    }
                }
                if (!meshFound)
                {
                    Report.ReportLog("SVIEX Normals not copied for " + section.meshName);
                }
            }

            cbItem.xxForm.RecreateRenderObjects();

            foreach (sviParser section in targetParser.sections)
            {
                foreach (ComboBoxItemMesh itemMesh in cbItem.meshes)
                {
                    if (section.meshName == itemMesh.meshFrame.Name)
                    {
                        xxSubmesh submesh = itemMesh.meshFrame.Mesh.SubmeshList[section.submeshIdx];
                        if (section.indices.Length != submesh.VertexList.Count)
                        {
                            break;
                        }
                        for (int i = 0; i < section.indices.Length; i++)
                        {
                            ushort   vIdx = section.indices[i];
                            xxVertex vert = submesh.VertexList[vIdx];
                            Vector3  norm = originalNormals[vert];
                            vert.Normal = norm;
                        }
                        break;
                    }
                }
            }
        }
Example #13
0
        private void buttonApproximateNormals_Click(object sender, EventArgs e)
        {
            try
            {
                ComboBoxItemXX srcItem      = (ComboBoxItemXX)comboBoxCorrectlyLitXX.SelectedItem;
                string         srcMeshes    = String.Empty;
                string         srcSubmeshes = String.Empty;
                foreach (ComboBoxItemMesh meshItem in comboBoxCorrectlyLitMeshes.Items)
                {
                    srcMeshes    += srcItem.xxForm.EditorVar + ".Meshes[" + srcItem.xxForm.Editor.Meshes.IndexOf(meshItem.meshFrame) + "], ";
                    srcSubmeshes += meshItem.submeshes.Split(',').Length + ", " + meshItem.submeshes + ", ";
                }
                srcMeshes    = srcMeshes.Substring(0, srcMeshes.Length - 2);
                srcSubmeshes = srcSubmeshes.Substring(0, srcSubmeshes.Length - 2);

                ComboBoxItemXX dstItem      = (ComboBoxItemXX)comboBoxTargetXX.SelectedItem;
                string         dstMeshes    = String.Empty;
                string         dstSubmeshes = String.Empty;
                foreach (ComboBoxItemMesh meshItem in comboBoxTargetMeshes.Items)
                {
                    dstMeshes += dstItem.xxForm.EditorVar + ".Meshes[" + dstItem.xxForm.Editor.Meshes.IndexOf(meshItem.meshFrame) + "], ";
                    if (meshItem.submeshes.Contains("-1"))
                    {
                        dstSubmeshes += "-1, ";
                    }
                    else
                    {
                        dstSubmeshes += meshItem.submeshes.Split(',').Length + ", " + meshItem.submeshes + ", ";
                    }
                }
                dstMeshes    = dstMeshes.Substring(0, dstMeshes.Length - 2);
                dstSubmeshes = dstSubmeshes.Substring(0, dstSubmeshes.Length - 2);

                string srcParserVar = Gui.Scripting.GetNextVariable("sviexParser");
                string dstParserVar = Gui.Scripting.GetNextVariable("sviexParser");
                string srcEditorVar = Gui.Scripting.GetNextVariable("sviexEditor");
                foreach (IWriteFile srcFile in comboBoxSourceSVIEXunits.Items)
                {
                    comboBoxSourceSVIEXunits.SelectedItem = srcFile;

                    string      parserCommand = srcParserVar + " = OpenSVIEX(parser=" + srcItem.ppForm.ParserVar + ", name=\"" + srcFile.Name + "\")";
                    sviexParser srcParser     = (sviexParser)Gui.Scripting.RunScript(parserCommand);

                    IWriteFile dstFile = (IWriteFile)comboBoxTargetSVIEXunits.Items[comboBoxSourceSVIEXunits.Items.IndexOf(srcFile)];
                    parserCommand = dstParserVar + " = OpenSVIEX(parser=" + dstItem.ppForm.ParserVar + ", name=\"" + dstFile.Name + "\")";
                    Gui.Scripting.RunScript(parserCommand);

                    sviexEditor srcEditor = (sviexEditor)Gui.Scripting.RunScript(srcEditorVar + " = sviexEditor(parser=" + srcParserVar + ")");
                    Gui.Scripting.RunScript(srcEditorVar + ".Reorder()");
                    srcEditor.progressBar = progressBarApproximation;
                    Gui.Scripting.RunScript(srcEditorVar + ".CopyNearestNormals(srcMeshes={ " + srcMeshes + " }, srcSubmeshes={ " + srcSubmeshes + " }, dstMeshes={ " + dstMeshes + " }, dstSubmeshes={ " + dstSubmeshes + " }, dstParser=" + dstParserVar + ", nearVertexThreshold=" + ((float)numericUpDownNearVertexSqDist.Value).ToFloatString() + ", nearestNormal=" + checkBoxNearestNormal.Checked + ", automatic=" + checkBoxAutomatic.Checked + ")");
                    Gui.Scripting.RunScript(dstItem.ppForm.EditorVar + ".ReplaceSubfile(file=" + dstParserVar + ")");

                    comboBoxTargetSVIEXunits.SelectedItem = dstFile;
                }
                dstItem.ppForm.Changed = true;
                Gui.Scripting.Variables.Remove(srcParserVar);
                Gui.Scripting.Variables.Remove(dstParserVar);
                Gui.Scripting.Variables.Remove(srcEditorVar);

                progressBarApproximation.Value   = 0;
                progressBarApproximation.Maximum = 1;
            }
            catch (Exception ex)
            {
                Utility.ReportException(ex);
            }
        }