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