インターネットを検索して、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>