The following class can be used to keep UI forms (or any models) relative to the camera.
The class is a Model3DVisual, so it can have children (models). Don't set its transform, as that's done automatically by the class as the camera moves.
As the camera changes position and orientation, the class changes its own coordinate space (via a transform) so that the origin is always centered at the camera. The X and Y axes point right and up, respectively, relative to the camera, and the Z axis extends
negatively into the scene.
To use the class, you'll need a Viewport3D and a perspective camera:
Form3DManager formMgr = new Form3DManager(camera);
Once the form manager's added to the viewport, you can add models to it:
When you set your model's transform, your model moves relative to the camera, and maintains its position relative to the camera, as the camera moves and changes orientation. Remember that negative Z values are in front of the camera.
Here's the class:
public class Form3DManager : ModelVisual3D
public Form3DManager(PerspectiveCamera camera)
camera.Changed += new EventHandler(camera_Changed);
private void camera_Changed(object sender, EventArgs e)
PerspectiveCamera camera = sender as PerspectiveCamera;
// Get the inverse of the camera's view matrix.
Matrix3D inverseViewMatrix = MathUtils.GetViewMatrix(camera);
// Build a transform (matrix) to transform this ModelVisual3D
// into the camera's coordinate system.
MatrixTransform3D transformToCameraSpace =
new MatrixTransform3D(inverseViewMatrix * camera.Transform.Value);
this.Transform = transformToCameraSpace;
Notice the class uses the GetViewMatrix() method from the WPF 3D Tools MathUtils class.