Skip to content

Latest commit

 

History

History
192 lines (115 loc) · 24.8 KB

File metadata and controls

192 lines (115 loc) · 24.8 KB
title description ms.date ms.topic dev_langs helpviewer_keywords ms.assetid
3D Graphics Overview
Get acquainted with 3D graphics in Windows Presentation Foundation (WPF) to draw, transform, and animate 3D graphics in both markup and procedural code.
03/30/2017
overview
csharp
vb
3D graphics [WPF]
graphics [WPF], 3D
67f31ed4-e36b-4b02-9889-dcce245d7afc

3D Graphics Overview

The 3D functionality in Windows Presentation Foundation (WPF) enables developers to draw, transform, and animate 3D graphics in both markup and procedural code. Developers can combine 2D and 3D graphics to create rich controls, provide complex illustrations of data, or enhance the user experience of an application's interface. 3D support in WPF is not designed to provide a full-featured game-development platform. This topic provides an overview of 3D functionality in the WPF graphics system.

3D in a 2D Container

3D graphics content in WPF is encapsulated in an element, xref:System.Windows.Controls.Viewport3D, that can participate in the two-dimensional element structure. The graphics system treats xref:System.Windows.Controls.Viewport3D as a two-dimensional visual element like many others in WPF. xref:System.Windows.Controls.Viewport3D functions as a window—a viewport—into a three-dimensional scene. More accurately, it is a surface on which a 3D scene is projected.

In a conventional 2D application, use xref:System.Windows.Controls.Viewport3D as you would another container element like Grid or Canvas. Although you can use xref:System.Windows.Controls.Viewport3D with other 2D drawing objects in the same scene graph, you cannot interpenetrate 2D and 3D objects within a xref:System.Windows.Controls.Viewport3D. This topic will focus on how to draw 3D graphics inside the xref:System.Windows.Controls.Viewport3D.

3D Coordinate Space

The WPF coordinate system for 2D graphics locates the origin in the upper left of the rendering area (typically the screen). In the 2D system, positive x-axis values proceed to the right and positive y-axis values proceed downward. In the 3D coordinate system, however, the origin is located in the center of the rendering area, with positive x-axis values proceeding to the right but positive y-axis values proceeding upward instead, and positive z-axis values proceeding outward from the origin, toward the viewer.

Coordinate systems
Conventional 2D and 3D coordinate system representations

The space defined by these axes is the stationary frame of reference for 3D objects in WPF. As you build models in this space and create lights and cameras to view them, it's helpful to distinguish this stationary frame of reference, or "world space," from the local frame of reference you create for each model when you apply transformations to it. Remember also that objects in world space might look entirely different, or not be visible at all, depending on light and camera settings, but the position of the camera does not change the location of objects in world space.

Cameras and Projections

Developers who work in 2D are accustomed to positioning drawing primitives on a two-dimensional screen. When you create a 3D scene, it's important to remember that you are really creating a 2D representation of 3D objects. Because a 3D scene looks different depending on the onlooker's point of view, you must specify that point of view. The xref:System.Windows.Media.Media3D.Camera class allows you to specify this point of view for a 3D scene.

Another way to understand how a 3D scene is represented on a 2D surface is by describing the scene as a projection onto the viewing surface. The xref:System.Windows.Media.Media3D.ProjectionCamera allows you to specify different projections and their properties to change how the onlooker sees 3D models. A xref:System.Windows.Media.Media3D.PerspectiveCamera specifies a projection that foreshortens the scene. In other words, the xref:System.Windows.Media.Media3D.PerspectiveCamera provides vanishing-point perspective. You can specify the position of the camera in the coordinate space of the scene, the direction and field of view for the camera, and a vector that defines the direction of "up" in the scene. The following diagram illustrates the xref:System.Windows.Media.Media3D.PerspectiveCamera's projection.

The xref:System.Windows.Media.Media3D.ProjectionCamera.NearPlaneDistance%2A and xref:System.Windows.Media.Media3D.ProjectionCamera.FarPlaneDistance%2A properties of xref:System.Windows.Media.Media3D.ProjectionCamera limit the range of the camera's projection. Because cameras can be located anywhere in the scene, it's possible for the camera to be actually positioned inside a model or very near a model, making it hard to distinguish objects properly. xref:System.Windows.Media.Media3D.ProjectionCamera.NearPlaneDistance%2A allows you to specify a minimum distance from the camera beyond which objects will not be drawn. Conversely, xref:System.Windows.Media.Media3D.ProjectionCamera.FarPlaneDistance%2A lets you specify a distance from the camera beyond which objects will not be drawn, which ensures that objects too far away to be recognizable won't be included in the scene.

Camera setup
Camera position

