Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF 3D
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);
            }
          }
      }
 
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);
        }
      }
 
 
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 ?
Posted 1-Oct-12 1:17am
jpg138

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 Smile | :) Especially look at his double buffer, I think thats the right way to go.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 431
1 Afzaal Ahmad Zeeshan 184
2 /\jmot 176
3 Sergey Alexandrovich Kryukov 175
4 Marcin Kozub 175
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100