简体   繁体   English

从Unity3D到DirectX的投影矩阵

[英]Projection matrix from Unity3D to DirectX

I found this question to perform a transformation between OpenGL and DirectX, but it doesn't seem to be working when performing the same things from Unity3D. 我发现这个问题是为了在OpenGL和DirectX之间进行转换,但是当从Unity3D执行相同的操作时它似乎没有用。

I was hoping that there is someone here who knows anything about the differences between an OpenGL projection matrix and the Unity3D projection matrix (and view matrix!). 我希望这里有人知道OpenGL投影矩阵和Unity3D投影矩阵(和视图矩阵!)之间的差异。

I have tried to use this article as well, but it didn't work either. 我也尝试过使用这篇文章 ,但它也没有用。

In Unity3D I'm calling to my own native C++ plugin: 在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;
}

And in my C++ plugin I try to convert these matrices via the article : 在我的C ++插件中,我尝试通过文章转换这些矩阵:

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];
}

I know things are drawn, because using the identity matrix for view and projection matrix do give me a triangle for vertices: {0,0,0},{0,1,0},{1,0,0}. 我知道绘制的东西,因为使用视图和投影矩阵的单位矩阵确实给出了顶点的三角形:{0,0,0},{0,1,0},{1,0,0}。 And I also see all sorts of shearing effects when moving my camera. 移动相机时我也会看到各种剪切效果。

EDIT - more info: 编辑 - 更多信息:
Unity3D Documentation tells us that it uses OpenGL notation. Unity3D文档告诉我们它使用OpenGL表示法。

EDIT - example project: 编辑 - 示例项目:
If anyone wants to try out my code, I've created a test project and uploaded it to the unity forum. 如果有人想尝试我的代码,我已经创建了一个测试项目并将其上传到统一论坛。

The article seems to be working just fine, but it seems that in all my rushing and trying I set the projection matrix in the world matrix. 这篇文章似乎工作得很好,但似乎在我所有的冲动和尝试中,我将投影矩阵设置在世界矩阵中。

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

should have been 本来应该

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

I've put some more information on the unity forum about the test project . 我在关于测试项目的统一论坛上提供了更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM