|
| 1 | +#include <Event.hpp> |
| 2 | +#include <Variable.hpp> |
| 3 | +#include <Modules/Client.hpp> |
| 4 | +#include <Modules/Engine.hpp> |
| 5 | +#include <Features/Renderer.hpp> |
| 6 | + |
| 7 | +Variable sar_demo_clean_start("sar_demo_clean_start", "0", 0, |
| 8 | + "Attempts to minimize visual interpolation of some elements (like post-processing or lighting) when demo playback begins.\n", 0); |
| 9 | +Variable sar_demo_clean_start_tonemap("sar_demo_clean_start_tonemap", "0", 0, |
| 10 | + "Overrides initial tonemap scalar value used in auto-exposure." |
| 11 | + "Setting it to 0 will attempt to skip over to target value for several ticks.\n", 0); |
| 12 | + |
| 13 | + |
| 14 | +bool g_tonemapScaleDemoLookupInProgress = false; |
| 15 | +int g_tonemapScaleDemoLookupTick; |
| 16 | +bool g_tonemapScaleContinouosSearchEnabled = false; |
| 17 | +int g_tonemapScaleTweakStartTick = 0; |
| 18 | + |
| 19 | +const float TONEMAP_SCALE_SEARCH_DISABLE_THRESHOLD = 0.01f; |
| 20 | +const int TONEMAP_SCALE_SEARCH_MAX_NOCHANGE_TICKS = 30; |
| 21 | + |
| 22 | +void startTonemapScaleDemoLookup(int tick) { |
| 23 | + g_tonemapScaleDemoLookupInProgress = true; |
| 24 | + g_tonemapScaleDemoLookupTick = tick; |
| 25 | +} |
| 26 | + |
| 27 | +void stopTonemapScaleDemoLookup(bool finished = false) { |
| 28 | + if (!g_tonemapScaleDemoLookupInProgress) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + if (!finished) { |
| 33 | + console->Print("Failed to sample tonemap scale before the end of the demo.\n"); |
| 34 | + } |
| 35 | + g_tonemapScaleDemoLookupInProgress = false; |
| 36 | + g_tonemapScaleDemoLookupTick = -1; |
| 37 | +} |
| 38 | + |
| 39 | +void updateTonemapScaleDemoLookup() { |
| 40 | + if (!g_tonemapScaleDemoLookupInProgress) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (!engine->demoplayer->IsPlaying() || engine->demoplayer->GetTick() < g_tonemapScaleDemoLookupTick) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + float tonemapScale = client->GetCurrentTonemappingSystem()->m_flCurrentTonemapScale; |
| 49 | + sar_demo_clean_start_tonemap.SetValue(tonemapScale); |
| 50 | + console->Print("Sampled tonemap scale value %.6f and stored it in \"sar_demo_clean_start_tonemap\" variable.\n", tonemapScale); |
| 51 | + |
| 52 | + stopTonemapScaleDemoLookup(true); |
| 53 | +} |
| 54 | + |
| 55 | +void triggerTonemapScaleTweak() { |
| 56 | + float tonemapScaleOverride = sar_demo_clean_start_tonemap.GetFloat(); |
| 57 | + |
| 58 | + if (tonemapScaleOverride == 0.0f) { |
| 59 | + g_tonemapScaleContinouosSearchEnabled = true; |
| 60 | + auto tonemapSystem = client->GetCurrentTonemappingSystem(); |
| 61 | + g_tonemapScaleTweakStartTick = tonemapSystem->m_nCurrentQueryFrame; |
| 62 | + } |
| 63 | + |
| 64 | + client->ResetToneMapping(tonemapScaleOverride); |
| 65 | +} |
| 66 | + |
| 67 | +void disableTonemapScaleContinouosSearch() { |
| 68 | + g_tonemapScaleContinouosSearchEnabled = false; |
| 69 | +} |
| 70 | + |
| 71 | +void updateTonemapScaleContinouosSearch() { |
| 72 | + if (!g_tonemapScaleContinouosSearchEnabled) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + auto tonemapSystem = client->GetCurrentTonemappingSystem(); |
| 77 | + if (tonemapSystem == nullptr) { |
| 78 | + disableTonemapScaleContinouosSearch(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + float diff = tonemapSystem->m_flTargetTonemapScale - tonemapSystem->m_flCurrentTonemapScale; |
| 83 | + |
| 84 | + if (fabsf(diff) > TONEMAP_SCALE_SEARCH_DISABLE_THRESHOLD) { |
| 85 | + // last sampled luminance was different enough to change target scale, reset it |
| 86 | + tonemapSystem->m_flCurrentTonemapScale = tonemapSystem->m_flTargetTonemapScale; |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + int searchDuration = tonemapSystem->m_nCurrentQueryFrame - g_tonemapScaleTweakStartTick; |
| 91 | + if (searchDuration > TONEMAP_SCALE_SEARCH_MAX_NOCHANGE_TICKS) { |
| 92 | + disableTonemapScaleContinouosSearch(); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void forceProjectedTexturesTargetColor() { |
| 97 | + for (int i = 0; i < Offsets::NUM_ENT_ENTRIES; ++i) { |
| 98 | + auto clientEntity = client->GetEntity(i); |
| 99 | + if (!clientEntity) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + if (strcmp(clientEntity->GetClientClass()->m_pNetworkName, "CEnvProjectedTexture") != 0) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + auto targetColor = clientEntity->field<color32>("m_LightColor"); |
| 108 | + auto &m_CurrentLinearFlotLightColor = clientEntity->fieldOff<Vector>("m_LightColor", 0x04); |
| 109 | + auto &m_flCurrentLinearFloatLightAlpha = clientEntity->fieldOff<float>("m_LightColor", 0x10); |
| 110 | + |
| 111 | + m_CurrentLinearFlotLightColor = Vector(targetColor.r, targetColor.g, targetColor.b); |
| 112 | + m_flCurrentLinearFloatLightAlpha = targetColor.a; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +CON_COMMAND(sar_demo_clean_start_tonemap_sample, |
| 117 | + "sar_demo_clean_start_tonemap_sample [tick] - samples tonemap scale from current demo " |
| 118 | + "at given tick and stores it in \"sar_demo_clean_start_tonemap\" variable. " |
| 119 | + "If no tick is given, sampling will happen when __END__ is seen in demo playback.") { |
| 120 | + if (args.ArgC() > 2) { |
| 121 | + return console->Print(sar_demo_clean_start_tonemap_sample.ThisPtr()->m_pszHelpString); |
| 122 | + } |
| 123 | + |
| 124 | + if (!engine->demoplayer->IsPlaying()) { |
| 125 | + return console->Print("No demo is currently being played back.\n"); |
| 126 | + } |
| 127 | + |
| 128 | + g_tonemapScaleDemoLookupInProgress = true; |
| 129 | + |
| 130 | + if (args.ArgC() == 2) { |
| 131 | + startTonemapScaleDemoLookup(std::atoi(args[1])); |
| 132 | + return console->Print("Tonemap sampling set up to trigger at tick %d\n", g_tonemapScaleDemoLookupTick); |
| 133 | + } else { |
| 134 | + startTonemapScaleDemoLookup(Renderer::segmentEndTick); |
| 135 | + return console->Print("Tonemap sampling set up to trigger at __END__ tick (%d)\n", g_tonemapScaleDemoLookupTick); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | +ON_EVENT(FRAME) { |
| 141 | + updateTonemapScaleContinouosSearch(); |
| 142 | + updateTonemapScaleDemoLookup(); |
| 143 | +} |
| 144 | + |
| 145 | +ON_EVENT(DEMO_START) { |
| 146 | + if (!sar_demo_clean_start.GetBool()) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + triggerTonemapScaleTweak(); |
| 151 | + forceProjectedTexturesTargetColor(); |
| 152 | +} |
| 153 | + |
| 154 | +ON_EVENT(DEMO_STOP) { |
| 155 | + disableTonemapScaleContinouosSearch(); |
| 156 | + stopTonemapScaleDemoLookup(); |
| 157 | +} |
0 commit comments