Introduction
We learned about 2D graphics in the last article on
2D Graphics in WPF. But the world is full of 3D objects, hence we should be able to havea 3D experience in developing as well.
Every 3D object in the world is represented by 3 points: X, Y & Z. X & Y are the axis coordinates specifying the height & width of an object, where Z represents the depth of an object. We can draw, transform, and animate 3D graphics in WPF.
Let's get started. Following are the things we need:
Viewport3D
It functions as a window. We have to place our 3D object inside this window.
Camera
Every 3D image is different from a different angle. It depends on where the viewer is standing.
In order to specify that point of view. we can use
CameraPerspective. CameraPerspective specifies which X,Y, & Z coordinate you are facing.
- <Viewport3D.Camera>
- <PerspectiveCamera Position=" 6 5 4" LookDirection="-6 -5 -4"/>
- </Viewport3D.Camera>
ModelVisual3D
It specifies the look & feel of a 3D object. You can add lights (filling up objects with a brush) to a Model3DGroup.
- <ModelVisual3D>
- <ModelVisual3D.Content>
- <DirectionalLight Direction="-1 -1 -1"/>
- </ModelVisual3D.Content>
- </ModelVisual3D>
GeometryModel3D
Model3D is an abstract base class.
To build a 3D scene, you need some objects. We can achieve that with GeometryModel3D.
MeshGeometry3D
You need to specify a list of triangle vertices as its position property. This triangles define the clarity of the object. Each group of 3 numbers represents the coordinates of each point of the 3D object.
- <ModelVisual3D>
- <ModelVisual3D.Content>
- <GeometryModel3D>
- <GeometryModel3D.Geometry>
- <MeshGeometry3D
- Positions = "0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 1 0 1 0 1 1 0 1 1"
- TriangleIndices = "2 3 1 3 1 0 7 1 3 7 5 1 6 5 7 6 4 5 6 2 0
- 2 0 4 2 7 3 2 6 7 0 1 5 0 5 4"/>
- </GeometryModel3D.Geometry>
- </GeometryModel3D>
- </ModelVisual3D.Content>
- </ModelVisual3D>
GeometryModel3D.Material
We have created a 3D object so far, but without texture it will not have that 3D feel. We have seen how colors can be shiny or matte in real life, and we can achieve such an effect in 3D modelling.
Some of them are as follows,
- DiffuseMaterial: Matte in color.
- SpecularMaterial: Shiny color, colors could be reflective. By setting up SpecularPower property we can define the intensity of texture.
- EmissiveMaterial: The intensity of emitting light is equal to the color of the brush.
It has a different form of DiffuseMaterial or SpecularMaterial in look & feel.
- <GeometryModel3D.Material>
- <DiffuseMaterial>
- <DiffuseMaterial.Brush>
- <SolidColorBrush Color = "Bisque"/>
- </DiffuseMaterial.Brush>
- </DiffuseMaterial>
- </GeometryModel3D.Material>
ModelVisual3D.Transform
You can transform light properties, position, color, direction etc. e.g. rotating.
- <ModelVisual3D.Transform>
- <RotateTransform3D>
- <RotateTransform3D.Rotation>
- <AxisAngleRotation3D x:Name="Roate3DOjbect" Axis="1 2 1"/>
- </RotateTransform3D.Rotation>
- </RotateTransform3D>
- </ModelVisual3D.Transform>
Now let's see how our XAML would look with all of those elements mentioned above.
- <Window x:Class="A.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:A"
- mc:Ignorable="d"
- Title="MainWindow" Height="200" Width="200">
- <Grid>
- <Viewport3D>
- <Viewport3D.Camera>
- <PerspectiveCamera Position=" 6 5 4" LookDirection="-6 -5 -4"/>
- </Viewport3D.Camera>
- <ModelVisual3D>
- <ModelVisual3D.Content>
- <DirectionalLight Direction="-1 -1 -1"/>
- </ModelVisual3D.Content>
- </ModelVisual3D>
- <ModelVisual3D>
- <ModelVisual3D.Content>
- <GeometryModel3D>
- <GeometryModel3D.Geometry>
- <MeshGeometry3D
- Positions = "0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 1 0 1 0 1 1 0 1 1"
- TriangleIndices = "2 3 1 3 1 0 7 1 3 7 5 1 6 5 7 6 4 5 6 2 0
- 2 0 4 2 7 3 2 6 7 0 1 5 0 5 4"/>
- </GeometryModel3D.Geometry>
- <GeometryModel3D.Material>
- <DiffuseMaterial>
- <DiffuseMaterial.Brush>
- <SolidColorBrush Color = "Yellow"/>
- </DiffuseMaterial.Brush>
- </DiffuseMaterial>
- </GeometryModel3D.Material>
- </GeometryModel3D>
- </ModelVisual3D.Content>
- <ModelVisual3D.Transform>
- <RotateTransform3D>
- <RotateTransform3D.Rotation>
- <AxisAngleRotation3D x:Name="Roate3DOjbect" Axis="1 2 1"/>
- </RotateTransform3D.Rotation>
- </RotateTransform3D>
- </ModelVisual3D.Transform>
- </ModelVisual3D>
- </Viewport3D>
-
- <Slider Minimum="0"
- Maximum="360"
- Height="20"
- Width="100"
- VerticalAlignment="Top"
- Value="{Binding ElementName=Roate3DOjbect, Path=Angle}"/>
- </Grid>
- </Window>
The output would look something like this,
What a cute cube.
This is a small example of how to create 3D objects in WPF.
Conclusion
In this article, we learned how to create a 3D object in WPF, how we can perform the basic transformation of an object & how close the relationship between these 3D objects in WPF & in real life is.
Use the same elements to create new 3D graphics in WPF, because base classes never change or hardly change.
Thank you & happy coding!
Connect with me,
References