Matrix3D Structure
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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)
Syntax
'Declaration
Public Structure Matrix3D _
Implements IFormattable
public struct Matrix3D : IFormattable
<Matrix3D .../>
<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.
Properties
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. |
Top
Methods
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. |
Top
Operators
Name | Description | |
---|---|---|
Equality | Compares two Matrix3D instances for exact equality. | |
Inequality | Compares two Matrix3D instances for inequality. | |
Multiply | Multiplies the specified matrices. |
Top
Explicit Interface Implementations
Name | Description | |
---|---|---|
IFormattable.ToString | Infrastructure. For a description of this member, see IFormattableToString(). |
Top
Remarks
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 Silverlight 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. For more information about how to use PlaneProjection, see 3-D Effects (Perspective Transforms).
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).
Examples
The following example uses a simple Matrix3D matrix to transform the image in the X and Y directions when you click 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">
<Image.Projection>
<Matrix3DProjection ProjectionMatrix="2, 0, 0, 0,
0, 2, 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 click 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;
}
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
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.