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.
Assembly: System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.
The Matrix3D type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | HasInverse | Gets a value that indicates whether this Matrix3D is invertible. |
![]() ![]() | Identity | Changes a Matrix3D structure into an identity Matrix3D. |
![]() | IsIdentity | Determines whether this Matrix3D structure is an identity Matrix3D. |
![]() | M11 | Gets or sets the value of the first row and first column of this Matrix3D. |
![]() | M12 | Gets or sets the value of the first row and second column of this Matrix3D. |
![]() | M13 | Gets or sets the value of the first row and third column of this Matrix3D. |
![]() | M14 | Gets or sets the value of the first row and fourth column of this Matrix3D. |
![]() | M21 | Gets or sets the value of the second row and first column of this Matrix3D. |
![]() | M22 | Gets or sets the value of the second row and second column of this Matrix3D. |
![]() | M23 | Gets or sets the value of the second row and third column of this Matrix3D. |
![]() | M24 | Gets or sets the value of the second row and fourth column of this Matrix3D. |
![]() | M31 | Gets or sets the value of the third row and first column of this Matrix3D. |
![]() | M32 | Gets or sets the value of the third row and second column of this Matrix3D. |
![]() | M33 | Gets or sets the value of the third row and third column of this Matrix3D. |
![]() | M34 | Gets or sets the value of the third row and fourth column of this Matrix3D. |
![]() | M44 | Gets or sets the value of the fourth row and fourth column of this Matrix3D. |
![]() | OffsetX | Gets or sets the value of the fourth row and first column of this Matrix3D. |
![]() | OffsetY | Gets or sets the value of the fourth row and second column of this Matrix3D. |
![]() | OffsetZ | Gets or sets the value of the fourth row and third column of this Matrix3D. |
| Name | Description | |
|---|---|---|
![]() | Equals(Matrix3D) | Tests equality between two matrices. |
![]() | Equals(Object) | Tests equality between two matrices. (Overrides ValueType::Equals(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.) |
![]() | GetHashCode | Returns the hash code for this matrix. (Overrides ValueType::GetHashCode().) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | Invert | Inverts this Matrix3D structure. |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString() | Creates a string representation of this Matrix3D. (Overrides ValueType::ToString().) |
![]() | ToString(IFormatProvider) | Creates a string representation of this Matrix3D. |
| Name | Description | |
|---|---|---|
![]() ![]() | Equality | Compares two Matrix3D instances for exact equality. |
![]() ![]() | Inequality | Compares two Matrix3D instances for inequality. |
![]() ![]() | Multiply | Multiplies the specified matrices. |
| Name | Description | |
|---|---|---|
![]() ![]() | IFormattable::ToString | Infrastructure. Formats the value of the current instance using the specified format. |
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
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; }






