Matrix3DProjection Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Enables you to apply a Matrix3D to an object.

Inheritance Hierarchy

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows.Media..::.Projection
      System.Windows.Media..::.Matrix3DProjection

Namespace:  System.Windows.Media
Assembly:  System.Windows (in System.Windows.dll)

Syntax

<ContentPropertyAttribute("ProjectionMatrix", True)> _
Public NotInheritable Class Matrix3DProjection _
    Inherits Projection
[ContentPropertyAttribute("ProjectionMatrix", true)]
public sealed class Matrix3DProjection : Projection

The Matrix3DProjection type exposes the following members.

Constructors

  Name Description
Matrix3DProjection Initializes a new instance of a Matrix3DProjection class.

Top

Properties

  Name Description
Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
ProjectionMatrix Gets or sets the Matrix3D that is used for the projection that is applied to the object.

Top

Methods

  Name Description
CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetAnimationBaseValue Returns any base value established for a Windows Phone dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetType Gets the Type of the current instance. (Inherited from Object.)
GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Fields

  Name Description
ProjectionMatrixProperty Identifies the ProjectionMatrix dependency property.

Top

Remarks

Matrix3DProjection is a wrapper class around a Matrix3D.

You need to provide a three-dimensional (3-D) transform matrix that goes from object space (UIElement space) to homogeneous screen space, and the system implicitly does the perspective divide on resulting coordinates.

You can use the Matrix3DProjection and Matrix3D types for more complex semi–3-D scenarios than are possible with the PlaneProjection. Matrix3DProjection provides you with a complete 3-D transform matrix to apply to any UIElement so that you can apply arbitrary model transformation matrices and perspective matrices to XAML UI elements.

Because these APIs are minimal, if you use them, you need to write the code that correctly creates the 3-D transform matrices. Therefore, it is easier to use PlaneProjection for most 3-D scenarios.

To use Matrix3DProjection, you need to use standard 3-D transformation matrices. For more information about how to construct and use these 3-D matrices, refer to the DirectX documentation. Functions of particular note to users of Matrix3DProjection are the standard translate, scale, rotate, and perspective matrices.

Examples

The following example uses a simple Matrix3D matrix to transform the image in the X and Y directions when you tap the image.

<!-- When you click on the image, the projection is applied. -->
<Image MouseLeftButtonDown="ApplyProjection" x:Name="BeachImage" Source="guy_by_the_beach.jpg"
       Width="200"/>

You can also apply a Matrix3DProjection to an object by using XAML. The following example shows how to apply the same transform as in the previous example but uses XAML instead of procedural code:

<Image Source="guy_by_the_beach.jpg" Width="200">
    <Image.Projection>
        <Matrix3DProjection  ProjectionMatrix="1, 0, 0, 0,
                                              0, 1, 0, 0,
                                              0, 0, 1, 0,
                                              100, 100, 0, 1"/>
    </Image.Projection>
</Image>

You can multiply matrices together to create more complex effects. The following example uses several Matrix3D matrices that apply a 3-D transform to an image when you tap the image.

<!-- When you click on the image, the projection is applied. -->
<Image MouseLeftButtonDown="ApplyProjection" x:Name="BeachImage" Source="guy_by_the_beach.jpg" 
       Width="200"/>
private void ApplyProjection(Object sender, MouseButtonEventArgs e)
{
    // Translate the image along the negative Z-axis such that it occupies 50% of the
    // vertical field of view.
    double fovY = Math.PI / 2.0;
    double translationZ = -BeachImage.ActualHeight / Math.Tan(fovY / 2.0);
    double theta = 20.0 * Math.PI / 180.0;

    // You can create a 3D effect by creating a number of simple 
    // tranformation Matrix3D matrixes and then multiply them together.
    Matrix3D centerImageAtOrigin = TranslationTransform(
             -BeachImage.ActualWidth / 2.0,
             -BeachImage.ActualHeight / 2.0, 0);
    Matrix3D invertYAxis = CreateScaleTransform(1.0, -1.0, 1.0);
    Matrix3D rotateAboutY = RotateYTransform(theta);
    Matrix3D translateAwayFromCamera = TranslationTransform(0, 0, translationZ);
    Matrix3D perspective = PerspectiveTransformFovRH(fovY,
            LayoutRoot.ActualWidth / LayoutRoot.ActualHeight,   // aspect ratio
            1.0,                                                // near plane
            1000.0);                                            // far plane
    Matrix3D viewport = ViewportTransform(LayoutRoot.ActualWidth, LayoutRoot.ActualHeight);

    Matrix3D m = centerImageAtOrigin * invertYAxis;
    m = m * rotateAboutY;
    m = m * translateAwayFromCamera;
    m = m * perspective;
    m = m * viewport;

    Matrix3DProjection m3dProjection = new Matrix3DProjection();
    m3dProjection.ProjectionMatrix = m;

    BeachImage.Projection = m3dProjection;
}

