Matrix3D 結構

定義

表示 4 × 4 矩陣,用於 3D 空間中的轉換。 當做 Matrix3DProjection.ProjectionMatrix的值使用。

public value class Matrix3D
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
struct Matrix3D
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 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
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

本範例會使用簡單的 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 類型更複雜的半-3D 案例。 Matrix3DProjection 提供完整的 3D 轉換矩陣,以套用至任何 UIElement (您使用此矩陣作為 UIElement.Projection 屬性的值) 。 矩陣可讓您將任意模型轉換矩陣和透視矩陣套用至視覺元素。

Matrix3D 具有此資料列向量語法:

M11M12M13M14
M21M22M23M24
M31M32M33M34
OffsetXOffsetYOffsetZM44

由於第四個數據行是可存取的,因此 Matrix3D 可以同時代表 affine 和非相依性轉換。

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 命名空間的程式碼命名空間集中。 若要使用 Matrix3D 做為 XAML 中的元素,您必須在 XAML 中包含 xmlns 宣告,以參考 ** using:** 語句的 Windows.UI.Xaml.Media.Media3D 。 然後使用您針對Windows.UI.Xaml.Media.Media3D中的類型對應的xmlns前置詞來限定 Matrix3D。
  • 即使您執行此對應,Matrix3D 物件元素也無法有屬性值來設定 16 個屬性,但 XAML 剖析器並未啟用, (其他 XAML 結構有屬性即屬性語法的特殊大小寫處理;Matrix3D 發生此) 。 您仍然需要使用初始化文字,將 16 個值設定為字串的連續 Atom。 在此情況下,字串會包含為 Matrix3D 物件專案的「內部文字」/內容。
  • 如您所見,物件元素語法比 Matrix3DProjection.ProjectionMatrix的內嵌屬性語法更容易讀取或使用,因此,Verbose Matrix3D 物件元素語法並不常見。

Matrix3D 的投影和成員

如果您使用 Microsoft .NET 語言 (C# 或 Microsoft Visual Basic) ,或在 Visual C++ 元件延伸模組中 (C++/CX) ,則 Matrix3D 有可用的非資料成員,且其資料成員會公開為讀寫屬性,而非欄位。

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

您無法在 XAML 中使用個別 XAML 屬性設定 Matrix3D 的屬性。 您必須使用指定所有 16 個值的初始化字串來初始化 Matrix3D 物件專案,或使用使用這個相同字串格式 之 Matrix3DProjection.ProjectionMatrix 的屬性語法。

欄位

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 的第四列和第三欄的值。

適用於

另請參閱