How to increase WPF 3D Performance?

Dragon 81 Reputation points
2020-04-10T04:58:49.733+00:00

I have a problem looking forward to your suggestions about WPF 3D performance. It runs quite well with a small width and length, but with a width and length of about 949x825 When measured by stopwatch, all of the implementation in c# code only takes about 0.6 seconds, but it takes about 6 seconds for it to display 3D images in screen and when using the mouse to zoom or rotate the 3D image it is very slow. Is there any way to increase performance?

Code in C#:

// **Test with width = 949 and length = 825. It's very slow**
        // Make a mesh to hold the surface.
        MeshGeometry3D mesh = new MeshGeometry3D();
        Int32Collection triangleIndices = new Int32Collection();
        Point3DCollection positions = new Point3DCollection();

        //defining point and triangles
        int ind1 = 0;
        int ind2 = 0;

        for (var z = 0; z < length; z++)
        {
            for (var x = 0; x < width; x++)
            {
                var point = new Point3D(x, map[z, x] * ImageConstants.YScale, z);
                positions.Add(point);

                if (z < length - 1 &&
                    x < width - 1)
                {
                    ind1 = x + z * width;
                    ind2 = ind1 + width;

                    //first triangle
                    triangleIndices.Add(ind1);
                    triangleIndices.Add(ind2);
                    triangleIndices.Add(ind2 + 1);

                    //second triangle
                    triangleIndices.Add(ind1);
                    triangleIndices.Add(ind2 + 1);
                    triangleIndices.Add(ind1 + 1);
                }
            }
        }

        mesh.Positions = positions;
        mesh.TriangleIndices = triangleIndices;

In Xaml:

<Grid Name="mainGrid" Background="Black" MouseWheel="OnMouseWheel" MouseDown="OnMouseDown">
        <Viewport3D Name="MainViewport3D">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="MainPerspectiveCamera" FieldOfView="35" />
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <Model3DGroup x:Name="MainModel3DGroup">
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>
    </Grid>

Thank you.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-04-13T07:44:46.987+00:00

    Welcome to our Microsoft Q&A platform!

    You can see the following link about Maximize WPF 3D Performance

    Thanks.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.