wpf 3d,一个Model3DGroup的多个实例,试图优化性能 [英] wpf 3d, multiple instances of a Model3DGroup, trying to optimize performance

查看:102
本文介绍了wpf 3d,一个Model3DGroup的多个实例,试图优化性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我的程序显示了一个静态场景,其中包含3d模型(例如树)在不同位置的多个实例.此模型可能很繁重",在50000到100000个三角形之间,并且性能随着实例数的增加而迅速下降(超过10个实例是不可接受的).

我的第一个未经优化的代码如下所示:

hello,

My program shows a static scene containing several instances, at different places, of a 3d model, for example a tree. This model can be ''heavy'', between 50000 and 100000 triangles, and the performance degrade quickly with the number of instances (performance is unacceptable with more than 10 instances).

My first code, without optimizations, looks like this :

public partial class Vue3D : Window
{
  public ModelVisual3D modelVisual = new ModelVisual3D();
  public Model3DGroup model3DGroup = new Model3DGroup();

  public Vue3D()
    {
      ...
      modelVisual.Content = model3DGroup;
      view1.Children.Add(modelVisual); // view1 = viewport3D
      ...
      BuildScene();
    }

  void BuildScene()
    {
      Model3DGroup model = GetModel("tree");
      foreach (Point3D pnt in <point collection>)
      {
        Model3DGroup modelClone = model.Clone();
        Transform3DGroup tg = new Transform3DGroup();
        tg.Children.Add(new TranslateTransform3D(pnt.X, pnt.Y, pnt.Z>));
        modelClone.Transform = tg;
        model3DGroup.Children.Add(modelClone);
      }
    }
}



对于每个实例,我克隆完整的模型,然后在转换后将克隆添加到视口中.
假设树模型包含2 GeometryModel3D:
{mesh1(20000个三角形),texture1}和{mesh2(40000个三角形),texture2}.
如果我显示10棵树,则我将添加20个GeometryModel3D.

由于所有这些实例共享相同的2个纹理,因此我认为我可以通过仅创建2个GeometryModel3D来加快处理速度:一个与所有与Texture1关联的三角形,一个与与Texture2关联的所有三角形.这就是我在这段代码中尝试过的内容:



For each instance i clone the complete model, and add the clone to the viewport after transformation.
Let''s say the tree model contains 2 GeometryModel3D :
{mesh1 (20000 triangles), texture1} and {mesh2 (40000 triangles), texture2}.
If i show 10 trees, i''m adding 20 GeometryModel3D.

Since all these instances share the same 2 textures, i thought i could speed up things by creating only 2 GeometryModel3D : one with all the triangles associated with texture1, one with all the triangles associated with texture2. That''s what i tried in this code :

void BuildScene()
     {
       List<Transform3DGroup>> listTg = new List<Transform3DGroup>();
       foreach (Point3D pnt in <point collection>)
       {
         Transform3DGroup tg = new Transform3DGroup();
         tg.Children.Add(new TranslateTransform3D(pnt.X, pnt.Y, pnt.Z>));
         listTg.Add(tg);
       }
       Model3DGroup model = GetModel("tree");
       foreach (GeometryModel3D gmodel in model.Children)
       {
         MeshGeometry3D mesh = gmodel.Geometry as MeshGeometry3D;
         GeometryModel3D newgmodel = new GeometryModel3D();
         newgmodel.Material = gmodel.Material;
         newgmodel.BackMaterial = gmodel.BackMaterial;
         MeshGeometry3D newmesh = new MeshGeometry3D();
         newgmodel.Geometry = newmesh;
         foreach (Transform3DGroup tg in listTg)
         {
           foreach (int indice in mesh.TriangleIndices)
             newmesh.TriangleIndices.Add(indice + newmesh.Positions.Count);
           foreach (Point3D p3 in mesh.Positions)
             newmesh.Positions.Add(tg.Transform(p3));
           if (mesh.TextureCoordinates != null)
           {
             foreach (System.Windows.Point p in mesh.TextureCoordinates)
               newmesh.TextureCoordinates.Add(p);
           }
         }
         newmesh.TriangleIndices.Freeze();
         newmesh.TextureCoordinates.Freeze();
         newmesh.Positions.Freeze();
         newgmodel.Material.Freeze();
         newgmodel.BackMaterial.Freeze();
         newgmodel.Freeze();
         model3DGroup.Children.Add(newgmodel);
       }
     }



而且性能不是更好,实际上是更差...我在这里想念的是什么?看来我的假设(即最小化GeometryModel3D的数字将是有益的)是错误的.我在某处已经读到,在wpf 3d中,三角形的数量对性能没有太大影响,但是纹理的数量更为关键.在这里,我有大量三角形,但只有2个纹理,只有一个ModelVisual3D,并且全部冻结了...任何建议?



And the performance is not better, in fact it''s rather worse ... What am I missing here ? It seems that my assumption, i.e. minimizing the number GeometryModel3D would be beneficial, is false. I have read somewhere that in wpf 3d, the number of triangles has no big impact on performance, but that the number of textures was more critical. Here i have a big number of triangles but only 2 textures, only one ModelVisual3D, and all is frozen... Any suggestion ?

推荐答案

3D使我的大脑受伤.但是这里有一个3D WPF实时动画的很好的例子:
http://stuff.seans.com/2008/08/24/raindrop-animation -in-wpf/ [ ^ ]
也许您可以在那里找到一些技巧:)特别是看看他的双重缓冲,我认为这是正确的方法.
3D makes my brain hurt. But there is a really good example of a 3D WPF real time animation here:
http://stuff.seans.com/2008/08/24/raindrop-animation-in-wpf/[^]
Perhaps you can get some tips there :) Especially look at his double buffer, I think thats the right way to go.


这篇关于wpf 3d,一个Model3DGroup的多个实例,试图优化性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