語言

Matrix3D 結構

定義

表示一個用於三維空間變換的4×4矩陣。 用作 Matrix3DProjection.ProjectionMatrix 的值。

public value class Matrix3D
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
struct Matrix3D
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
public struct Matrix3D
Public Structure Matrix3D
<Matrix3DProjection ProjectionMatrix="m11, m12, m13, m14, 
  m21, m22, m23, m24, m31, m32, m33, m34, offsetX, offsetY, offsetZ, m44" />
- or -
<!--xmlns:m3d="using:Windows.UI.Xaml.Media.Media3D"-->
<m3d:Matrix3D>
m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, offsetX, offsetY, offsetZ, m44
</m3d:Matrix3D>
繼承
Matrix3D
屬性

範例

這個範例使用簡單的 Matrix3D 矩陣,點擊圖片時會將圖片從 X 和 Y 方向轉換。

<!-- When you click on the image, the projection is applied. -->
<Image PointerPressed="ApplyProjection" x:Name="BeachImage" Source="guy_by_the_beach.jpg"
       Width="200"/>
private void ApplyProjection(Object sender, PointerRoutedEventArgs 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;

}
<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>
<!-- When you click on the image, the projection is applied. -->
<Image PointerPressed="ApplyProjection" x:Name="BeachImage" Source="guy_by_the_beach.jpg" 
       Width="200"/>
private void ApplyProjection(Object sender, PointerRoutedEventArgs 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 matrices 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 = Matrix3DHelper.Multiply(centerImageAtOrigin,invertYAxis);
    m = Matrix3D.Multiply(m ,rotateAboutY);
    m = Matrix3D.Multiply(m,translateAwayFromCamera);
    m = Matrix3D.Multiply(m,perspective);
    m = Matrix3D.Multiply(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;
}

備註

你可以使用 Matrix3DProjection 和 Matrix3D 類型來處理比 PlaneProjection 類型更複雜的半立體場景。 Matrix3DProjection 提供完整的三維轉換矩陣,可套用到任何 UIElement (你可以用這個作為 UIElement.Projection 屬性的值)。 矩陣允許你對視覺元素套用任意模型轉換矩陣和透視矩陣。

Matrix3D 採用以下列向量語法:

M11M12M13M14
M21M22M23M24
M31M32M33M34
OffsetXOffsetY偏移ZM44

由於第四欄是可存取的,Matrix3D 可以表示仿射與非仿射轉換。

Matrix3D 的 XAML 語法

Matrix3D 值可以在 XAML 中宣告,但語法有限,且與你根據其他 Windows 執行階段 結構(如 Thickness)對 XAML UI 的支援不同;Matrix3D 類型屬性最常見的用途是依賴 Matrix3D 類型內建的初始化字串行為,並設定任何使用 Matrix3D 值作為屬性的值。 你指定一個「初始化文字」格式的字串來構建 Matrix3D 值:16 個獨立的 Double 值,每個用逗號或空格分隔。 你可以在下方的「範例」中看到 XAML 中使用的這個格式。

  • 目前只有一個屬性使用 Matrix3D 值: Matrix3DProjection.ProjectionMatrix。 這就是這裡顯示的主要 XAML 語法。
  • 所示的次要 XAML 語法包含一個實際的 Matrix3D 物件元素。 但請注意它有 XAML 命名空間的前綴。 Windows。UI。Xaml.Media.Media3D 命名空間並未包含在 Windows 執行階段 XAML 解析器用於預設 XAML 命名空間的代碼命名空間集合中。 要在 XAML 中使用 Matrix3D 作為元素,你必須在 XAML 中加入一個引用 Windows 的 xmlns 宣告。UI。Xaml.Media.Media3D 以 ** 使用:** 陳述句。 然後用你在 Windows 中為類型映射的 xmlns 前綴來限定 Matrix3D。UI。Xaml.Media.Media3D。
  • 即使你做了這個映射,Matrix3D 物件元素也無法設定 16 個屬性的屬性值,XAML 解析器並沒有啟用這個功能(其他 XAML 結構有特殊處理屬性即屬性語法;Matrix3D 恰巧沒有這個功能)。 你仍然必須使用初始化文字,將 16 個值設定為字串的連續原子。 此時字串包含為 Matrix3D 物件元素的「內部文字」/內容。
  • 如你所見,物件元素的語法並不比 Matrix3DProjection.ProjectionMatrix 的內嵌屬性語法更易閱讀或使用,所以冗長的 Matrix3D 物件元素語法並不常見。

Matrix3D 的投影與成員

如果你使用的是Microsoft .NET語言(C#),那麼 Matrix3D 有非資料成員可用,且其資料成員是以讀寫屬性(card-write property)形式暴露,而非欄位。

如果你使用 Windows 執行階段 範本函式庫(WRL)使用 C++ 程式設計,則只有資料成員欄位會作為 Matrix3D 的成員存在,且你無法使用成員表中列出的工具方法或屬性。 WRL 程式碼可存取 Matrix3DHelper 類別中類似的工具方法。

你無法用 XAML 設定 Matrix3D 的屬性,並擁有個別的 XAML 屬性。 你必須用一個初始化字串初始化 Matrix3D 的物件元素,該字串指定所有 16 個值,或者使用 Matrix3DProjection.ProjectionMatrix 的屬性語法,該字串格式相同。

欄位

名稱 Description
M11

這個 Matrix3D 的第一列第一欄的值。

M12

這是 Matrix3D 第一列第二欄的值。

M13

這是這個 Matrix3D 的第一列第三欄的值。

M14

這是 Matrix3D 第一列第四欄的值。

M21

這是 Matrix3D 第二列第一欄的值。

M22

這個 Matrix3D 的第二列第二欄的值。

M23

這個 Matrix3D 的第二列第三欄的值。

M24

這是 Matrix3D 第二列第四欄的值。

M31

這是 Matrix3D 第三列第一欄的值。

M32

這是 Matrix3D 第三列第二欄的值。

M33

這是這個 Matrix3D 第三列、第三列的數值。

M34

這個 Matrix3D 第三行第四欄的值。

M44

這個 Matrix3D 第四列、第四欄的值。

OffsetX

這個 Matrix3D 第四列第一欄的值。

OffsetY

這是這個 Matrix3D 第四列第二欄的值。

OffsetZ

這個 Matrix3D 第四列第三欄的值。

適用於

另請參閱