Beschleunigen von 3d Simulationen

  • WPF

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von matzemaschine.

    Beschleunigen von 3d Simulationen

    Hallo alle zusammen,
    ich habe gerade angefangen Simulationen zu programmieren. Aufgabe war es eine Datei mit Messwerten einzulesen und über Funktionen zu einen Körper zusammenzustellen. Jeder Messpunkt wir auf 2 Dreiecken hochgerechnet (ca. 37062 Dreiecke), was auch wunderbar funktioniert.
    Mein Problem ist nachdem ich den Körper erstellt und angezeigt habe, ist die Simulation extrem langsam und mein Rechner langweilt sich auch noch.

    Zu dem Code
    Erstellung des Körpers in Vb.net

    VB.NET-Quellcode

    1. Sub PaintCube()
    2. Dim geometryFront As New GeometryModel3D
    3. Dim geometryBack As New GeometryModel3D
    4. Dim geometryRight As New GeometryModel3D
    5. Dim geometryLeft As New GeometryModel3D
    6. Dim geometryTop As New GeometryModel3D
    7. Dim geometryBottom As New GeometryModel3D
    8. ' Dim geometry(anzahl) As GeometryModel3D
    9. ReDim Preserve geometry(anzahl)
    10. 'Beispiel für Bild-Textur-------------------------
    11. Dim materialBack As New DiffuseMaterial
    12. Dim imgBack As New ImageBrush
    13. imgBack.ImageSource = New BitmapImage(New Uri("D:\corporatemark_t.png"))
    14. imgBack.Stretch = Stretch.Uniform
    15. materialBack.Brush = imgBack
    16. 'Beispiel für Farbtextur-------------------------
    17. Dim materialRight1 As New DiffuseMaterial
    18. Dim materialRight2 As New DiffuseMaterial
    19. Dim materialRight3 As New DiffuseMaterial
    20. Dim materialRight4 As New DiffuseMaterial
    21. Dim materialRight5 As New DiffuseMaterial
    22. Dim materialRight6 As New DiffuseMaterial
    23. Dim materialRight7 As New DiffuseMaterial
    24. Dim materialRight8 As New DiffuseMaterial
    25. Dim imgRight As New ImageBrush
    26. Dim transformGroup As New Transform3DGroup
    27. materialRight1.Brush = New SolidColorBrush(Colors.LimeGreen)
    28. materialRight2.Brush = New SolidColorBrush(Colors.Olive)
    29. materialRight3.Brush = New SolidColorBrush(Colors.DarkGreen)
    30. materialRight4.Brush = New SolidColorBrush(Colors.Blue)
    31. materialRight5.Brush = New SolidColorBrush(Colors.Yellow)
    32. materialRight6.Brush = New SolidColorBrush(Colors.Orange)
    33. materialRight7.Brush = New SolidColorBrush(Colors.OrangeRed)
    34. materialRight8.Brush = New SolidColorBrush(Colors.Red)
    35. 'materialRight.Brush = New LinearGradientBrush()
    36. ' Dim black_white_brush As New LinearGradientBrush(New Point(0, 0), New Point(1, 1), , Color.White)
    37. ProgressBar1.Maximum = anzahl
    38. Me.ProgressBar1.Visibility = Visibility.Visible
    39. For i = 0 To anzahl - 1
    40. ProgressBar1.Value = i
    41. Dim pointx As New Point3D(Koerper(1, 0, i), Koerper(1, 1, i), Koerper(1, 2, i))
    42. Dim pointy As New Point3D(Koerper(2, 0, i), Koerper(2, 1, i), Koerper(2, 2, i))
    43. Dim pointz As New Point3D(Koerper(3, 0, i), Koerper(3, 1, i), Koerper(3, 2, i))
    44. If array_farbe(i) = 1 Then
    45. geometry(i) = Paint3DRectangle(pointx, pointy, pointz, TextureP0, TextureP1, TextureP2, TextureP3, materialRight1)
    46. ElseIf array_farbe(i) = 2 Then
    47. geometry(i) = Paint3DRectangle(pointx, pointy, pointz, TextureP0, TextureP1, TextureP2, TextureP3, materialRight2)
    48. ElseIf array_farbe(i) = 3 Then
    49. geometry(i) = Paint3DRectangle(pointx, pointy, pointz, TextureP0, TextureP1, TextureP2, TextureP3, materialRight3)
    50. ElseIf array_farbe(i) = 4 Then
    51. geometry(i) = Paint3DRectangle(pointx, pointy, pointz, TextureP0, TextureP1, TextureP2, TextureP3, materialRight4)
    52. ElseIf array_farbe(i) = 5 Then
    53. geometry(i) = Paint3DRectangle(pointx, pointy, pointz, TextureP0, TextureP1, TextureP2, TextureP3, materialRight5)
    54. ElseIf array_farbe(i) = 6 Then
    55. geometry(i) = Paint3DRectangle(pointx, pointy, pointz, TextureP0, TextureP1, TextureP2, TextureP3, materialRight6)
    56. ElseIf array_farbe(i) = 7 Then
    57. geometry(i) = Paint3DRectangle(pointx, pointy, pointz, TextureP0, TextureP1, TextureP2, TextureP3, materialRight7)
    58. ElseIf array_farbe(i) = 8 Then
    59. geometry(i) = Paint3DRectangle(pointx, pointy, pointz, TextureP0, TextureP1, TextureP2, TextureP3, materialRight8)
    60. Else
    61. End If
    62. ResultModel3DGroup.Children.Add(geometry(i))
    63. Next
    64. 'Dim myDirectionalLight As New DirectionalLight()
    65. 'myDirectionalLight.Color = Colors.White
    66. 'myDirectionalLight.Direction = New Vector3D(-0.61, -0.5, spotlligth)
    67. 'ResultModel3DGroup.Children.Add(myDirectionalLight)
    68. Model.Content = ResultModel3DGroup
    69. ' transformGroup.Children.Add(New co(RotationX))
    70. transformGroup.Children.Add(New RotateTransform3D(RotationX))
    71. transformGroup.Children.Add(New RotateTransform3D(RotationY))
    72. transformGroup.Children.Add(New RotateTransform3D(RotationZ))
    73. transformGroup.Children.Add(TranslateZ)
    74. transformGroup.Children.Add(Translatey)
    75. transformGroup.Children.Add(Translatex)
    76. transformGroup.Children.Add(zeropoint)
    77. Model.Transform = transformGroup
    78. Me.mainViewport.Children.Remove(Model)
    79. Me.mainViewport.Children.Add(Model)
    80. End Sub


    Zudem der Aufruf einer Simulation

    VB.NET-Quellcode

    1. Dim rotateAnimationz As New DoubleAnimation
    2. rotateAnimationz.Duration = TimeSpan.Parse("0:0:4")
    3. rotateAnimationz.From = -180
    4. rotateAnimationz.To = 180
    5. rotateAnimationz.AutoReverse = True
    6. rotateAnimationz.AccelerationRatio = 0.1
    7. rotateAnimationz.DecelerationRatio = 0.1
    8. rotateAnimationz.RepeatBehavior = New RepeatBehavior(2)
    9. RotationZ.BeginAnimation(AxisAngleRotation3D.AngleProperty, rotateAnimationz)


    Die Darstellung erfolg auf einer WPF Oberfläche

    VB.NET-Quellcode

    1. <Viewport3D Name="mainViewport" ClipToBounds="True" RenderOptions.CachingHint="Cache" RenderOptions.EdgeMode="Aliased">
    2. <Viewport3D.Camera>
    3. <PerspectiveCamera x:Name="cam" Position="0,0,4" LookDirection="0,0,-3.5" ></PerspectiveCamera>
    4. </Viewport3D.Camera>
    5. <ModelVisual3D>
    6. <ModelVisual3D.Content>
    7. <DirectionalLight
    8. Color="White"
    9. Direction="0,0,-1" />
    10. <!--<SpotLight Color="Aqua" Direction="0,1,-1">
    11. </SpotLight>-->
    12. <!--<PointLight Color="White" >
    13. </PointLight>-->
    14. <!--<AmbientLight Color="White" >
    15. </AmbientLight>-->
    16. </ModelVisual3D.Content>
    17. </ModelVisual3D>
    18. </Viewport3D>


    Was kann ich machen damit die Simulation schneller und flüssiger wird???

    Edit by ErfinderDesRades: (Thema verschoben) Bitte richtiges UnterForum wählen!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ErfinderDesRades“ ()

    Hi, danke für die schnelle Antwort.

    ich habe Folgende DLL Importiert

    VB.NET-Quellcode

    1. Imports System.Windows.Media.Media3D
    2. Imports System.Windows.Media.Animation
    3. Imports System.IO
    4. Imports Microsoft.Win32


    Leider habe ich auch Probleme wenn ich die Simulation mit der Maus in verschiede Richtungen schwenken möchte. Von daher ist es die Einstellung der Duration nicht meine Lösung, denke ich.

    VB.NET-Quellcode

    1. Private Sub Grid_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
    2. If e.LeftButton = MouseButtonState.Pressed Then
    3. mdown = True
    4. Dim pos As Point = Mouse.GetPosition(mainViewport)
    5. mlastpos = New Point(pos.X - mainViewport.ActualWidth / 2, mainViewport.ActualHeight / 2 - pos.Y)
    6. Dim rayMeshResult As RayMeshGeometry3DHitTestResult = DirectCast(VisualTreeHelper.HitTest(mainViewport, pos), RayMeshGeometry3DHitTestResult)
    7. 'Dim hit As Model3D
    8. 'hit = rayMeshResult.ModelHit
    9. 'ResultModel3DGroup.Children.Remove(hit)
    10. End If
    11. End Sub
    12. Private Sub Grid_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
    13. If mdown = True Then
    14. Dim pos As Point = Mouse.GetPosition(mainViewport)
    15. Dim actualpos As Point = New Point(pos.X - mainViewport.ActualWidth / 2, mainViewport.ActualHeight / 2 - pos.Y)
    16. Dim dx As Double = actualpos.X - mlastpos.X, dy = actualpos.Y - mlastpos.Y
    17. Dim mouseangle As Double = 0
    18. If dx <> 0 And dy <> 0 Then
    19. mouseangle = Math.Asin(Math.Abs(dy) / Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)))
    20. If dx < 0 And dy > 0 Then
    21. mouseangle += Math.PI / 2
    22. ElseIf dx < 0 And dy < 0 Then
    23. mouseangle += Math.PI
    24. ElseIf dx > 0 And dy < 0 Then
    25. mouseangle += Math.PI * 1.5
    26. End If
    27. ElseIf dx = 0 And dy <> 0 Then
    28. mouseangle = If(Math.Sign(dy) > 0, Math.PI / 2, Math.PI * 1.5)
    29. ElseIf dx <> 0 And dy = 0 Then
    30. mouseangle = If(Math.Sign(dx) > 0, 0, Math.PI)
    31. End If
    32. Dim axisangle As Double = mouseangle + Math.PI / 2
    33. Dim axis As Vector3D = New Vector3D(Math.Cos(axisangle) * 4, Math.Sin(axisangle) * 4, 0)
    34. Dim rotation As Double = 0.01 * Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2))
    35. Dim group As Transform3DGroup = TryCast(Model.Transform, Transform3DGroup)
    36. Dim r As New QuaternionRotation3D(New Quaternion(axis, rotation * 180 / Math.PI))
    37. group.Children.Add(New RotateTransform3D(r))
    38. mlastpos = actualpos
    39. End If
    40. End Sub
    41. Private Sub Grid_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
    42. cam.Position = New Point3D(cam.Position.X, cam.Position.Y, cam.Position.Z - e.Delta / 50D)
    43. End Sub