Adding 3-D Features In WPF C# Help

This section gives you an introduction to the 3-D features of WPF.Here you’ll find the information to get started.

To understand 3-D with WPF it is important to know the difference of the coordination system. fisua’e  35-22 shows the coordination system of WPF 3-D. The origin is placed in the center. The x-axis hu positive values to the ript and negative values to the left. The y-axis is vertical with positive vatu. up and negative vu.. down. The z-axis defines positive values in direction to the viewer.

Figure 35.22

The most important class and their functionality are described in the following table.




This section starts with a simple 3-D sample. A 3-D model is made up of triangles, so a simple model is Just one triangle. The triangle is defined by the positions property of the MeshGeometry3D. The three points all use the same z coordinate, -4, and x/y coordinates -1′-1,1-1, and 0 1.The property Trianglelndices indicates the order of the positions in a counterclockwise way. With this property you define which side of the triangle is visible. One side of the triangle shows the color defined with the Material property of the GeometryMode13D class, and the other side shows the BackMaterial property.

The camera that is used to show the scenario is positioned at the coordinates 0, 0, 0, and looks into the direction 0, 0, -8. Changing the camera position to the left side, the rectangle moves to the right and vice versa. Changing the y position of the camera, the rectangle appears larger or smaller.

The light that is used in this scene is an AmbientLight to light up the complete scene with a white light.


Figure 35-23

Changing Lights

It just shows a simple triangle where you can get the same result with less effort using 2-D. However, from here you can continue getting into 3-D features. For example, by changing the light from an ambient light to a spotlight with-the element SpotLight you can immediately see a different appearance of the triangle. With the spotlight you define a position where the light is placed, and the position to which the light is directed. Specifying -1 1 2 for the position, the light is placed at the left comer of the triangle and the y coordinate to the height of the triangle. From there the light is directed down and to the left. You can see the new appearance of the triangle.

Figure 35-24

Adding Textures

Instead of using a Solid color brush with the materials of the triangle, you can use a differenrbrush such . as the LinearGradientBruah as shown with the following XAMLcode. The LinearGradientBrush element defined with DiffuseMaterial defines gradient stops with the colors yellow, orange, red, blue, and violet. Tomap a 2-D surface from an object such as the brush to a 3-D geometry, the TextCoordinates property must be set. TextCoordinates defines a collection of 2-D points that map to the 3-Dpositions. Figure 35-25shows the 2-D coordinates of the brush from the sample application. The first position in the triangle, -1-1, maps to the brush coordinates 01; the position 1-1, which is the lower comer on the right, maps to 11 of the brush, which is violet; and 0 1 maps to 0.5 O.

Figure 35-25


Figure 35-26

You can add text or other controls in a similar way to the materials. To do this you just need to create a VisualBrush with the elements that should be painted.

3-Dimenslonal Object

Now let’s get into a real three-dimensional object: a box. The box is made up of five rectangles: the back, front, left, right, and bottom sides. Each rectangle is made up of two triangles because this is the core of a mesh. With WPF and 3-D the term mesh is used to describe the triangle primitive for building 3-Dshapes.

Here IS the code of the rectangle for the front side of the box that consists of two triangles. The positions of the triangles are set in a counterclockwise order as defined by the Trianglelndices. The front side of the rectangle is done with a red brush; the back side with a gray brush. Both of these brushes are of type Sol idColorBrush and defined with the resources of the window.



All the rectangles are combined within a Model3DGroup, so one transformation can be done with all the sides of the box:

With the Trans form property of the Model3DGroup element, all the geometries inside this group can be transformed. Here a RotateTransform3D isu sed that defines an AxisAngleRotation3D. To rotate the box during runtime, the Angle property isbound to the value of a Slider control.


To see .the box, a camera is needed. Here the PerspectiveCamera is used so that the box gets smaller the further the camera is. The position and direction of the camera can be set during runtime.


The other light source is a SpotLight. With this light source it is possible to highlight a specific area on the box. The SpotLight defines the properties InnerConeAngle and OuterConeAngle to define the area of the full illumination:


When running the application you can change the rotation of the box, the camera, and lights as shown.

Creating a 3-D model tiuit consists ofjust rectangles or triangles is asy to do. You would not manually create more complex models; instead you would use one of several tools. You can find 3-D tools for WPF atwww.codeplex/3DTools.

Figure 35-27

Posted on October 31, 2015 in Advanced WPF

Share the Story

Back to Top