xref:System.Windows.Media.Media3D.OrthographicCamera specifies an orthogonal projection of a 3D model to a 2D visual surface. Like other cameras, it specifies a position, viewing direction, and "upward" direction. Unlike xref:System.Windows.Media.Media3D.PerspectiveCamera, however, xref:System.Windows.Media.Media3D.OrthographicCamera describes a projection that does not include perspective foreshortening. In other words, xref:System.Windows.Media.Media3D.OrthographicCamera describes a viewing box whose sides are parallel, instead of one whose sides meet in a point at the camera. The following image shows the same model as viewed using xref:System.Windows.Media.Media3D.PerspectiveCamera and xref:System.Windows.Media.Media3D.OrthographicCamera.

Orthographic and perspective projection
Perspective and Orthographic Projections

The following code shows some typical camera settings.

[!code-csharp3dgallery_procedural_snip#Basic3DShapeCodeExampleInline1] [!code-vb3dgallery_procedural_snip#Basic3DShapeCodeExampleInline1]

Model and Mesh Primitives

xref:System.Windows.Media.Media3D.Model3D is the abstract base class that represents a generic 3D object. To build a 3D scene, you need some objects to view, and the objects that make up the scene graph derive from xref:System.Windows.Media.Media3D.Model3D. Currently, the WPF supports modeling geometries with xref:System.Windows.Media.Media3D.GeometryModel3D. The xref:System.Windows.Media.Media3D.GeometryModel3D.Geometry%2A property of this model takes a mesh primitive.

To build a model, begin by building a primitive, or mesh. A 3D primitive is a collection of vertices that form a single 3D entity. Most 3D systems provide primitives modeled on the simplest closed figure: a triangle defined by three vertices. Because the three points of a triangle are coplanar, you can continue adding triangles in order to model more complex shapes, called meshes.

The WPF 3D system currently provides the xref:System.Windows.Media.Media3D.MeshGeometry3D class, which allows you to specify any geometry; it does not currently support predefined 3D primitives like spheres and cubic forms. Begin creating a xref:System.Windows.Media.Media3D.MeshGeometry3D by specifying a list of triangle vertices as its xref:System.Windows.Media.Media3D.MeshGeometry3D.Positions%2A property. Each vertex is specified as a xref:System.Windows.Media.Media3D.Point3D. (In XAML, specify this property as a list of numbers grouped in threes that represent the coordinates of each vertex.) Depending on its geometry, your mesh might be composed of many triangles, some of which share the same corners (vertices). To draw the mesh correctly, the WPF needs information about which vertices are shared by which triangles. You provide this information by specifying a list of triangle indices with the xref:System.Windows.Media.Media3D.MeshGeometry3D.TriangleIndices%2A property. This list specifies the order in which the points specified in the xref:System.Windows.Media.Media3D.MeshGeometry3D.Positions%2A list will determine a triangle.

[!code-xamlbasic3d#Basic3DXAML3DN3]

In the preceding example, the xref:System.Windows.Media.Media3D.MeshGeometry3D.Positions%2A list specifies four vertices to define a rectangle mesh. The xref:System.Windows.Media.Media3D.MeshGeometry3D.TriangleIndices%2A property specifies a list of two groups of three indices. Each number in the list refers to an offset into the xref:System.Windows.Media.Media3D.MeshGeometry3D.Positions%2A list. For example, the first three vertices specified by the xref:System.Windows.Media.Media3D.MeshGeometry3D.Positions%2A list are :::no-loc text="(-1,-1,0)":::, :::no-loc text="(1,-1,0)":::, and :::no-loc text="(-1,1,0)":::. The first three indices specified by the xref:System.Windows.Media.Media3D.MeshGeometry3D.TriangleIndices%2A list are 0, 1, and 2, which correspond to the first, second, and third points in the xref:System.Windows.Media.Media3D.MeshGeometry3D.Positions%2A list. As a result, the first triangle that makes up the rectangle model will be composed from :::no-loc text="(-1,-1,0)"::: to :::no-loc text="(1,-1,0)"::: to :::no-loc text="(-1,1,0)":::, and the second triangle will be determined similarly.

You can continue defining the model by specifying values for the xref:System.Windows.Media.Media3D.MeshGeometry3D.Normals%2A and xref:System.Windows.Media.Media3D.MeshGeometry3D.TextureCoordinates%2A properties. To render the surface of the model, the graphics system needs information about which direction the surface is facing at any given triangle. It uses this information to make lighting calculations for the model: surfaces that face directly towards a light source appear brighter than those angled away from the light. Though the WPF can determine default normal vectors by using the position coordinates, you can also specify different normal vectors to approximate the appearance of curved surfaces.

The xref:System.Windows.Media.Media3D.MeshGeometry3D.TextureCoordinates%2A property specifies a collection of xref:System.Windows.Points that tell the graphics system how to map the coordinates that determine how a texture is drawn to the vertices of the mesh. xref:System.Windows.Media.Media3D.MeshGeometry3D.TextureCoordinates%2A are specified as a value between zero and 1, inclusive. As with the xref:System.Windows.Media.Media3D.MeshGeometry3D.Normals%2A property, the graphics system can calculate default texture coordinates, but you might choose to set different texture coordinates to control the mapping of a texture that includes part of a repeating pattern, for example. More information about texture coordinates can be found in subsequent topics or in the Managed Direct3D SDK.

The following example shows how to create one face of the cube model in procedural code. You can draw the entire cube as a single GeometryModel3D; this example draws the cube's face as a distinct model in order to apply separate textures to each face later.

[!code-csharp3doverview#3DOverview3DN6] [!code-vb3doverview#3DOverview3DN6]

[!code-csharp3doverview#3DOverview3DN7] [!code-vb3doverview#3DOverview3DN7]

'

Applying Materials to the Model

For a mesh to look like a three-dimensional object, it must have an applied texture to cover the surface defined by its vertices and triangles so it can be lit and projected by the camera. In 2D, you use the xref:System.Windows.Media.Brush class to apply colors, patterns, gradients, or other visual content to areas of the screen. The appearance of 3D objects, however, is a function of the lighting model, not just of the color or pattern applied to them. Real-world objects reflect light differently depending on the quality of their surfaces: glossy and shiny surfaces don't look the same as rough or matte surfaces, and some objects seem to absorb light while others glow. You can apply all the same brushes to 3D objects that you can apply to 2D objects, but you can't apply them directly.

To define the characteristics of a model's surface, WPF uses the xref:System.Windows.Media.Media3D.Material abstract class. The concrete subclasses of Material determine some of the appearance characteristics of the model's surface, and each also provides a Brush property to which you can pass a SolidColorBrush, TileBrush, or VisualBrush.

  • xref:System.Windows.Media.Media3D.DiffuseMaterial specifies that the brush will be applied to the model as though that model were lit diffusely. Using DiffuseMaterial most resembles using brushes directly on 2D models; model surfaces do not reflect light as though shiny.

  • xref:System.Windows.Media.Media3D.SpecularMaterial specifies that the brush will be applied to the model as though the model's surface were hard or shiny, capable of reflecting highlights. You can set the degree to which the texture will suggest this reflective quality, or "shine," by specifying a value for the xref:System.Windows.Media.Media3D.SpecularMaterial.SpecularPower%2A property.

  • xref:System.Windows.Media.Media3D.EmissiveMaterial allows you to specify that the texture will be applied as though the model were emitting light equal to the color of the brush. This does not make the model a light; however, it will participate differently in shadowing than it would if textured with DiffuseMaterial or SpecularMaterial.

For better performance, the backfaces of a xref:System.Windows.Media.Media3D.GeometryModel3D (those faces that are out of view because they are on the opposite side of the model from the camera) are culled from the scene. To specify a xref:System.Windows.Media.Media3D.Material to apply to the backface of a model like a plane, set the model's xref:System.Windows.Media.Media3D.GeometryModel3D.BackMaterial%2A property.

To achieve some surface qualities, like glowing or reflective effects, you might want to apply several different brushes to a model in succession. You can apply and reuse multiple Materials by using the xref:System.Windows.Media.Media3D.MaterialGroup class. The children of the MaterialGroup are applied first to last in multiple rendering passes.

The following code examples show how to apply a solid color and a drawing as brushes to 3D models.

[!code-xamlbasic3d#Basic3DXAML3DN5]
[!code-xaml3doverview#3DOverview3DN9]
[!code-csharp3doverview#3DOverview3DN8]
[!code-vb3doverview#3DOverview3DN8]

Illuminating the Scene

Lights in 3D graphics do what lights do in the real world: they make surfaces visible. More to the point, lights determine what part of a scene will be included in the projection. Light objects in WPF create a variety of light and shadow effects and are modeled after the behavior of various real-world lights. Include at least one light in your scene, or no models will be visible.

The following lights derive from the base class xref:System.Windows.Media.Media3D.Light:

  • xref:System.Windows.Media.Media3D.AmbientLight: Provides ambient lighting that illuminates all objects uniformly regardless of their location or orientation.

  • xref:System.Windows.Media.Media3D.DirectionalLight: Illuminates like a distant light source. Directional lights have a xref:System.Windows.Media.Media3D.DirectionalLight.Direction%2A specified as a Vector3D, but no specified location.

  • xref:System.Windows.Media.Media3D.PointLight: Illuminates like a nearby light source. PointLights have a position and cast light from that position. Objects in the scene are illuminated depending on their position and distance with respect to the light. xref:System.Windows.Media.Media3D.PointLightBase exposes a xref:System.Windows.Media.Media3D.PointLightBase.Range%2A property, which determines a distance beyond which models will not be illuminated by the light. PointLight also exposes attenuation properties, which determine how the light's intensity diminishes over distance. You can specify constant, linear, or quadratic interpolations for the light's attenuation.

  • xref:System.Windows.Media.Media3D.SpotLight: Inherits from xref:System.Windows.Media.Media3D.PointLight. Spotlights illuminate like PointLight and have both position and direction. They project light in a cone-shaped area set by xref:System.Windows.Media.Media3D.SpotLight.InnerConeAngle%2A and xref:System.Windows.Media.Media3D.SpotLight.OuterConeAngle%2A properties, specified in degrees.

Lights are xref:System.Windows.Media.Media3D.Model3D objects, so you can transform and animate light properties, including position, color, direction, and range.

[!code-xamlhittest3d#HitTest3D3DN6]

[!code-csharpbasic3d#Basic3D3DN11] [!code-vbbasic3d#Basic3D3DN11]

[!code-csharpbasic3d#Basic3D3DN12] [!code-vbbasic3d#Basic3D3DN12]

[!code-csharpbasic3d#Basic3D3DN13] [!code-vbbasic3d#Basic3D3DN13]

Transforming Models

When you create models, they have a particular location in the scene. To move those models around in the scene, to rotate them, or to change their size, it's not practical to change the vertices that define the models themselves. Instead, just as in 2D, you apply transformations to models.

Each model object has a xref:System.Windows.Media.Media3D.Model3D.Transform%2A property with which you can move, reorient, or resize the model. When you apply a transform, you effectively offset all the points of the model by whatever vector or value specified by the transform. In other words, you've transformed the coordinate space in which the model is defined ("model space"), but you haven't changed the values that make up the model's geometry in the coordinate system of the entire scene ("world space").

For more information about transforming models, see 3D Transformations Overview.

Animating Models

The WPF 3D implementation participates in the same timing and animation system as 2D graphics. In other words, to animate a 3D scene, animate the properties of its models. It's possible to animate properties of primitives directly, but it's typically easier to animate transformations that change the position or appearance of models. Because transformations can be applied to xref:System.Windows.Media.Media3D.Model3DGroup objects as well as individual models, it's possible to apply one set of animations to a child of a Model3DGroup and another set of animations to a group of child objects. You can also achieve a variety of visual effects by animating the properties of your scene's lighting. Finally, you might choose to animate the projection itself by animating the camera position or field of view. For background information on the WPF timing and animation system, see the Animation Overview, Storyboards Overview, and Freezable Objects Overview topics.

To animate an object in WPF, you create a timeline, define an animation (which is really a change in some property value over time), and specify the property to which to apply the animation. Because all the objects in a 3D scene are children of xref:System.Windows.Controls.Viewport3D, the properties targeted by any animation you want to apply to the scene are properties of Viewport3D.

Suppose you want to make a model appear to wobble in place. You might choose to apply a xref:System.Windows.Media.Media3D.RotateTransform3D to the model, and animate the axis of its rotation from one vector to another. The following code example demonstrates applying a Vector3DAnimation to the Axis property of the transformation's Rotation3D, assuming the RotateTransform3D to be one of several transforms applied to the model with a TransformGroup.

[!code-csharp3doverview#3DOverview3DN1] [!code-vb3doverview#3DOverview3DN1]

[!code-csharp3doverview#3DOverview3DN3] [!code-vb3doverview#3DOverview3DN3]

[!code-csharp3doverview#3DOverview3DN4] [!code-vb3doverview#3DOverview3DN4]

[!code-csharp3doverview#3DOverview3DN5] [!code-vb3doverview#3DOverview3DN5]

Add 3D Content to the Window

To render the scene, add models and lights to a xref:System.Windows.Media.Media3D.Model3DGroup, then set the xref:System.Windows.Media.Media3D.Model3DGroup as the xref:System.Windows.Media.Media3D.ModelVisual3D.Content%2A of a xref:System.Windows.Media.Media3D.ModelVisual3D. Add the xref:System.Windows.Media.Media3D.ModelVisual3D to the xref:System.Windows.Controls.Viewport3D.Children%2A collection of the xref:System.Windows.Controls.Viewport3D. Add cameras to the xref:System.Windows.Controls.Viewport3D by setting its xref:System.Windows.Controls.Viewport3D.Camera%2A property.

Finally, add the xref:System.Windows.Controls.Viewport3D to the window. When the xref:System.Windows.Controls.Viewport3D is included as the content of a layout element like Canvas, specify the size of the Viewport3D by setting its xref:System.Windows.FrameworkElement.Height%2A and xref:System.Windows.FrameworkElement.Width%2A properties (inherited from xref:System.Windows.FrameworkElement).

[!code-xamlhostingwpfusercontrolinwf#1]

See also