From 3d1cc643b2a037e7e422d16c792bb601f5819c12 Mon Sep 17 00:00:00 2001 From: QuestionableM <77170113+QuestionableM@users.noreply.github.com> Date: Thu, 14 Dec 2023 17:04:32 +0300 Subject: [PATCH] Improvements to the cycle --- Code/DynamicSun.cpp | 36 +++++++++++++++++++++++++++--------- Code/DynamicSun.hpp | 2 ++ Code/GraphicsOptionsMenu.cpp | 9 +++++++-- Code/GraphicsOptionsMenu.hpp | 7 +++---- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/Code/DynamicSun.cpp b/Code/DynamicSun.cpp index 297b8c3..bdef10f 100644 --- a/Code/DynamicSun.cpp +++ b/Code/DynamicSun.cpp @@ -22,10 +22,10 @@ inline static float lerp_float(float a, float b, float f) const float GetLightIntensity(float time_of_day) { - constexpr float c_fSunriseTime = 0.3f; + constexpr float c_fSunriseTime = 0.27f; constexpr float c_fSunsetTime = 0.75f; constexpr float c_fSunsetTimeReversed = 1.0f - c_fSunsetTime; - constexpr float c_fSunriseOffset = 0.27f; + constexpr float c_fSunriseOffset = 0.265f; constexpr float c_fSunsetOffset = 0.23f; if (time_of_day <= c_fSunriseTime) @@ -57,7 +57,7 @@ DirectX::XMVECTOR DynamicSun::GetRotationQuat(float time_of_day) else { const float v_range_start = time_of_day - 0.5f; - const float v_range_end = 1.0f - 0.77f; + const float v_range_end = 1.0f - 0.75f; const float v_distance = 1.0f - std::fmaxf((v_range_end - v_range_start) / v_range_end, 0.0f); v_lerp_factor = 0.5f + (v_distance / 2.0f); @@ -70,6 +70,17 @@ DirectX::XMVECTOR DynamicSun::GetRotationQuat(float time_of_day) return ComputeQuatFromAngle(v_rotation_radians); } +const static DirectX::FXMVECTOR v_right_dir = { 1.0f, 0.0f, 0.0f, 0.0f }; +const static DirectX::FXMVECTOR v_up_dir = { 0.0f, 1.0f, 0.0f, 0.0f }; + +void DynamicSun::UpdateLightDirection(DirectX::FXMVECTOR& dir, DirectX::FXMVECTOR& quat, float intensity) +{ + DirectX::FXMVECTOR v_shadow_dir = DirectX::XMVectorScale( + DirectX::XMVector3Normalize(DirectX::XMVector3Rotate(dir, quat)), intensity); + + DirectX::XMStoreFloat3(&DynamicSun::GetLightDirection(), v_shadow_dir); +} + __int64 DynamicSun::Update(void* device) { const float delta_time = ms_deltaTimeTimer.Update(); @@ -80,14 +91,21 @@ __int64 DynamicSun::Update(void* device) if (v_game_instance) { const float v_light_intensity = GetLightIntensity(v_game_instance->time_of_day); + DynamicSun::UpdateLightDirection(v_right_dir, GetRotationQuat(v_game_instance->time_of_day), v_light_intensity); + } + else + { + ms_fMainMenuTime = std::fmod(ms_fMainMenuTime + delta_time * 0.015625f, 1.0f); + + const float v_rot = lerp_float(0.0f, DirectX::XM_2PI, ms_fMainMenuTime); - DirectX::FXMVECTOR v_rot_quat = GetRotationQuat(v_game_instance->time_of_day); - DirectX::FXMVECTOR v_shadow_dir = DirectX::XMVectorScale( - DirectX::XMVector3Normalize( - DirectX::XMVector3Rotate({ 1.0f, 0.0f, 0.0f, 0.0f }, v_rot_quat) - ), v_light_intensity); + DirectX::FXMVECTOR v_right_rotation = DirectX::XMQuaternionRotationAxis( + { -1.0f, 0.0f, 0.0f, 0.0f }, DirectX::XMConvertToRadians(70.0f) - std::abs(ms_fSunAngle)); + DirectX::FXMVECTOR v_roll_rotation = DirectX::XMQuaternionRotationAxis( + { 0.0f, 0.0f, 1.0f, 0.0f }, v_rot); + DirectX::FXMVECTOR v_quat = DirectX::XMQuaternionMultiply(v_right_rotation, v_roll_rotation); - DirectX::XMStoreFloat3(&v_curLightDir, v_shadow_dir); + DynamicSun::UpdateLightDirection(v_up_dir, v_quat, 1.0f); } return DynamicSun::o_PresentFunction(device); diff --git a/Code/DynamicSun.hpp b/Code/DynamicSun.hpp index 5a18cab..af0ff2b 100644 --- a/Code/DynamicSun.hpp +++ b/Code/DynamicSun.hpp @@ -13,6 +13,7 @@ class DynamicSun static DirectX::XMFLOAT3& GetLightDirection(); static DirectX::XMVECTOR GetRotationQuat(float time_of_day); + static void UpdateLightDirection(DirectX::FXMVECTOR& dir, DirectX::FXMVECTOR& quat, float intensity); static __int64 Update(void* device); inline static float ms_fSunAngle = 0.32f; @@ -20,4 +21,5 @@ class DynamicSun private: inline static Timer ms_deltaTimeTimer; inline static float ms_fLightIntensity = 0.0f; + inline static float ms_fMainMenuTime = 0.5f; }; \ No newline at end of file diff --git a/Code/GraphicsOptionsMenu.cpp b/Code/GraphicsOptionsMenu.cpp index 4ca3bc4..88322ed 100644 --- a/Code/GraphicsOptionsMenu.cpp +++ b/Code/GraphicsOptionsMenu.cpp @@ -1,10 +1,10 @@ #include "GraphicsOptionsMenu.hpp" #include "OptionsItemSlider.hpp" +#include "GameInstanceData.hpp" #include "GameSettings.hpp" #include "DynamicSun.hpp" - #include "Utils/Console.hpp" #include "Utils/Memory.hpp" @@ -55,7 +55,12 @@ __int64 GraphicsOptionsMenu::h_CreateWidgets(GraphicsOptionsMenu* self) OptionsItemSlider* v_new_slider = new OptionsItemSlider( v_new_option, "Sun Angle", "SunAngle", v_min_sun_angle, v_max_sun_angle, 20, [](std::size_t value) -> void { - DynamicSun::ms_fSunAngle = *reinterpret_cast(&value); + const float v_flt_val = *reinterpret_cast(&value); + + if (GameInstanceData::GetInstance()) + DynamicSun::ms_fSunAngle = v_flt_val; + else + DynamicSun::ms_fSunAngle = std::abs(v_flt_val); } ); diff --git a/Code/GraphicsOptionsMenu.hpp b/Code/GraphicsOptionsMenu.hpp index a7a2628..a6f5d49 100644 --- a/Code/GraphicsOptionsMenu.hpp +++ b/Code/GraphicsOptionsMenu.hpp @@ -30,10 +30,9 @@ static_assert(sizeof(OptionsSubMenuBase) == 0x168, "OptionsSubMenuBase: Incorrec struct GraphicsOptionsMenu; -template -struct CustomHasher +struct VoidHasher { - inline std::size_t operator()(const T* ptr) const noexcept + inline std::size_t operator()(const void* ptr) const noexcept { return reinterpret_cast(ptr); } @@ -45,7 +44,7 @@ struct GraphicsOptionsMenu : public OptionsSubMenuBase using Destructor = void (*)(GraphicsOptionsMenu*, char); inline static std::unordered_map, CustomHasher> ms_customItems = {}; + std::vector, VoidHasher> ms_customItems = {}; /* 0x0168 */ MyGUI::Button* some_button; /* 0x0170 */ std::shared_ptr shader_quality_dropdown;