What’s new in WPF 3.5
With the release of WPF 3.5 beta 2 (download here: http://www.microsoft.com/downloads/details.aspx?FamilyId=D2F74873-C796-4E60-91C8-F0EF809B09EE&displaylang=en), we’ve added some exciting new features to WPF 3D. At a very high level these can be grouped in to two main additions: UIElement3D and Viewport2DVisual3D. Over the next couple of weeks we’ll be adding examples and tips and tricks to the blog on these new additions, but for now we’ll start with a quick overview of what both of these provide.
UIElement3D
In the 2D world, UIElement adds layout, input, focus and eventing on to Visual. UIElement3D brings these same things (except no layout) to 3D. What this means is that the standard events and means of adding event handlers that you’re used to with UIElements now applies in the 3D world with UIElement3D.
UIElement3D itself is an abstract class that derives from Visual3D. To make it useable out of the box without having to derive from UIElement3D yourself, we’ve provided two new classes: ModelUIElement3D and ContainerUIElement3D.
ContainerUIElement3D does exactly what its name says. It is a container for other Visual3Ds. It has one main property, Children, which is used to add and remove 3D children. The ContainerUIElement3D doesn’t have a visual representation itself, but rather is just a collection of other 3D objects.
ModelUIElement3D has one property, Model, which is the Model3D that should be displayed to represent the UIElement3D. It has no children itself, and in some ways you can think of ModelUIElement3D like a Shape in the 2D world.
If you’re familiar with ModelVisual3D, then ContainerUIElement3D and ModelUIElement3D should look very familiar. The difference is we’ve gone and split the functionality of ModelVisual3D (i.e. a model and children) in to two separate classes, one with a model and the other with children.
With these then, making use of layout, focus and eventing is very easy. For instance, say you want to create a 3D object that responds to mouse events, you can just do:
<ModelUIElement3D MouseDown="OnMouseDown"/>
And then in the code behind have:
protected void OnMouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Hello");
}
If you have experience adding event handlers in the 2D world, then this should look immediately familiar. In fact it’s exactly the same, and UIElement3D is going to handle the same routed events that a UIElement deals with. With the addition of UIElement3D you now get all the great functionality that UIElement provided to 2D, but now in the 3D world!
Viewport2DVisual3D
The second main addition is Viewport2DVisual3D which enables you to put interactive 2D on 3D in WPF. If you’ve used the 3DTools work that was released shortly after the 3.0 release of WPF, Viewport2DVisual3D will look very familiar to you. With 3.5 though we’ve fully integrated 2D on 3D in to WPF rather than requiring a separate DLL to have this feature. The 2D and 3D trees are also integrated now, so if you walk up to a 2D visual on 3D and ask it for its parent, it will tell you the 3D object it is on.
Viewport2DVisual3D derives from Visual3D and has three main dependency properties: Visual, Geometry and Material.
Visual – This is the 2D Visual that will be placed on the 3D object.
Geometry – The 3D geometry for the Viewport2DVisual3D
Material – This describes the look of the 3D object. You can use any material you want. For the material that you want to have the Visual be placed on, you simply need to set the Viewport2DVisual3D.IsVisualHostMaterial attached property to true.
Below is a XAML example using Viewport2DVisual3D
<Viewport2DVisual3D Geometry="{StaticResource plane}">
<Viewport2DVisual3D.Material>
<DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="true" />
</Viewport2DVisual3D.Material>
<Button>3.5!</Button>
</Viewport2DVisual3D>
The above is a very quick overview of what is added in 3.5, so expect more posts on more specific details as well as some code samples on how to use it.
-Kurt Berglund