From c1e08f2beaf35911b2be5f0180270a1159ba7824 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sun, 5 Jan 2025 21:40:38 +0300 Subject: [PATCH] Replace camera aspect ratio/field of view logic. --- UnleashedRecomp/patches/camera_patches.cpp | 45 ++++++++++------------ UnleashedRecompLib/config/SWA.toml | 14 +++---- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/UnleashedRecomp/patches/camera_patches.cpp b/UnleashedRecomp/patches/camera_patches.cpp index 17cdf427..cea316e1 100644 --- a/UnleashedRecomp/patches/camera_patches.cpp +++ b/UnleashedRecomp/patches/camera_patches.cpp @@ -2,40 +2,37 @@ #include #include -constexpr float m_baseAspectRatio = 16.0f / 9.0f; +static constexpr float ORIGINAL_ASPECT_RATIO = 4.0f / 3.0f; +static constexpr float ORIGINAL_WIDESCREEN_ASPECT_RATIO = 16.0f / 9.0f; -bool CameraAspectRatioMidAsmHook(PPCRegister& r31) +void CameraAspectRatioMidAsmHook(PPCRegister& r30, PPCRegister& r31) { - auto pCamera = (SWA::CCamera*)g_memory.Translate(r31.u32); - auto newAspectRatio = (float)GameWindow::s_width / (float)GameWindow::s_height; + r30.u32 = 0; + + auto camera = (SWA::CCamera*)g_memory.Translate(r31.u32); // Dynamically adjust horizontal aspect ratio to window dimensions. - pCamera->m_HorzAspectRatio = newAspectRatio; - - if (auto s_pVertAspectRatio = (be*)g_memory.Translate(0x82028FE0)) - { - // Dynamically adjust vertical aspect ratio for VERT+. - *s_pVertAspectRatio = 2.0f * atan(tan(45.0f / 2.0f) * (m_baseAspectRatio / newAspectRatio)); - } - - // Jump to 4:3 code for VERT+ adjustments if using a narrow aspect ratio. - return newAspectRatio < m_baseAspectRatio; + camera->m_HorzAspectRatio = float(GameWindow::s_width) / float(GameWindow::s_height); } -bool CameraBoostAspectRatioMidAsmHook(PPCRegister& r31, PPCRegister& f0, PPCRegister& f10, PPCRegister& f12) +void CameraFieldOfViewMidAsmHook(PPCRegister& r31, PPCRegister& f31) { - auto pCamera = (SWA::CCamera*)g_memory.Translate(r31.u32); + auto camera = (SWA::CCamera*)g_memory.Translate(r31.u32); - if (GameWindow::s_width < GameWindow::s_height) + // Interpolate to the original 4:3 field of view for narrow aspect ratios. + if (camera->m_HorzAspectRatio < ORIGINAL_WIDESCREEN_ASPECT_RATIO) { - pCamera->m_VertFieldOfView = pCamera->m_HorzFieldOfView + f10.f64; - } - else - { - pCamera->m_VertFieldOfView = (f12.f64 / f0.f64) + f10.f64; - } + float factor = std::clamp((camera->m_HorzAspectRatio - ORIGINAL_ASPECT_RATIO) / (ORIGINAL_WIDESCREEN_ASPECT_RATIO - ORIGINAL_ASPECT_RATIO), 0.0f, 1.0f); + factor = ORIGINAL_ASPECT_RATIO + (1.0f - ORIGINAL_ASPECT_RATIO) * factor; - return true; + f31.f64 *= factor; + + // For tall aspect ratios, use proper VERT+. + if (camera->m_HorzAspectRatio < ORIGINAL_ASPECT_RATIO) + { + f31.f64 = 2.0 * atan(tan(0.5 * f31.f64) / camera->m_HorzAspectRatio * ORIGINAL_ASPECT_RATIO); + } + } } PPC_FUNC_IMPL(__imp__sub_824697B0); diff --git a/UnleashedRecompLib/config/SWA.toml b/UnleashedRecompLib/config/SWA.toml index 2a7630dc..d9fb3b7c 100644 --- a/UnleashedRecompLib/config/SWA.toml +++ b/UnleashedRecompLib/config/SWA.toml @@ -91,16 +91,14 @@ registers = ["r11"] [[midasm_hook]] name = "CameraAspectRatioMidAsmHook" -address = 0x82468E84 -registers = ["r31"] -jump_address_on_true = 0x82468E88 -jump_address_on_false = 0x82468EE0 +address = 0x82468E78 +registers = ["r30", "r31"] [[midasm_hook]] -name = "CameraBoostAspectRatioMidAsmHook" -address = 0x8246BDA0 -registers = ["r31", "f0", "f10", "f12"] -jump_address_on_true = 0x8246BDAC +name = "CameraFieldOfViewMidAsmHook" +address = 0x82468EDC +registers = ["r31", "f31"] +jump_address = 0x82468EE0 [[midasm_hook]] name = "ResetScoreOnRestartMidAsmHook"