-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMWCameraManager.cpp
More file actions
346 lines (285 loc) · 9.98 KB
/
Copy pathMWCameraManager.cpp
File metadata and controls
346 lines (285 loc) · 9.98 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 "MWCameraManager.h"
#include <cmath>
#include "Math.h"
#include "MostWanted.h"
#include "ConfigurationManager.h"
#include "TimeManager.h"
#include "CameraManager.h"
#include "minhook/include/MinHook.h"
// ==========================================================
// GLOBAL STRUCTS
// ==========================================================
// Set by wrapper hook, consumed by look-at hook
static volatile LONG gApplyUG2Flag = 0;
// Set by look-at hook, consumed by final SetCameraMatrix hook.
static volatile LONG gApplyRollOnSetMatrix = 0;
constexpr float PI = 3.14159265358979323846f;
using CubicUpdate_t = int(__thiscall*)(void* self, float dt);
using CreateLookAt_t = void(__cdecl*)(Mat4* mat, Vec3* eye, Vec3* center, Vec3* up);
using SetCameraMatrix_t = int(__thiscall*)(void* self, Mat4* mat, float dt);
static CubicUpdate_t oCubicUpdate = nullptr;
static CreateLookAt_t oCreateLookAt = nullptr;
static SetCameraMatrix_t oSetCameraMatrix = nullptr;
static bool gUseFinalSetMatrixHook = false;
// ==========================================================
// LOOK-AT HOOK
// ==========================================================
static int __fastcall hkCubicUpdate(void* self, void*, float dt)
{
InterlockedExchange(&gApplyUG2Flag, 1);
return oCubicUpdate(self, dt);
}
static int __fastcall hkSetCameraMatrix(void* self, void*, Mat4* mat, float dt)
{
if (InterlockedExchange(&gApplyRollOnSetMatrix, 0) != 0)
ApplyRollKeepPos(mat, g_cam.rollBias);
return oSetCameraMatrix(self, mat, dt);
}
static void __cdecl hkCreateLookAtMatrix(Mat4* mat, Vec3* eye, Vec3* center, Vec3* up)
{
const bool rawMode = (g_rollCfg.snappyMode < -0.5f);
const bool balancedMode = (g_rollCfg.snappyMode > 0.5f);
// Let MW build camera (director, blends, etc.)
oCreateLookAt(mat, eye, center, up);
// Authoritative pass only once per frame
bool applyRoll = (InterlockedExchange(&gApplyUG2Flag, 0) != 0);
// ------------------------------------------------------------
// INIT
// ------------------------------------------------------------
if (!g_cam.inited)
{
init_camera(eye);
return;
}
// ------------------------------------------------------------
// TIME
// ------------------------------------------------------------
float t = GetTimeSeconds_Safe();
if (!g_cam.timeValid)
{
g_cam.lastT = t;
g_cam.timeValid = true;
g_cam.prevFrom = *eye;
g_cam.viewYawValid = false;
return;
}
float rawDt = t - g_cam.lastT;
g_cam.lastT = t;
if (rawDt <= 0.0f || rawDt > 0.5f)
{
g_cam.viewYawValid = false;
return;
}
float dt = clampf(rawDt, 1.0f / 240.0f, 1.0f / 30.0f);
// ------------------------------------------------------------
// CAMERA POSITION / CUT DETECTION
// ------------------------------------------------------------
Vec3 fromNow = *eye;
Vec3 delta = sub(fromNow, g_cam.prevFrom);
// Determine vertical axis from 'up' argument (works in all cams)
Vec3 upW = *up;
if (len(upW) < 1e-4f) upW = v3(0, 0, 1);
upW = norm(upW);
// Flatten delta onto ground plane
delta = sub(delta, mul(upW, dot(delta, upW)));
float cutDist = len(delta);
// Cut threshold: distance on ground plane + dt sanity
if (cutDist > 60.0f)
{
g_cam.prevFrom = fromNow;
// Reset motion estimates
g_cam.velDirFilt = v3(0, 0, 1);
g_cam.prevVelDirFilt = g_cam.velDirFilt;
g_cam.speedFilt = 0.0f;
g_cam.yawRateFilt = 0.0f;
// Reset roll
g_cam.rollBias = 0.0f;
g_cam.rollTargetFilt = 0.0f;
g_cam.viewYawValid = false;
return;
}
g_cam.prevFrom = fromNow;
// ------------------------------------------------------------
// SPEED + VELOCITY DIRECTION (GROUND PLANE)
// ------------------------------------------------------------
float speed = cutDist / dt;
Vec3 vdir = delta;
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 && speed > 1e-2f)
{
vdir = mul(vdir, 1.0f / vlen);
Vec3 prevF = g_cam.prevVelDirFilt;
Vec3 curF = vdir;
float yawS = dot(upW, cross(prevF, curF));
float yawRateRaw = yawS / dt;
if (rawMode)
{
g_cam.velDirFilt = vdir;
g_cam.speedFilt = speed;
g_cam.yawRateFilt = yawRateRaw;
g_cam.prevVelDirFilt = vdir;
}
else
{
g_cam.velDirFilt = norm(add(
mul(g_cam.velDirFilt, 1.0f - dirResp),
mul(vdir, dirResp)));
g_cam.speedFilt += (speed - g_cam.speedFilt) * speedResp;
curF = g_cam.velDirFilt;
g_cam.prevVelDirFilt = curF;
// Signed yaw delta on the ground plane around upW:
// yawS = dot(upW, cross(prevF, curF))
yawS = dot(upW, cross(prevF, curF));
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
{
g_cam.yawRateFilt += (0.0f - g_cam.yawRateFilt) * speedResp;
g_cam.speedFilt += (0.0f - g_cam.speedFilt) * speedResp;
}
}
// Deadzone
if (!rawMode && fabsf(g_cam.yawRateFilt) < 0.015f)
g_cam.yawRateFilt = 0.0f;
// ------------------------------------------------------------
// ROLL INTENT (UG2 STYLE - CAMERA HEADING BASED)
// ------------------------------------------------------------
float yawRate = g_cam.yawRateFilt;
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) *
g_cam.speedFilt *
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);
float rollTarget =
signf(-yawRate) *
g01 *
DEG2RAD(g_rollCfg.maxRollDeg);
// ------------------------------------------------------------
// UG2 RESPONSE
// ------------------------------------------------------------
// target filter (optional in snappy mode)
if (!rawMode)
{
float cmdK = balancedMode ? (g_rollCfg.cmdResponse * 1.8f) : g_rollCfg.cmdResponse;
float cmdResp = 1.0f - expf(-cmdK * dt);
g_cam.rollTargetFilt += (rollTarget - g_cam.rollTargetFilt) * cmdResp;
rollTarget = g_cam.rollTargetFilt;
}
else
{
g_cam.rollTargetFilt = rollTarget;
}
if (rawMode)
{
// Near-raw mode: no rate limiting or wind/unwind damping.
g_cam.rollBias = rollTarget;
}
else
{
float rateLimitDeg = balancedMode ? (g_rollCfg.rateLimitDeg * 1.8f) : g_rollCfg.rateLimitDeg;
float maxStep = DEG2RAD(rateLimitDeg) * dt;
float diff = clampf(rollTarget - g_cam.rollBias, -maxStep, +maxStep);
rollTarget = g_cam.rollBias + diff;
float k = (fabsf(rollTarget) > fabsf(g_cam.rollBias))
? g_rollCfg.windInK
: g_rollCfg.unwindK;
if (balancedMode)
k *= 1.6f;
g_cam.rollBias += (rollTarget - g_cam.rollBias) *
(1.0f - expf(-k * dt));
}
g_cam.rollBias = clampf(g_cam.rollBias,
DEG2RAD(-g_rollCfg.clampRollDeg),
DEG2RAD(+g_rollCfg.clampRollDeg));
// ------------------------------------------------------------
// HORIZON LOCK (USE SAME YAW SOURCE)
// ------------------------------------------------------------
float yaw01 = saturate(fabsf(yawRate) / 0.10f);
float speed01 = saturate(g_cam.speedFilt / 35.0f);
g_cam.horizonLock =
clampf(
g_rollCfg.horizonBase
- g_rollCfg.horizonYawK * yaw01
+ g_rollCfg.horizonSpeedK * speed01,
g_rollCfg.horizonMin,
g_rollCfg.horizonMax);
// ------------------------------------------------------------
// APPLY ROLL
// ------------------------------------------------------------
if (!applyRoll)
{
// Director/non-authoritative pass: do not integrate or decay.
// This avoids extra damping from multi-pass camera updates.
return;
}
// Authoritative pass only.
if (gUseFinalSetMatrixHook)
{
// MW has multiple post-look-at passes. Apply at final SetCameraMatrix instead.
InterlockedExchange(&gApplyRollOnSetMatrix, 1);
}
else
{
// Fallback when final hook is unavailable.
ApplyRollKeepPos(mat, g_cam.rollBias);
}
}
// ==========================================================
// Hook
// ==========================================================
bool InstallHooks_MW()
{
LoadConfig();
if (!g_rollCfg.enabled)
return false;
if (MH_Initialize() != MH_OK)
return false;
MH_CreateHook(
(LPVOID)Game::DisableTiltsAddr,
hkCubicUpdate,
(LPVOID*)&oCubicUpdate
);
MH_CreateHook(
(LPVOID)Game::CreateLookAtAddr,
hkCreateLookAtMatrix,
(LPVOID*)&oCreateLookAt
);
if (Game::SetCameraMatrixAddr)
{
if (MH_CreateHook(
(LPVOID)Game::SetCameraMatrixAddr,
hkSetCameraMatrix,
(LPVOID*)&oSetCameraMatrix) == MH_OK)
{
gUseFinalSetMatrixHook = true;
}
}
MH_EnableHook(MH_ALL_HOOKS);
return true;
}