-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarbonCameraManager.cpp
More file actions
346 lines (291 loc) · 10.6 KB
/
Copy pathCarbonCameraManager.cpp
File metadata and controls
346 lines (291 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "CarbonCameraManager.h"
#include "Carbon.h"
#include "ConfigurationManager.h"
#include "TimeManager.h"
#include "CameraManager.h"
#include "minhook/include/MinHook.h"
#include <d3d9.h>
// ==========================================================
// GLOBAL STRUCTS
// ==========================================================
// Set by wrapper hook, consumed by look-at hook
static volatile LONG gApplyUG2Flag = 0;
// Set by authoritative look-at hook, consumed by final SetCameraMatrix hook.
static volatile LONG gApplyRollOnSetMatrix = 0;
LONG InterlockedExchange(
volatile LONG* Target,
LONG Value
);
// ==========================================================
// WRAPPER HOOK (NAKED, SAFE)
// ==========================================================
static void* oCameraWrapper = nullptr; // trampoline
static volatile LONG gRollArmed = 0;
static float gRollArmTime = 0.0f;
__declspec(naked) void hkCameraWrapper_Naked()
{
__asm {
pushfd
pushad
mov eax, 1
lock xchg dword ptr [gApplyUG2Flag], eax // your old flag, still ok
popad
popfd
jmp dword ptr [oCameraWrapper]
}
}
// ------------------------------------------------------------
// INIT CAMERA STATE
// ------------------------------------------------------------
static void init_camera(void* outMatrix, Vec3* from, Vec3* to, Vec3* up)
{
Vec3 fromEngine = *from;
Vec3 toEngine = *to;
Vec3 upEngine = norm(*up); // Carbon: Z-up
Vec3 fEngine = norm(sub(toEngine, fromEngine));
Vec3 rEngine = norm(cross(fEngine, upEngine)); // engine right
Vec3 uEngine = norm(cross(rEngine, fEngine)); // re-orthogonalized up
{
g_cam.inited = true;
g_cam.prevFrom = fromEngine;
g_cam.prevVelDirFilt = fEngine;
g_cam.velDirFilt = fEngine;
g_cam.speedFilt = 0.0f;
g_cam.yawRateFilt = 0.0f;
g_cam.yawBias = 0.0f;
g_cam.rollBias = 0.0f;
g_cam.gNorm = 0.0f;
g_cam.horizonLock = 1.0f;
}
}
// ==========================================================
// LOOK-AT HOOK
// ==========================================================
using CreateLookAt_t = int(__cdecl*)(void* out, Vec3* from, Vec3* to, Vec3* up);
static CreateLookAt_t oCreateLookAt = nullptr;
using SetCameraMatrix_t = void(__thiscall*)(void* self, Mat4* mat, float dt);
static SetCameraMatrix_t oSetCameraMatrix = nullptr;
static bool gUseFinalSetMatrixHook = false;
static void __fastcall hkSetCameraMatrix(void* self, void*, Mat4* mat, float dt)
{
if (InterlockedExchange(&gApplyRollOnSetMatrix, 0) != 0)
ApplyRollKeepPos(mat, g_cam.rollBias);
oSetCameraMatrix(self, mat, dt);
}
static int __cdecl hkCreateLookAt(void* outMatrix, Vec3* from, Vec3* to, Vec3* up)
{
const bool rawMode = (g_rollCfg.snappyMode < -0.5f);
const bool balancedMode = (g_rollCfg.snappyMode > 0.5f);
if (!from || !to || !up)
return oCreateLookAt(outMatrix, from, to, up);
// 1) engine builds camera first
int ret = oCreateLookAt(outMatrix, from, to, up);
// 2) time ALWAYS advances (prevents director stealing dt)
const float now = GetTimeSeconds_Safe();
if (!g_cam.inited)
{
init_camera(outMatrix, from, to, up);
g_cam.lastT = now; // IMPORTANT: seed time here
// apply is harmless here (roll=0), but keep consistent
ApplyRollKeepPos((Mat4*)outMatrix, g_cam.rollBias);
return ret;
}
float rawDt = now - g_cam.lastT;
g_cam.lastT = now;
// if dt is garbage, still apply current roll but DO NOT integrate
if (rawDt <= 0.0f || rawDt > 0.5f)
{
ApplyRollKeepPos((Mat4*)outMatrix, g_cam.rollBias);
return ret;
}
float dt = clampf(rawDt, 1.0f / 240.0f, 1.0f / 30.0f);
// 3) detect dummy/probe cameras (the {0,0,0}->{1,0,0} ones)
// DO NOT integrate on these.
const bool isProbe =
(fabsf(from->x) < 1e-4f && fabsf(from->y) < 1e-4f && fabsf(from->z) < 1e-4f);
// 4) consume wrapper flag ONCE
const bool authoritative =
(InterlockedExchange((volatile LONG*)&gApplyUG2Flag, 0) != 0);
// =====================================================================
// INTEGRATE ONLY ON (authoritative && !probe)
// =====================================================================
if (authoritative && !isProbe)
{
// stable up axis
Vec3 upW = *up;
if (len(upW) < 1e-4f) upW = v3(0, 0, 1);
upW = norm(upW);
// velocity from camera position
Vec3 vel = sub(*from, g_cam.prevFrom);
g_cam.prevFrom = *from;
// project onto ground plane (Z-up => remove component along upW)
vel = sub(vel, mul(upW, dot(vel, upW)));
float speedU = len(vel) / dt;
Vec3 vdir = vel;
float vlen = len(vdir);
float dirK = balancedMode ? 18.0f : 10.0f;
float speedK = balancedMode ? 12.0f : 6.0f;
float yawK = balancedMode ? 16.0f : 8.0f;
float dirResp = 1.0f - expf(-dirK * dt);
float speedResp = 1.0f - expf(-speedK * dt);
if (vlen > 1e-3f && speedU > 1e-2f)
{
vdir = mul(vdir, 1.0f / vlen);
Vec3 prevF = g_cam.prevVelDirFilt;
Vec3 curF = vdir;
float yawS = dot(upW, cross(prevF, curF)); // signed
float yawRateRaw = yawS / dt;
if (rawMode)
{
g_cam.velDirFilt = vdir;
g_cam.speedFilt = speedU;
g_cam.yawRateFilt = yawRateRaw;
g_cam.prevVelDirFilt = vdir;
}
else
{
// direction filter (NO lerp helper needed)
g_cam.velDirFilt = norm(add(
mul(g_cam.velDirFilt, 1.0f - dirResp),
mul(vdir, dirResp)));
// speed filter
g_cam.speedFilt += (speedU - g_cam.speedFilt) * speedResp;
// yaw rate from filtered direction around upW
curF = g_cam.velDirFilt;
g_cam.prevVelDirFilt = curF;
yawS = dot(upW, cross(prevF, curF)); // signed
yawRateRaw = yawS / dt;
float yawResp = 1.0f - expf(-yawK * dt);
g_cam.yawRateFilt += (yawRateRaw - g_cam.yawRateFilt) * yawResp;
}
}
else
{
if (rawMode)
{
g_cam.yawRateFilt = 0.0f;
g_cam.speedFilt = 0.0f;
}
else
{
// decay when stopped
g_cam.yawRateFilt += (0.0f - g_cam.yawRateFilt) * speedResp;
g_cam.speedFilt += (0.0f - g_cam.speedFilt) * speedResp;
}
}
// --- UG2 roll intent (configurable) ---
float yawRate = g_cam.yawRateFilt;
float speedF = g_cam.speedFilt;
float yawDeadzone = g_rollCfg.yawDeadzone;
if (rawMode)
yawDeadzone = 1e-6f;
else if (balancedMode)
yawDeadzone = fmaxf(0.001f, g_rollCfg.yawDeadzone * 0.5f);
if (fabsf(yawRate) < yawDeadzone)
yawRate = 0.0f;
float latG = fabsf(yawRate) * speedF * g_rollCfg.latGScale;
float g01 = saturate((latG - g_rollCfg.latGStart) /
(g_rollCfg.latGFull - g_rollCfg.latGStart));
if (!rawMode)
g01 = g01 * g01 * (3.0f - 2.0f * g01); // smoothstep
float rollTarget = signf(-yawRate) * g01 * DEG2RAD(g_rollCfg.maxRollDeg);
// target filter (optional in snappy mode)
float rollCmd = rollTarget;
if (!rawMode)
{
float cmdK = balancedMode ? (g_rollCfg.cmdResponse * 1.8f) : g_rollCfg.cmdResponse;
float cmdResp = 1.0f - expf(-cmdK * dt);
g_cam.yawBias += (rollTarget - g_cam.yawBias) * cmdResp;
rollCmd = g_cam.yawBias;
}
else
{
g_cam.yawBias = rollTarget;
}
if (rawMode)
{
// Near-raw mode: no rate limiting or wind/unwind damping.
g_cam.rollBias = rollCmd;
}
else
{
// rate limit
float rateLimitDeg = balancedMode ? (g_rollCfg.rateLimitDeg * 1.8f) : g_rollCfg.rateLimitDeg;
float maxStep = DEG2RAD(rateLimitDeg) * dt;
float diff = clampf(rollCmd - g_cam.rollBias, -maxStep, +maxStep);
rollCmd = g_cam.rollBias + diff;
// asym damping (wind-in / unwind)
float k = (fabsf(rollCmd) > fabsf(g_cam.rollBias))
? g_rollCfg.windInK
: g_rollCfg.unwindK;
if (balancedMode)
k *= 1.6f;
g_cam.rollBias += (rollCmd - g_cam.rollBias) * (1.0f - expf(-k * dt));
}
// optional clamp
g_cam.rollBias = clampf(g_cam.rollBias,
DEG2RAD(-g_rollCfg.clampRollDeg),
DEG2RAD(+g_rollCfg.clampRollDeg));
}
else
{
// Director/non-authoritative pass: do not integrate or decay.
// Keep prevFrom sane so next authoritative frame has no teleport impulse.
if (!isProbe)
g_cam.prevFrom = *from;
return ret;
}
// Authoritative pass only.
if (gUseFinalSetMatrixHook)
{
// Carbon has post-look-at camera passes; apply at final SetCameraMatrix.
InterlockedExchange(&gApplyRollOnSetMatrix, 1);
}
else
{
// Fallback when final hook is unavailable.
ApplyRollKeepPos((Mat4*)outMatrix, g_cam.rollBias);
}
return ret;
}
// ==========================================================
// Hook
// ==========================================================
bool InstallHooks_CB()
{
LoadConfig();
if (!g_rollCfg.enabled)
return false;
if (MH_Initialize() != MH_OK)
return false;
// 1) Hook wrapper (authoritative camera update)
MH_CreateHook(
(LPVOID)Game::CameraWrapperAddr,
&hkCameraWrapper_Naked,
&oCameraWrapper
);
// 2) Hook CreateLookAt builder
MH_CreateHook(
(LPVOID)Game::CreateLookAtAddr,
&hkCreateLookAt,
(LPVOID*)&oCreateLookAt
);
if (Game::SetCameraMatrixAddr)
{
if (MH_CreateHook(
(LPVOID)Game::SetCameraMatrixAddr,
&hkSetCameraMatrix,
(LPVOID*)&oSetCameraMatrix) == MH_OK)
{
gUseFinalSetMatrixHook = true;
}
}
// 3) Optional: disable tilts
if (Game::DisableTiltsAddr)
{
static float DisableTilts = -1000.0f;
*(float**)Game::DisableTiltsAddr = &DisableTilts;
}
MH_EnableHook(MH_ALL_HOOKS);
return true;
}