Skip to content

Commit

Permalink
fix: camera data fixes (#572)
Browse files Browse the repository at this point in the history
* fix: camera data fixes

* style: 🎨 apply clang-format changes

---------

Co-authored-by: doodlum <doodlum@users.noreply.github.com>
  • Loading branch information
doodlum and doodlum authored Sep 28, 2024
1 parent 46cb578 commit 9a8115f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
29 changes: 17 additions & 12 deletions src/Features/LightLimitFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

static constexpr uint CLUSTER_MAX_LIGHTS = 128;
static constexpr uint MAX_LIGHTS = 2048;
static constexpr float LIGHTS_NEAR = 0;
static constexpr float LIGHTS_FAR = 16384;

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
LightLimitFix::Settings,
Expand Down Expand Up @@ -303,8 +301,8 @@ void LightLimitFix::BSLightingShader_SetupGeometry_After(RE::BSRenderPass*)
auto& context = State::GetSingleton()->context;
auto accumulator = RE::BSGraphics::BSShaderAccumulator::GetCurrentAccumulator();

strictLightDataTemp.LightsNear = LIGHTS_NEAR;
strictLightDataTemp.LightsFar = LIGHTS_FAR;
strictLightDataTemp.LightsNear = lightsNear;
strictLightDataTemp.LightsFar = lightsFar;

static bool wasEmpty = false;
static bool wasWorld = false;
Expand Down Expand Up @@ -595,8 +593,8 @@ void LightLimitFix::AddCachedParticleLights(eastl::vector<LightData>& lightsData

light.color *= dimmer;

float distantLightFadeStart = LIGHTS_FAR * LIGHTS_FAR * (lightFadeStart / lightFadeEnd);
float distantLightFadeEnd = LIGHTS_FAR * LIGHTS_FAR;
float distantLightFadeStart = lightsFar * lightsFar * (lightFadeStart / lightFadeEnd);
float distantLightFadeEnd = lightsFar * lightsFar;

if (distance < distantLightFadeStart || distantLightFadeEnd == 0.0f) {
dimmer = 1.0f;
Expand Down Expand Up @@ -634,6 +632,12 @@ float3 LightLimitFix::Saturation(float3 color, float saturation)

void LightLimitFix::UpdateLights()
{
static float& cameraNear = (*(float*)(REL::RelocationID(517032, 403540).address() + 0x40));
static float& cameraFar = (*(float*)(REL::RelocationID(517032, 403540).address() + 0x44));

lightsNear = std::max(1.0f, cameraNear);
lightsFar = std::min(16384.0f, cameraFar);

auto shadowSceneNode = RE::BSShaderManager::State::GetSingleton().shadowSceneNode[0];

// Cache data since cameraData can become invalid in first-person
Expand Down Expand Up @@ -669,8 +673,8 @@ void LightLimitFix::UpdateLights()

float distance = CalculateLightDistance(light.positionWS[0].data, light.radius);

float distantLightFadeStart = LIGHTS_FAR * LIGHTS_FAR * (lightFadeStart / lightFadeEnd);
float distantLightFadeEnd = LIGHTS_FAR * LIGHTS_FAR;
float distantLightFadeStart = lightsFar * lightsFar * (lightFadeStart / lightFadeEnd);
float distantLightFadeEnd = lightsFar * lightsFar;

float dimmer;

Expand Down Expand Up @@ -803,16 +807,17 @@ void LightLimitFix::UpdateLights()
auto projMatrixUnjittered = Util::GetCameraData(0).projMatrixUnjittered;
float fov = atan(1.0f / static_cast<float4x4>(projMatrixUnjittered).m[0][0]) * 2.0f * (180.0f / 3.14159265359f);

static float _fov = 0.0f;
if (fabs(_fov - fov) > 1e-4) {
static float _lightsNear = 0.0f, _lightsFar = 0.0f, _fov = 0.0f;
if (fabs(_fov - fov) > 1e-4 || fabs(_lightsNear - lightsNear) > 1e-4 || fabs(_lightsFar - lightsFar) > 1e-4) {
LightBuildingCB updateData{};
updateData.InvProjMatrix[0] = DirectX::XMMatrixInverse(nullptr, projMatrixUnjittered);
if (eyeCount == 1)
updateData.InvProjMatrix[1] = updateData.InvProjMatrix[0];
else
updateData.InvProjMatrix[1] = DirectX::XMMatrixInverse(nullptr, Util::GetCameraData(1).projMatrixUnjittered);
updateData.LightsNear = LIGHTS_NEAR;
updateData.LightsFar = LIGHTS_FAR;
updateData.LightsNear = lightsNear;
updateData.LightsFar = lightsFar;
;

lightBuildingCB->Update(updateData);

Expand Down
2 changes: 2 additions & 0 deletions src/Features/LightLimitFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ struct LightLimitFix : Feature
eastl::unique_ptr<Buffer> lightGrid = nullptr;

std::uint32_t lightCount = 0;
float lightsNear = 1;
float lightsFar = 16384;

struct ParticleLightInfo
{
Expand Down
16 changes: 8 additions & 8 deletions src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,15 @@ namespace Util

float4 GetCameraData()
{
static float& cameraNear = (*(float*)(REL::RelocationID(517032, 403540).address() + 0x40));
static float& cameraFar = (*(float*)(REL::RelocationID(517032, 403540).address() + 0x44));

float4 cameraData{};
if (auto accumulator = RE::BSGraphics::BSShaderAccumulator::GetCurrentAccumulator()) {
if (accumulator->kCamera) {
cameraData.x = accumulator->kCamera->GetRuntimeData2().viewFrustum.fFar;
cameraData.y = accumulator->kCamera->GetRuntimeData2().viewFrustum.fNear;
cameraData.z = accumulator->kCamera->GetRuntimeData2().viewFrustum.fFar - accumulator->kCamera->GetRuntimeData2().viewFrustum.fNear;
cameraData.w = accumulator->kCamera->GetRuntimeData2().viewFrustum.fFar * accumulator->kCamera->GetRuntimeData2().viewFrustum.fNear;
}
}
cameraData.x = cameraFar;
cameraData.y = cameraNear;
cameraData.z = cameraFar - cameraNear;
cameraData.w = cameraFar * cameraNear;

return cameraData;
}

Expand Down

0 comments on commit 9a8115f

Please sign in to comment.