WPF 3D Model in VB.NET

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

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"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Background="White"
  Title="" Height="300" Width="300" Loaded="Window1_Loaded">
    <Window.Resources>
        <MeshGeometry3D x:Key="triangleMesh" Positions="-1,-1,0 5,-2,-1 1,1,0" TriangleIndices="0 1 2" />
    </Window.Resources>
    <Viewport3D x:Name="a">
        <Viewport3D.Camera>
            <PerspectiveCamera LookDirection="0,0,-1" Position="0,0,5" />
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <PointLight Position="0,-1,1" Color="White" />
            </ModelVisual3D.Content>
        </ModelVisual3D>
   
</Viewport3D>
</
Window>

//File:MainWindow.xaml.vb 

Imports System.Windows
Imports System.Windows.Media
Imports System.Windows.Media.Media3D
Class MainWindow
    Inherits Window
    Public Sub New()
        InitializeComponent()
    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 
            a.Children.Add(modelVisual3D)
        Next
    End Sub
End Class

Output Window

geo.gif

Conclusion

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.