1

我想画点云,在网上搜索显示 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>

4

3 回答 3

4

您可以对代码进行两项更改以显着提高性能:

  1. 在通过调用该方法GeometryModel3D将其添加到组之前冻结每个。通过对and 对象GeometryModel3D.Freeze()调用 freeze,您可能还会看到一个小的改进。MeshGeometry3DMeshGeometry3D.Positions

  2. 将所有GeometryModel3D对象添加到一个临时Model3DCollection 实例,并Model3DGroup.Children在 for 循环结束时将实例分配给。

于 2012-09-14T20:05:39.017 回答
0

您需要将这些模型添加到一个组中,这会大大提高性能。

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

于 2012-06-19T12:12:12.100 回答
0

与 OpenGL 和 DirectX 相比,WPF 3D 在速度上有很大的限制,您可以轻松达到极限。

于 2012-06-19T13:04:35.533 回答