WPF HelixTools 来自 Obj 文件的网格

问题描述 投票:0回答:1

我对 WPF 和 3D 环境相当陌生。我制作了一个应用程序,在其中导入了一个相当大的 3D 模型 .obj 文件。渲染它需要 1 到 2 分钟,这意味着模型本身有大量的面/三角形,因为它是实体模型。有没有一种方法可以导入 obj 文件并将其转换/渲染为曲面模型?

提前致谢!

这是我的代码:

       ModelImporter importer = new ModelImporter();
       System.Windows.Media.Media3D.Material material = new DiffuseMaterial(new SolidColorBrush(Colors.Beige));
       importer.DefaultMaterial = material;


       mymodels = new Model3DGroup();

       Part1 = importer.Load("3dmodels\\*****.obj");
       mymodels.Children.Add(Part1);


       this.Our_Model = mymodels;
c# wpf 3d helix-3d-toolkit
1个回答
0
投票

obj 文件是曲面模型。意思是由三角形/四边形/等组成...我认为这种格式中没有四边形/六边形/等。 不管怎样,如果你有体积物体并且想要“仅”将表面渲染为三角形/四边形......你需要做一些工作,以下是我对四边形的做法:

  • 首先分解你的四面体:

      private void DecomposeTetra(ModelMesh mesh, uint[] indices)
      {
          AddFaceAdjacency(new Triangle(indices[0], indices[2], indices[1]));
          AddFaceAdjacency(new Triangle(indices[0], indices[3], indices[2]));
          AddFaceAdjacency(new Triangle(indices[0], indices[1], indices[3]));
          AddFaceAdjacency(new Triangle(indices[1], indices[2], indices[3]));
      }
    

然后将人脸邻接注册到字典中

private void AddFaceAdjacency(Triangle t)
    {
        var combi = t.GetKeyCombinations();
        if (facesAdjacency.ContainsAny(combi, out var contained))
            facesAdjacency[contained] = null;
        else
            facesAdjacency.Add(combi[0], t);
    }

其中 GetKeyCombinations() 返回索引的排列。如果一个面已经注册,则使该面无效(意味着该面有邻接 -> 不在表面上)

最后创建你的网格:

        private void CreateVisibleMesh(Mesh mesh)
    {
        float count = facesAdjacency.Count;
        foreach (var pair in facesAdjacency)
        {
            if (pair.Value == null)
                continue;
            mesh.Faces.Add(pair.Value);
        }
        facesAdjacency.Clear();
        facesAdjacency = null;
    }
  • 您可能需要调整“CreateVisibleMesh”以满足Helix要求(正确添加顶点和索引)。

  • 这不一定是最有效的方法(排列和其他东西),但它可以完成工作。 最后只是为了帮助你:

          private static string[] GetKeyCombinations(this Triangle face)
          => new string[]
          {
               $"{face.VertexIndexes[0]}|{face.VertexIndexes[1]}|{face.VertexIndexes[2]}",
               $"{face.VertexIndexes[1]}|{face.VertexIndexes[2]}|{face.VertexIndexes[0]}",
               $"{face.VertexIndexes[2]}|{face.VertexIndexes[0]}|{face.VertexIndexes[1]}",
    
               $"{face.VertexIndexes[2]}|{face.VertexIndexes[1]}|{face.VertexIndexes[0]}",
               $"{face.VertexIndexes[0]}|{face.VertexIndexes[2]}|{face.VertexIndexes[1]}",
               $"{face.VertexIndexes[1]}|{face.VertexIndexes[0]}|{face.VertexIndexes[2]}",
          };
    
© www.soinside.com 2019 - 2024. All rights reserved.