Interacting with 2D on 3D in WPF

Interacting with 2D placed on 3D is now possible in v1 of the Windows Presentation Foundation!

In between shipping Vista and planning the next version of WPF, we realized that with a clever implementation it was possible to provide this feature today on v1 bits, and I’m happy to report that as of now, the binaries (and source code to them) needed to do this are available for download (here).

The download also contains two sample applications - InteractiveViewport3DSample and Channel9Demo - which illustrate how to use the code.  So if you just want to dive right in to 2D on 3D, that's a good place to start. 

The rest of this post will describe how to use this feature as a developer, as well as how it’s implemented behind the scenes.  If you want the video version though, which includes a cool demo of an app we made with the 2D on 3D code (photo shown below – source here, check it out on Channel9 (here).

Flickr Application

How do you use it?

There are two main classes needed to add 2D on 3D to you app: Interactive3DDecorator and InteractiveVisual3D.  The Interactive3DDecorator handles the majority of the work that allows the 2D on 3D interaction to happen.  To use it, you simply place it around the Viewport3D that you intend to make interactive.  So in XAML, you have:



Then, to actually add interactive 2D on 3D objects in to your scene, you use the InteractiveVisual3D class.  InteractiveVisual3D is a subclass of ModelVisual3D and provides the following dependency properties which set up the interactive 2D on 3D:

  • Geometry  - The Geometry3D that is to become the content of the InteractiveVisual3D .
  • Visual – The 2D you want to interact with on the 3D geometry.
  • Material – A user specified material to use on the 3D object.  By default, a DiffuseMaterial is used.
  • IsBackVisible – Indicates whether the material used for the front face should also be mirrored on the back face.

The first two properties, Geometry and Visual, are the primary ones needed.  They allow the geometry that should be used to bet set, as well as the Visual that should appear on that geometry.  For example, the below XAML code demonstrates how to create an InteractiveVisual3D.

<local:InteractiveVisual3D Geometry="{StaticResource PlaneMesh}">
<Label Content="Sample UI" />
<Button Content="Close Window"/>
<TextBox />

The other properties allow for fine tuning of the object’s appearance.  The Material property allows a user to create their own material for the object.  To do so, a user can create a material as always, and then specify using the IsInteractiveMaterial attached property which material is intended to be “interactive” (i.e. the VisualBrush created using the passed in Visual is set as the Brush for that material). Note, this attached property must be set to true on at least one material – otherwise an exception will be thrown.  If you ever want to disable interaction, you can set “IsHitTestVisible” on the Visual to be false.  As an example of setting a custom material, the following code sets the material to be composed of a DiffuseMaterial, which will contain the visual brush, and a SpecularMaterial.

<DiffuseMaterial local:InteractiveVisual3D.IsInteractiveMaterial="True"/>
<SpecularMaterial Brush="Red" />

Finally, IsBackVisible sets the material used on the front face to also be used on the back face.  Currently, the Interactive3DDecorator does not distinguish between front and back faces, so this allows for there to be interaction with the back face as well.

How does it work?

At a very high level, the interaction with 2D on 3D is achieved by really interacting with a hidden version of that 2D content in 2D.  The 2D is positioned such that the point in 3D the mouse is over is the exact same point as the mouse is over on the hidden 2D version.  Then, when the user clicks, etc… they are interacting with exactly the same location.  If you want to see this for yourself, you can set the “Debug” property on Interactive3DDecorator to true, which makes the hidden layer partially visible.

The Interactive3DDecorator then consists of two elements: the 3D content that is displayed within it and a hidden layer that is used to position and display the 2D content that is being interacted with.  Depending on what 2D content on 3D is being interacted with, the hidden layer changes to hold that 2D content.  The images below give a visual representation of what is going on.  The first photo is just of the 2D content we wish to place on 3D.  The second is that 2D content on 3D.  And finally, the third image shows how the 2D on 3D interaction takes place.  When the mouse moves over the “B” in the button, the hidden layer (shown to be only partially transparent for this example) is moved such that the mouse is over the same point in the hidden layer as it is in the 3D scene.

To figure out where to position the hidden layer works as follows.  When the mouse moves in the 3D scene, a ray is shot in to the 3D scene to see if it intersects any object.  If it hit an object, and it is an InteractiveVisual3D, we can use the return parameters from the intersection to compute the texture coordinate that was hit.  Then from these, we can map from the (u,v) value of the texture coordinate, on to an x,y point on the 2D visual, which is the point we need to place under the mouse.  More specifically, the code assumes texture coordinates are all in the range (0,0) to (1,1) – i.e. upper left to lower right of the image (for those planning to use this, this is important to know, since your texture coordinates need to be within this range to enable interaction with the 2D content).  Then the point on the 2D object that was hit is simply (u * Width, v * Height).

One of the great things about this method then is that the only event that needs to be tracked is when the mouse moves.  There’s no need for catching and forwarding on every type of event: the hidden layer takes care of all of this work.  

Positioning With Capture

There’s one very interesting “gotcha” though: what happens when one of the 2D objects grabs capture and then you move off the 3D mesh it is on?  For example, you select some text, and then move the mouse above that selected text, or click and hold on a button, and then move away from it.  Correct hidden content positioning becomes more complicated in this case.  The problem becomes difficult for many reasons.  In the normal 2D situation, both the mouse position and the 2D content exist in the same plane.  The transformation that is applied to the 2D content can be used to transform the mouse position to the content’s local coordinate system.  However in 3D, due to the projection of 3D on to a 2D plane, the mouse’s position actually corresponds to a line in 3D space.  In addition, the element with capture could also be mapped to any arbitrary geometry.  When the mouse is over the 3D object, hit testing tells us where it is relative to the 2D visual.  When it is off the 3D object, due to the above issues, there is no longer a straight forward answer to this question: the 2D point corresponds to a 3D line and the 2D content could be on arbitrary 3D geometry.  Also, because the element has capture, it wants to receive all events.  Before, we only needed to be sure that the mouse was over the correct object at all times.  Now we need to position the hidden visual such that it is in the proper position relative to the object that has capture.

To solve this issue, the code takes the following approach:  The overall idea is to reduce the 3D problem back to 2D.  In the normal 2D case, the transformations applied to the content can be used to convert the mouse position to the content’s local coordinate system.  This transformed position then lets the content know where the mouse is relative to it.  In 3D, due to the many orientations of the geometry and texture coordinate layouts, it’s difficult to say where a 3D point is in the relative coordinate system of the 2D content on 3D.  To approximate this, the outline of the 2D content on 3D, after it has been projected to screen space, is computed, and then the mouse is positioned based on this projection. 

For example, take the 3 images below.  The first is the 2D content on 3D.  In the second image, text is selected, and the mouse is moved to a point off the object.  The third image is the outline of the text box (the object that has capture).  This outline is then used to position the hidden visual. 

After the outline is available, The closest point on this outline to the mouse position is computed, and then this point on the outline is considered what was “hit” and it is placed under the moue position (hence, highlighting up to the “T” in the middle image below).  Since we place the mouse by the closest edge point, the interaction tends to behave as it would in 2D, since we position the hidden content based on what the mouse is closest to on the 2D content on 3D.  By placing it at the closest edge point, we are explicitly stating about where we expect the mouse to be relative to the 2D on 3D element’s orientation.

This method helps provide an intuitive response to the interaction, since the interaction happens with the closest point on the object with capture to the mouse.


Enjoy using it!  We’ve had a lot of fun building this, and can’t wait to see what kind of incredible applications will be built with it!