【问题标题】:Projection matrix from Unity3D to DirectX从 Unity3D 到 DirectX 的投影矩阵
【发布时间】:2013-08-22 11:46:20
【问题描述】:

我发现 this question 可以在 OpenGL 和 DirectX 之间执行转换,但在 Unity3D 中执行相同的操作时它似乎不起作用。

我希望这里有人知道 OpenGL 投影矩阵和 Unity3D 投影矩阵(以及视图矩阵!)之间的区别。

我也尝试过使用this article,但也没有用。

在 Unity3D 中,我调用了我自己的原生 C++ 插件:

SetView(MatrixToArray(Camera.current.worldToCameraMatrix));
SetProjection(MatrixToArray(Matrix4x4.identity));
SetWorld(MatrixToArray(Camera.current.projectionMatrix));

private static float[] MatrixToArray(Matrix4x4 _matrix)
{
    float[] result = new float[16];

    for (int _row = 0; _row < 4; _row++)
    {
        for (int _col = 0; _col < 4; _col++)
        {
            result[_col + _row * 4] = _matrix[_row, _col];
        }
    }

    return result;
}

在我的 C++ 插件中,我尝试通过 the article 转换这些矩阵:

extern "C" void EXPORT_API SetProjection(float a[])
{
    g_Proj._11 = a[0];
    g_Proj._21 = a[1];
    g_Proj._31 = -a[2];
    g_Proj._41 = a[3];
    g_Proj._12 = a[4];
    g_Proj._22 = a[5];
    g_Proj._32 = -a[6];
    g_Proj._42 = a[7];
    g_Proj._13 = a[8];
    g_Proj._23 = a[9];
    g_Proj._33 = -a[10];
    g_Proj._43 = a[11];
    g_Proj._14 = a[12];
    g_Proj._24 = a[13];
    g_Proj._34 = -a[14];
    g_Proj._44 = a[15];
}

extern "C" void EXPORT_API SetView(float a[])
{
    g_View._11 = a[0];
    g_View._21 = a[1];
    g_View._31 = -a[2];
    g_View._41 = a[3];
    g_View._12 = a[4];
    g_View._22 = a[5];
    g_View._32 = -a[6];
    g_View._42 = a[7];
    g_View._13 = -a[8];
    g_View._23 = -a[9];
    g_View._33 = a[10];
    g_View._43 = -a[11];
    g_View._14 = a[12];
    g_View._24 = a[13];
    g_View._34 = a[14];
    g_View._44 = a[15];
}

我知道东西是绘制出来的,因为使用单位矩阵作为视图和投影矩阵确实给了我一个三角形的顶点:{0,0,0},{0,1,0},{1,0,0} .而且我在移动相机时也会看到各种剪切效果。

编辑 - 更多信息:
Unity3D Documentation 告诉我们它使用 OpenGL 表示法。

编辑 - 示例项目:
如果有人想试用我的代码,我创建了一个 test project 并将其上传到统一论坛。

【问题讨论】:

    标签: graphics directx unity3d projection-matrix


    【解决方案1】:

    article 似乎工作得很好,但似乎在我所有的匆忙和尝试中,我在世界矩阵中设置了投影矩阵。

    SetProjection(MatrixToArray(Matrix4x4.identity));
    SetWorld(MatrixToArray(Camera.current.projectionMatrix));
    

    应该是

    SetWorld(MatrixToArray(Matrix4x4.identity));
    SetProjection(MatrixToArray(Camera.current.projectionMatrix));
    

    我在统一论坛上发布了更多关于test project 的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2015-11-01
      • 2013-08-03
      • 1970-01-01
      • 2015-04-11
      • 2012-04-24
      • 1970-01-01
      相关资源
      最近更新 更多