簡體   English   中英

WPF 3D性能

[英]WPF 3D performance

我想繪制一個點雲,搜索互聯網顯示3D中沒有點,我需要繪制立方體。

所以我試着添加到viewPort3D 50,000個立方體,它只花了很多時間(約5分鍾)

我能在這段代碼上做些什么來獲得更好的性能嗎?

這是我的代碼:

 public partial class MainWindow : Window
{
    private Int32Collection _indices;
    DiffuseMaterial _diffuseMaterial = new DiffuseMaterial(Brushes.Gray);

    public MainWindow()
    {
        Int32CollectionConverter icc = new Int32CollectionConverter();
        _indices = (Int32Collection)icc.ConvertFromString(@"
                                  3,2,4 2,7,4 
                                  2,1,7 7,1,6
                                  4,7,5 5,7,6
                                  0,3,4 0,4,5
                                  1,0,5 1,5,6
                                  1,2,3 1,3,0      
                                 ");


        InitializeComponent();

        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();

        for (int i = 0; i < 50000; i++)
        {
            AddFeature(1, i, 1);
        }

        stopwatch.Stop();
        Console.WriteLine(stopwatch.Elapsed.TotalSeconds + "sec");
    }

    private void AddFeature(double x, double y, double z, double size = 1)
    {
        GeometryModel3D geometryModel3D = new GeometryModel3D();
        MeshGeometry3D model = new MeshGeometry3D();

        model.Positions.Add(new Point3D(-size + x, -size + y, -size + z));
        model.Positions.Add(new Point3D(size + x, -size + y, -size + z));
        model.Positions.Add(new Point3D(size + x, -size + y, size + z));
        model.Positions.Add(new Point3D(-size + x, -size + y, size + z));
        model.Positions.Add(new Point3D(-size + x, size + y, size + z));
        model.Positions.Add(new Point3D(-size + x, size + y, -size + z));
        model.Positions.Add(new Point3D(size + x, size + y, -size + z));
        model.Positions.Add(new Point3D(size + x, size + y, size + z));

        model.TriangleIndices = _indices;
        geometryModel3D.Material = _diffuseMaterial;
        geometryModel3D.Geometry = model;
        model3DGroup.Children.Add(geometryModel3D);
    }

}

XAML:

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Viewport3D x:Name="Viewport3D" Margin="4,4,4,4" Grid.Row="0" Grid.Column="0" ClipToBounds="False" IsHitTestVisible="False">
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup x:Name="model3DGroup">
                    <!-- Lights -->
                    <AmbientLight Color="Gray"/>
                    <DirectionalLight Color="Gray" Direction="1,-2,-3" />
                    <DirectionalLight Color="Gray" Direction="-1,2,3" />
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>

        <Viewport3D.Camera>
            <PerspectiveCamera 
              Position = "2, 4, 6"
              LookDirection = "-1, -2, -3"
              UpDirection = "0, 1, 0"
              FieldOfView = "60">
                <PerspectiveCamera.Transform>
                    <Transform3DGroup>
                        <ScaleTransform3D x:Name="scaleTransform3D" CenterX="0" CenterY="0" CenterZ="0" />
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D x:Name="axisAngleRotate1"
                                  Axis="0 1 0" 
                                   />
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D x:Name="axisAngleRotate2"
                                  Axis="1 0 0" 
                                   />
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </Transform3DGroup>
                </PerspectiveCamera.Transform>
            </PerspectiveCamera>
        </Viewport3D.Camera>
    </Viewport3D>


</Grid>

您可以對代碼進行兩項更改,以顯着提高性能:

  1. 通過調用GeometryModel3D.Freeze()方法將每個GeometryModel3D添加到組之前凍結它們。 您可能還會通過調用MeshGeometry3DMeshGeometry3D.Positions對象上的凍結來看到一個小的改進。

  2. 將所有GeometryModel3D對象添加到臨時Model3DCollection實例,並在for循環結束時將實例分配給Model3DGroup.Children

您需要將這些模型添加到組中,它會大幅提高性能。

你可以在這里找到完整的例子。 http://msdn.microsoft.com/en-US/System.Windows.Media.Media3D.Model3DGroup.aspx

與OpenGL和DirectX相比,WPF 3D在速度上有很大的局限性,您可以輕松達到極限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM