DirectXMath在错误的轴上取消投影

问题描述 投票:0回答:1

编辑:解决了这个问题,看看我自己的答案

最近我一直在研究一个3D世界编辑器,它将使用拾取来升高或降低地形。我正在使用相机无投影和光线投射来查找鼠标屏幕坐标的世界位置。

但是,似乎光线在错误的轴上。我记得,应该从非投影出来的光线应该直接来自相机。

Here是它目前的样子。

我的问题是,当它应该在Z轴上时,为什么光线会在Y轴上?

XMFLOAT3 D3D11Camera::Unproject(const float& px, const float& py, const float& pz)
{
    const XMFLOAT2& res = D3D11RenderSettings::Instance()->resolution();

    XMVECTOR coords = XMVector3Unproject(XMVectorSet(px, res.y - py, pz, 0.0f), 0.0f, 0.0f, res.x, res.y, near_plane_, far_plane_, projection_, view_, XMMatrixIdentity());

    XMFLOAT3 to_ret;
    XMStoreFloat3(&to_ret, coords);

    return to_ret;
}

这是非投影代码..

这就是我使用它的方式

projectRay: function()
{
    var p = Mouse.position(MousePosition.Relative);
    p.x = (p.x + RenderSettings.resolution().w / 2);
    p.y = (p.y + RenderSettings.resolution().h / 2);

    var unprojA = this._camera.unproject(p.x, p.y, 0);
    var unprojB = this._camera.unproject(p.x, p.y, 1);

    var dir = Vector3D.normalise(Vector3D.sub(unprojB, unprojA));
    var ray = Ray.construct(unprojA, dir);

    var p1 = ray.origin;
    var p2 = Vector3D.add(ray.origin, Vector3D.mul(ray.direction, 1000));

    RenderTargets.ui.drawLine(p1.x, p1.y, p1.z, 1, 0, 0, p2.x, p2.y, p2.z, 1, 0, 0);

    return ray;
}

干杯!

camera projection raycasting directxmath
1个回答
0
投票

除了愚蠢之外,我正在使用倒Y轴坐标系和默认 - 非倒置坐标系。它似乎是一个垂直方向的光线,但事实上它是一个朝向'/'方向的光线,而它应该被定向为'\'。将结果的Y分量乘以-1解决了该问题。

© www.soinside.com 2019 - 2024. All rights reserved.