Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET_WORLD_COORD_FROM_SCREEN_COORD for RedM #2370

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions code/components/extra-natives-five/include/GamePrimitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ struct fwSearchVolume

struct grcViewport
{
float m_mat1[16];
float m_mat2[16];
float m_viewProjection[16];
float m_world[16];
float m_worldView[16];
float m_worldViewProj[16];
float m_inverseView[16];
char m_pad[64];
float m_view[16];
float m_projection[16];
};

Expand Down Expand Up @@ -146,7 +146,7 @@ inline rage::Vec3V Unproject(const rage::grcViewport& viewport, const rage::Vec3
{
using namespace DirectX;

auto invVP = XMMatrixInverse(NULL, XMLoadFloat4x4((const XMFLOAT4X4*)viewport.m_viewProjection));
auto invVP = XMMatrixInverse(NULL, XMLoadFloat4x4((const XMFLOAT4X4*)viewport.m_worldViewProj));
auto inVec = XMVectorSet((viewPos.x * 2.0f) - 1.0f, ((1.0 - viewPos.y) * 2.0f) - 1.0f, viewPos.z, 1.0f);
auto outCoord = XMVector3TransformCoord(inVec, invVP);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void gz::Draw(const RemoteDrawLists* lists)
SetBlendState(GetStockStateIdentifier(BlendStateDefault));

shader->SetParameter(uViewport, viewportSize, 8, 1);
shader->SetParameter(uViewProjMatrix, viewport.m_viewProjection, 16, 4);
shader->SetParameter(uViewProjMatrix, viewport.m_worldViewProj, 16, 4);

rage::grcDrawMode mode;

Expand Down
40 changes: 40 additions & 0 deletions code/components/extra-natives-rdr3/include/GamePrimitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,44 @@ struct spdRay
Vec3V start;
Vec3V end;
};

struct grcViewport
{
float m_world[16];
float m_worldView[16];
float m_projection[16];
float m_inverseView[16];
float m_unknown[16];
float m_view[16];
};
}

struct CViewportGame
{
public:
virtual ~CViewportGame() = 0;

private:
char m_pad[8];

public:
rage::grcViewport viewport;
};

extern CViewportGame** g_viewportGame;

inline rage::Vec3V Unproject(const rage::grcViewport& viewport, const rage::Vec3V& viewPos)
{
using namespace DirectX;

auto composite = XMMatrixMultiply(XMLoadFloat4x4((const XMFLOAT4X4*)&viewport.m_worldView), XMLoadFloat4x4((const XMFLOAT4X4*)&viewport.m_projection));
auto invVP = XMMatrixInverse(NULL, composite);
auto inVec = XMVectorSet((viewPos.x * 2.0f) - 1.0f, ((1.0 - viewPos.y) * 2.0f) - 1.0f, viewPos.z, 1.0f);
auto outCoord = XMVector3TransformCoord(inVec, invVP);

return {
XMVectorGetX(outCoord),
XMVectorGetY(outCoord),
XMVectorGetZ(outCoord)
};
}
29 changes: 29 additions & 0 deletions code/components/extra-natives-rdr3/src/GraphicsNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "CfxRGBA.h"
#include "Hooking.Stubs.h"
#include "GamePrimitives.h"
#include "scrEngine.h"

enum ScriptImDrawType : uint32_t
{
Expand Down Expand Up @@ -104,13 +105,16 @@ struct WorldhorizonManager

static WorldhorizonManager* g_worldhorizonMgr;

CViewportGame** g_viewportGame;

static HookFunction hookFunction([]()
{
static_assert(sizeof(ScriptImRequest) == 64);
static_assert(sizeof(DrawOriginData) == 32);
static_assert(sizeof(DrawOriginStore) == 1040);

{
g_viewportGame = hook::get_address<CViewportGame**>(hook::get_pattern("0F 2F F0 76 ? 4C 8B 35", 8));
g_worldhorizonMgr = hook::get_address<WorldhorizonManager*>(hook::get_pattern("89 44 24 40 48 8D 0D ? ? ? ? 8B 84 24 90 00", 7));
}

Expand Down Expand Up @@ -286,4 +290,29 @@ static HookFunction hookFunction([]()
g_worldhorizonMgr->m_disableRendering = flag;
}
});

fx::ScriptEngine::RegisterNativeHandler("GET_WORLD_COORD_FROM_SCREEN_COORD", [](fx::ScriptContext& context)
{
float screenX = context.GetArgument<float>(0);
float screenY = context.GetArgument<float>(1);

using namespace DirectX;
rage::Vec3V start = Unproject((*g_viewportGame)->viewport, rage::Vec3V{ screenX, screenY, 0.0f });
rage::Vec3V end = Unproject((*g_viewportGame)->viewport, rage::Vec3V{ screenX, screenY, 1.0f });

auto startVector = XMLoadFloat3((XMFLOAT3*)&start);
auto endVector = XMLoadFloat3((XMFLOAT3*)&end);
auto normalVector = XMVector3Normalize(XMVectorSubtract(endVector, startVector));

scrVector* worldOut = context.GetArgument<scrVector*>(2);
scrVector* normalOut = context.GetArgument<scrVector*>(3);

worldOut->x = start.x;
worldOut->y = start.y;
worldOut->z = start.z;

normalOut->x = XMVectorGetX(normalVector);
normalOut->y = XMVectorGetY(normalVector);
normalOut->z = XMVectorGetZ(normalVector);
});
});
Loading