WPF 3D Model in VB.NET

In this article you will learn about how to draw and work with 3D Model in WPF.
  • 5821

3D Model: 3D graphics is a powerful feature in WPF it creates the user interface more interactive and the basic thing which you have remember always to create an 3D graphics is about the dimensional model of an 3D object, the dimensional model have three dimensions and you have to define as a ViewPort3D-object, a camera, at least one light and of course the models you want to display. To design a complex 3d model you will be rendered, that has been designed with 3D Studio Max (3ds-file). To achieve this, we will have to export a 3ds-model to XAML. There are some nice tools available to convert 3ds to XAML, we will use a free one called Viewer3DS from Andrej Benedik available here. You can just drag and drop a 3s file to Viewer3ds and export the whole model to XAML.

All surfaces of objects have a material and a brush. The material defines how much light is reflected for a specific angle and the brush defines the color. A brush can either be a simple color or a gradient or even an image called texture.

Example of 3D Model

<Window x:Class="MainWindow"
  Title="" Height="300" Width="300" Loaded="Window1_Loaded">
        <MeshGeometry3D x:Key="triangleMesh" Positions="-1,-1,0 5,-2,-1 1,1,0" TriangleIndices="0 1 2" />
    <Viewport3D x:Name="a">
            <PerspectiveCamera LookDirection="0,0,-1" Position="0,0,5" />
                <PointLight Position="0,-1,1" Color="White" />


Imports System.Windows
Imports System.Windows.Media
Imports System.Windows.Media.Media3D
Class MainWindow
    Inherits Window
    Public Sub New()
    End Sub
    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim triangleMesh As MeshGeometry3D = DirectCast(TryFindResource("triangleMesh"), MeshGeometry3D)
        For i As Integer = 0 To 3
            Dim modelVisual3D As New ModelVisual3D()
            Dim geometryModel3D As New GeometryModel3D()
            geometryModel3D.Geometry = triangleMesh
            geometryModel3D.Material = New DiffuseMaterial(Brushes.Firebrick)
            modelVisual3D.Content = geometryModel3D
            Dim rotateTransform As New RotateTransform3D()
            rotateTransform.Rotation = New AxisAngleRotation3D(New Vector3D(0, 0, -1), i * 40)
            modelVisual3D.Transform = rotateTransform 
    End Sub
End Class

Output Window



Hope this article would help you to understanding the 3D concept in WPF.


More Articles

© 2019 DotNetHeaven. All rights reserved.