Matrix3D Structure
Collapse the table of content
Expand the table of content

Matrix3D Structure

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

Represents a 4 × 4 matrix that is used for transformations in a three-dimensional (3-D) space.

Namespace:  System.Windows.Media.Media3D
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

No code example is currently available or this language may not be supported.
<object property="m11,m12,m13,m14,m21,m22,m23,m24,m31,m32,m33,m34,offsetX,offsetY,offsetZ,m44"/>
- or -
<object property="Identity"/>

The Matrix3D type exposes the following members.

  NameDescription
Public methodMatrix3DInitializes a new instance of the Matrix3D class.
Top

  NameDescription
Public propertyHasInverseGets a value that indicates whether this Matrix3D is invertible.
Public propertyStatic memberIdentityChanges a Matrix3D structure into an identity Matrix3D.
Public propertyIsIdentityDetermines whether this Matrix3D structure is an identity Matrix3D.
Public propertyM11Gets or sets the value of the first row and first column of this Matrix3D.
Public propertyM12Gets or sets the value of the first row and second column of this Matrix3D.
Public propertyM13Gets or sets the value of the first row and third column of this Matrix3D.
Public propertyM14Gets or sets the value of the first row and fourth column of this Matrix3D.
Public propertyM21Gets or sets the value of the second row and first column of this Matrix3D.
Public propertyM22Gets or sets the value of the second row and second column of this Matrix3D.
Public propertyM23Gets or sets the value of the second row and third column of this Matrix3D.
Public propertyM24Gets or sets the value of the second row and fourth column of this Matrix3D.
Public propertyM31Gets or sets the value of the third row and first column of this Matrix3D.
Public propertyM32Gets or sets the value of the third row and second column of this Matrix3D.
Public propertyM33Gets or sets the value of the third row and third column of this Matrix3D.
Public propertyM34Gets or sets the value of the third row and fourth column of this Matrix3D.
Public propertyM44Gets or sets the value of the fourth row and fourth column of this Matrix3D.
Public propertyOffsetXGets or sets the value of the fourth row and first column of this Matrix3D.
Public propertyOffsetYGets or sets the value of the fourth row and second column of this Matrix3D.
Public propertyOffsetZGets or sets the value of the fourth row and third column of this Matrix3D.
Top

  NameDescription
Public methodEquals(Matrix3D)Tests equality between two matrices.
Public methodEquals(Object)Tests equality between two matrices. (Overrides ValueType::Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this matrix. (Overrides ValueType::GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInvertInverts this Matrix3D structure.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToString()Creates a string representation of this Matrix3D. (Overrides ValueType::ToString().)
Public methodToString(IFormatProvider)Creates a string representation of this Matrix3D.
Top

  NameDescription
Public operatorStatic memberEqualityCompares two Matrix3D instances for exact equality.
Public operatorStatic memberInequalityCompares two Matrix3D instances for inequality.
Public operatorStatic memberMultiplyMultiplies the specified matrices.
Top

  NameDescription
Explicit interface implemetationPrivate methodIFormattable::ToStringInfrastructure. Formats the value of the current instance using the specified format.
Top

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

Because these APIs are minimal, if you use them, you have 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 must use standard 3-D transformation matrices. For information about how to construct and use these 3-D matrices, see the DirectX documentation. Functions of particular note to users of Matrix3Dare the standard translate, scale, rotate, and perspective matrices.

Matrix3D has the following row-vector syntax:

Because the fourth column is also accessible, the matrix lets developers represent both affine and non-affine transforms.

XAML Values

m11

The value in the first row and first column of this Matrix3D. For more information, see the M11 property.

m12

The value in the first row and second column of this Matrix3D. For more information, see the M12 property.

m13

The value in the first row and third column of this Matrix3D. For more information, see the M13 property.

m14

The value in the first row and fourth column of this Matrix3D. For more information, see the M14 property.

m21

The value in the second row and first column of this Matrix3D. For more information, see the M21 property.

m22

The value in the second row and second column of this Matrix3D. For more information, see the M22 property.

m23

The value in the second row and third column of this Matrix3D. For more information, see the M23 property.

m24

The value in the second row and fourth column of this Matrix3D. For more information, see the M24 property.

m31

The value in the third row and first column of this Matrix3D. For more information, see the M31 property.

m32

The value in the third row and second column of this Matrix3D. For more information, see the M32 property.

m33

The value in the third row and third column of this Matrix3D. For more information, see the M33 property.

m34

The value in the third row and fourth column of this Matrix3D. For more information, see the M34 property.

m44

The value in the fourth row and fourth column of this Matrix3D. For more information, see the M44 property.

offsetX

The value in the fourth row and first column of this Matrix3D. For more information, see the OffsetX property.

offsetY

The value in the fourth row and second column of this Matrix3D. For more information, see the OffsetY property.

offsetZ

The value in the fourth row and third column of this Matrix3D. For more information, see the OffsetZ property.

Identity

A literal value that represents the Identity matrix.

All values except the literal Identity are treated as floating-point values (these use the backing Double type in the managed APIs).

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"/>



private void ApplyProjection(Object sender, MouseButtonEventArgs e)
{
    Matrix3D m = new Matrix3D();

    // This matrix simply translates the image 100 pixels
    // down and 100 pixels right.
    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 = 100; m.OffsetY = 100; m.OffsetZ = 0; m.M44 = 1.0;

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

    BeachImage.Projection = m3dProjection;

}


You can also apply a Matrix3DProjection to an object by using XAML. The following example shows how to apply the same transform as the previous example by using 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 to apply a 3-D transform to an image so that when you tap the image, the 3-D effect is displayed.


<!-- 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;
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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

Show:
© 2017 Microsoft