private Matrix3D TranslationTransform(double tx, double ty, double tz)
{
    Matrix3D m = new Matrix3D();

    m.M11 = 1.0; m.M12 = 0.0; m.M13 = 0.0; m.M14 = 0.0;
    m.M21 = 0.0; m.M22 = 1.0; m.M23 = 0.0; m.M24 = 0.0;
    m.M31 = 0.0; m.M32 = 0.0; m.M33 = 1.0; m.M34 = 0.0;
    m.OffsetX = tx; m.OffsetY = ty; m.OffsetZ = tz; m.M44 = 1.0;

    return m;
}

private Matrix3D CreateScaleTransform(double sx, double sy, double sz)
{
    Matrix3D m = new Matrix3D();

    m.M11 = sx; m.M12 = 0.0; m.M13 = 0.0; m.M14 = 0.0;
    m.M21 = 0.0; m.M22 = sy; m.M23 = 0.0; m.M24 = 0.0;
    m.M31 = 0.0; m.M32 = 0.0; m.M33 = sz; m.M34 = 0.0;
    m.OffsetX = 0.0; m.OffsetY = 0.0; m.OffsetZ = 0.0; m.M44 = 1.0;

    return m;
}

private Matrix3D RotateYTransform(double theta)
{
    double sin = Math.Sin(theta);
    double cos = Math.Cos(theta);

    Matrix3D m = new Matrix3D();

    m.M11 = cos; m.M12 = 0.0; m.M13 = -sin; m.M14 = 0.0;
    m.M21 = 0.0; m.M22 = 1.0; m.M23 = 0.0; m.M24 = 0.0;
    m.M31 = sin; m.M32 = 0.0; m.M33 = cos; m.M34 = 0.0;
    m.OffsetX = 0.0; m.OffsetY = 0.0; m.OffsetZ = 0.0; m.M44 = 1.0;

    return m;
}

private Matrix3D RotateZTransform(double theta)
{
    double cos = Math.Cos(theta);
    double sin = Math.Sin(theta);

    Matrix3D m = new Matrix3D();
    m.M11 = cos; m.M12 = sin; m.M13 = 0.0; m.M14 = 0.0;
    m.M21 = -sin; m.M22 = cos; m.M23 = 0.0; m.M24 = 0.0;
    m.M31 = 0.0; m.M32 = 0.0; m.M33 = 1.0; m.M34 = 0.0;
    m.OffsetX = 0.0; m.OffsetY = 0.0; m.OffsetZ = 0.0; m.M44 = 1.0;
    return m;
}

private Matrix3D PerspectiveTransformFovRH(double fieldOfViewY, double aspectRatio, double zNearPlane, double zFarPlane)
{
    double height = 1.0 / Math.Tan(fieldOfViewY / 2.0);
    double width = height / aspectRatio;
    double d = zNearPlane - zFarPlane;

    Matrix3D m = new Matrix3D();
    m.M11 = width; m.M12 = 0; m.M13 = 0; m.M14 = 0;
    m.M21 = 0; m.M22 = height; m.M23 = 0; m.M24 = 0;
    m.M31 = 0; m.M32 = 0; m.M33 = zFarPlane / d; m.M34 = -1;
    m.OffsetX = 0; m.OffsetY = 0; m.OffsetZ = zNearPlane * zFarPlane / d; m.M44 = 0;

    return m;
}

private Matrix3D ViewportTransform(double width, double height)
{
    Matrix3D m = new Matrix3D();

    m.M11 = width / 2.0; m.M12 = 0.0; m.M13 = 0.0; m.M14 = 0.0;
    m.M21 = 0.0; m.M22 = -height / 2.0; m.M23 = 0.0; m.M24 = 0.0;
    m.M31 = 0.0; m.M32 = 0.0; m.M33 = 1.0; m.M34 = 0.0;
    m.OffsetX = width / 2.0; m.OffsetY = height / 2.0; m.OffsetZ = 0.0; m.M44 = 1.0;

    return m;
}

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Windows.Media Namespace