/
Camera.cs
757 lines (720 loc) · 18.6 KB
/
Camera.cs
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
using System;
using CitizenFX.Core.Native;
using System.Security;
#if MONO_V2
using CitizenFX.Core;
using API = CitizenFX.FiveM.Native.Natives;
namespace CitizenFX.FiveM
#else
namespace CitizenFX.Core
#endif
{
public enum CameraShake
{
Hand,
SmallExplosion,
MediumExplosion,
LargeExplosion,
Jolt,
Vibrate,
RoadVibration,
Drunk,
SkyDiving,
FamilyDrugTrip,
DeathFail
}
public sealed class Camera : PoolObject, IEquatable<Camera>, ISpatial
{
#region Fields
internal static readonly string[] _shakeNames = {
"HAND_SHAKE",
"SMALL_EXPLOSION_SHAKE",
"MEDIUM_EXPLOSION_SHAKE",
"LARGE_EXPLOSION_SHAKE",
"JOLT_SHAKE",
"VIBRATE_SHAKE",
"ROAD_VIBRATION_SHAKE",
"DRUNK_SHAKE",
"SKY_DIVING_SHAKE",
"FAMILY5_DRUG_TRIP_SHAKE",
"DEATH_FAIL_IN_EFFECT_SHAKE"
};
#endregion
public Camera(int handle) : base(handle)
{
}
/// <summary>
/// Gets the memory address of this <see cref="Camera"/>.
/// </summary>
public IntPtr MemoryAddress
{
//get { return MemoryAccess.GetCameraAddress(Handle); }
get
{
// CFX-TODO
return IntPtr.Zero;
}
}
private IntPtr MatrixAddress
{
[SecuritySafeCritical]
get
{
IntPtr address = MemoryAddress;
if (address == IntPtr.Zero)
{
return IntPtr.Zero;
}
#if MONO_V2
return MemoryAccess.Offset(address, MemoryAccess.IsBitSet(address, 0x220, 0) ? 0x110 : 0x30);
#else
return (MemoryAccess.ReadByte(address + 0x220) & 1) == 0 ? address + 0x30 : address + 0x110;
#endif
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Camera"/> is currently being rendered.
/// </summary>
/// <value>
/// <c>true</c> if this <see cref="Camera"/> is active; otherwise, <c>false</c>.
/// </value>
public bool IsActive
{
get
{
return API.IsCamActive(Handle);
}
set
{
API.SetCamActive(Handle, value);
}
}
/// <summary>
/// Gets or sets the position of this <see cref="Camera"/>.
/// </summary>
public Vector3 Position
{
get
{
return API.GetCamCoord(Handle);
}
set
{
API.SetCamCoord(Handle, value.X, value.Y, value.Z);
}
}
/// <summary>
/// Gets or sets the rotation of this <see cref="Camera"/>.
/// </summary>
/// <value>
/// The yaw, pitch and roll rotations measured in degrees.
/// </value>
public Vector3 Rotation
{
get
{
return API.GetCamRot(Handle, 2);
}
set
{
API.SetCamRot(Handle, value.X, value.Y, value.Z, 2);
}
}
/// <summary>
/// Gets or sets the direction this <see cref="Camera"/> is pointing in.
/// </summary>
public Vector3 Direction
{
get { return ForwardVector; }
set
{
value.Normalize();
Vector3 vector1 = new Vector3(value.X, value.Y, 0f);
Vector3 vector2 = new Vector3(value.Z, vector1.Length(), 0f);
Vector3 vector3 = Vector3.Normalize(vector2);
Rotation = new Vector3((float)(System.Math.Atan2(vector3.X, vector3.Y) * 57.295779513082323), Rotation.Y, (float)(System.Math.Atan2(value.X, value.Y) * -57.295779513082323));
}
}
/// <summary>
/// Gets the up vector of this <see cref="Camera"/>.
/// </summary>
public Vector3 UpVector
{
get
{
return Matrix.Up;
}
}
/// <summary>
/// Gets the forward vector of this <see cref="Camera"/>, see also <seealso cref="Direction"/>.
/// </summary>
public Vector3 ForwardVector
{
get
{
return Matrix.Forward;
}
}
/// <summary>
/// Gets the right vector of this <see cref="Camera"/>.
/// </summary>
public Vector3 RightVector
{
get
{
return Matrix.Right;
}
}
/// <summary>
/// Gets the matrix of this <see cref="Camera"/>.
/// </summary>
public Matrix Matrix
{
get
{
Vector3 rightVector = new Vector3();
Vector3 forwardVector = new Vector3();
Vector3 upVector = new Vector3();
Vector3 position = new Vector3();
API.GetCamMatrix(Handle, ref rightVector, ref forwardVector, ref upVector, ref position);
return new Matrix(
rightVector.X, rightVector.Y, rightVector.Z, 0.0f,
forwardVector.X, forwardVector.Y, forwardVector.Z, 0.0f,
upVector.X, upVector.Y, upVector.Z, 0.0f,
position.X, position.Y, position.Z, 1.0f
);
}
}
/// <summary>
/// Gets the position in world coords of an offset relative to this <see cref="Camera"/>
/// </summary>
/// <param name="offset">The offset from this <see cref="Camera"/>.</param>
public Vector3 GetOffsetPosition(Vector3 offset)
{
return Vector3.TransformCoordinate(offset, Matrix);
}
/// <summary>
/// Gets the relative offset of this <see cref="Camera"/> from a world coords position
/// </summary>
/// <param name="worldCoords">The world coords.</param>
public Vector3 GetPositionOffset(Vector3 worldCoords)
{
return Vector3.TransformCoordinate(worldCoords, Matrix.Invert(Matrix));
}
/// <summary>
/// Gets or sets the field of view of this <see cref="Camera"/>.
/// </summary>
public float FieldOfView
{
get
{
return API.GetCamFov(Handle);
}
set
{
API.SetCamFov(Handle, value);
}
}
/// <summary>
/// Gets or sets the near clip of this <see cref="Camera"/>.
/// </summary>
public float NearClip
{
get
{
return API.GetCamNearClip(Handle);
}
set
{
API.SetCamNearClip(Handle, value);
}
}
/// <summary>
/// Gets or sets the far clip of this <see cref="Camera"/>.
/// </summary>
public float FarClip
{
get
{
return API.GetCamFarClip(Handle);
}
set
{
API.SetCamFarClip(Handle, value);
}
}
/// <summary>
/// Sets the near depth of field for this <see cref="Camera"/>.
/// </summary>
public float NearDepthOfField
{
set
{
API.SetCamNearDof(Handle, value);
}
}
/// <summary>
/// Gets or sets the far depth of field of this <see cref="Camera"/>.
/// </summary>
public float FarDepthOfField
{
get
{
return API.GetCamFarDof(Handle);
}
set
{
API.SetCamFarDof(Handle, value);
}
}
/// <summary>
/// Sets the depth of field strength for this <see cref="Camera"/>.
/// </summary>
public float DepthOfFieldStrength
{
set
{
API.SetCamDofStrength(Handle, value);
}
}
/// <summary>
/// Sets the strenght of the motion blur for this <see cref="Camera"/>
/// </summary>
public float MotionBlurStrength
{
set
{
API.SetCamMotionBlurStrength(Handle, value);
}
}
/// <summary>
/// Shakes this <see cref="Camera"/>.
/// </summary>
/// <param name="shakeType">Type of the shake to apply.</param>
/// <param name="amplitude">The amplitude of the shaking.</param>
public void Shake(CameraShake shakeType, float amplitude)
{
API.ShakeCam(Handle, _shakeNames[(int)shakeType], amplitude);
}
/// <summary>
/// Stops shaking this <see cref="Camera"/>.
/// </summary>
public void StopShaking()
{
API.StopCamShaking(Handle, true);
}
/// <summary>
/// Gets a value indicating whether this <see cref="Camera"/> is shaking.
/// </summary>
/// <value>
/// <c>true</c> if this <see cref="Camera"/> is shaking; otherwise, <c>false</c>.
/// </value>
public bool IsShaking
{
get
{
return API.IsCamShaking(Handle);
}
}
/// <summary>
/// Sets the shake amplitude for this <see cref="Camera"/>.
/// </summary>
public float ShakeAmplitude
{
set
{
API.SetCamShakeAmplitude(Handle, value);
}
}
/// <summary>
/// Points this <see cref="Camera"/> at a specified <see cref="Entity"/>.
/// </summary>
/// <param name="target">The <see cref="Entity"/> to point at.</param>
/// <param name="offset">The offset from the <paramref name="target"/> to point at.</param>
public void PointAt(Entity target, Vector3 offset = default(Vector3))
{
API.PointCamAtEntity(Handle, target.Handle, offset.X, offset.Y, offset.Z, true);
}
/// <summary>
/// Points this <see cref="Camera"/> at a specified <see cref="PedBone"/>.
/// </summary>
/// <param name="target">The <see cref="PedBone"/> to point at.</param>
/// <param name="offset">The offset from the <paramref name="target"/> to point at</param>
public void PointAt(PedBone target, Vector3 offset = default(Vector3))
{
API.PointCamAtPedBone(Handle, target.Owner.Handle, target, offset.X, offset.Y, offset.Z, true);
}
/// <summary>
/// Points this <see cref="Camera"/> at a specified position.
/// </summary>
/// <param name="target">The position to point at.</param>
public void PointAt(Vector3 target)
{
API.PointCamAtCoord(Handle, target.X, target.Y, target.Z);
}
/// <summary>
/// Stops this <see cref="Camera"/> pointing at a specific target.
/// </summary>
public void StopPointing()
{
API.StopCamPointing(Handle);
}
/// <summary>
/// Starts a transition between this <see cref="Camera"/> and the new camera. (Old (incorrect) function, please use the overload function!)
/// </summary>
/// <param name="to"></param>
/// <param name="duration"></param>
/// <param name="easePosition"></param>
/// <param name="easeRotation"></param>
public void InterpTo(Camera to, int duration, bool easePosition, bool easeRotation)
{
API.SetCamActiveWithInterp(to.Handle, Handle, duration, easePosition ? 1 : 0, easeRotation ? 1 : 0);
}
/// <summary>
/// Starts a transition between this <see cref="Camera"/> and the new camera.
/// </summary>
/// <param name="to"></param>
/// <param name="duration"></param>
/// <param name="easePosition"></param>
/// <param name="easeRotation"></param>
public void InterpTo(Camera to, int duration, int easePosition, int easeRotation)
{
API.SetCamActiveWithInterp(to.Handle, Handle, duration, easePosition, easeRotation);
}
/// <summary>
/// Gets a value indicating whether this <see cref="Camera"/> is interpolating.
/// </summary>
/// <value>
/// <c>true</c> if this <see cref="Camera"/> is interpolating; otherwise, <c>false</c>.
/// </value>
public bool IsInterpolating
{
get
{
return API.IsCamInterpolating(Handle);
}
}
/// <summary>
/// Attaches this <see cref="Camera"/> to a specific <see cref="Entity"/>.
/// </summary>
/// <param name="entity">The <see cref="Entity"/> to attach to.</param>
/// <param name="offset">The offset from the <paramref name="entity"/> to attach to.</param>
public void AttachTo(Entity entity, Vector3 offset)
{
API.AttachCamToEntity(Handle, entity.Handle, offset.X, offset.Y, offset.Z, true);
}
/// <summary>
/// Attaches this <see cref="Camera"/> to a specific <see cref="PedBone"/>.
/// </summary>
/// <param name="pedBone">The <see cref="PedBone"/> to attach to.</param>
/// <param name="offset">The offset from the <paramref name="pedBone"/> to attach to.</param>
public void AttachTo(PedBone pedBone, Vector3 offset)
{
API.AttachCamToPedBone(Handle, pedBone.Owner.Handle, pedBone, offset.X, offset.Y, offset.Z, true);
}
/// <summary>
/// Detaches this <see cref="Camera"/> from any <see cref="Entity"/> or <see cref="PedBone"/> it may be attached to.
/// </summary>
public void Detach()
{
API.DetachCam(Handle);
}
/// <summary>
/// Destroys this <see cref="Camera"/>.
/// </summary>
public override void Delete()
{
API.DestroyCam(Handle, false);
}
public override bool Exists()
{
return API.DoesCamExist(Handle);
}
public static bool Exists(Camera camera)
{
return !ReferenceEquals(camera, null) && camera.Exists();
}
public bool Equals(Camera camera)
{
return !ReferenceEquals(camera, null) && Handle == camera.Handle;
}
public override bool Equals(object obj)
{
return !ReferenceEquals(obj, null) && obj.GetType() == GetType() && Equals((Camera)obj);
}
public override int GetHashCode()
{
return Handle;
}
public static bool operator ==(Camera left, Camera right)
{
return ReferenceEquals(left, null) ? ReferenceEquals(right, null) : left.Equals(right);
}
public static bool operator !=(Camera left, Camera right)
{
return !(left == right);
}
}
public static class GameplayCamera
{
/// <summary>
/// Gets the memory address of the <see cref="GameplayCamera"/>.
/// </summary>
public static IntPtr MemoryAddress
{
//get { return MemoryAccess.GetGameplayCameraAddress(); }
get { return IntPtr.Zero; }
}
/// <summary>
/// Gets the position of the <see cref="GameplayCamera"/>.
/// </summary>
public static Vector3 Position
{
get
{
return API.GetGameplayCamCoord();
}
}
/// <summary>
/// Gets the rotation of the <see cref="GameplayCamera"/>.
/// </summary>
/// <value>
/// The yaw, pitch and roll rotations measured in degrees.
/// </value>
public static Vector3 Rotation
{
get
{
return API.GetGameplayCamRot(2);
}
}
/// <summary>
/// Gets the direction the <see cref="GameplayCamera"/> is pointing in.
/// </summary>
public static Vector3 Direction
{
get
{
return ForwardVector;
}
}
/// <summary>
/// Gets the up vector of the <see cref="GameplayCamera"/>.
/// </summary>
public static Vector3 UpVector
{
#if MONO_V2
[SecuritySafeCritical] get => MemoryAccess.ReadIfNotNull(MemoryAddress, 0x210, Vector3.Up);
#else
get { return MemoryAccess.ReadVector3(MemoryAddress + 0x210); }
#endif
}
/// <summary>
/// Gets the forward vector of the <see cref="GameplayCamera"/>, see also <seealso cref="Direction"/>.
/// </summary>
public static Vector3 ForwardVector
{
#if MONO_V2
[SecuritySafeCritical] get => MemoryAccess.ReadIfNotNull(MemoryAddress, 0x200, Vector3.ForwardLH);
#else
get { return MemoryAccess.ReadVector3(MemoryAddress + 0x200); }
#endif
}
/// <summary>
/// Gets the right vector of the <see cref="GameplayCamera"/>.
/// </summary>
public static Vector3 RightVector
{
#if MONO_V2
[SecuritySafeCritical] get => MemoryAccess.ReadIfNotNull(MemoryAddress, 0x1F0, Vector3.Right);
#else
get { return MemoryAccess.ReadVector3(MemoryAddress + 0x1F0); }
#endif
}
/// <summary>
/// Gets the matrix of the <see cref="GameplayCamera"/>.
/// </summary>
public static Matrix Matrix
{
#if MONO_V2
[SecuritySafeCritical] get => MemoryAccess.ReadIfNotNull(MemoryAddress, 0x1F0, default(Matrix));
#else
get { return MemoryAccess.ReadMatrix(MemoryAddress + 0x1F0); }
#endif
}
/// <summary>
/// Gets the position in world coords of an offset relative to the <see cref="GameplayCamera"/>
/// </summary>
/// <param name="offset">The offset from the <see cref="GameplayCamera"/>.</param>
public static Vector3 GetOffsetPosition(Vector3 offset)
{
return Vector3.TransformCoordinate(offset, Matrix);
}
/// <summary>
/// Gets the relative offset of the <see cref="GameplayCamera"/> from a world coords position
/// </summary>
/// <param name="worldCoords">The world coords.</param>
public static Vector3 GetPositionOffset(Vector3 worldCoords)
{
return default(Vector3);
//return Matrix.InverseTransformPoint(worldCoords);
}
/// <summary>
/// Gets or sets the relative pitch of the <see cref="GameplayCamera"/>.
/// </summary>
public static float RelativePitch
{
get
{
return API.GetGameplayCamRelativePitch();
}
set
{
API.SetGameplayCamRelativePitch(value, 1f);
}
}
/// <summary>
/// Gets or sets the relative heading of the <see cref="GameplayCamera"/>.
/// </summary>
public static float RelativeHeading
{
get
{
return API.GetGameplayCamRelativeHeading();
}
set
{
API.SetGameplayCamRelativeHeading(value);
}
}
/// <summary>
/// Clamps the yaw of the <see cref="GameplayCamera"/>.
/// </summary>
/// <param name="min">The minimum yaw value.</param>
/// <param name="max">The maximum yaw value.</param>
public static void ClampYaw(float min, float max)
{
API.ClampGameplayCamYaw(min, max);
}
/// <summary>
/// Clamps the pitch of the <see cref="GameplayCamera"/>.
/// </summary>
/// <param name="min">The minimum pitch value.</param>
/// <param name="max">The maximum pitch value.</param>
public static void ClampPitch(float min, float max)
{
API.ClampGameplayCamPitch(min, max);
}
/// <summary>
/// Gets the zoom of the <see cref="GameplayCamera"/>.
/// </summary>
public static float Zoom
{
get
{
return API.GetGameplayCamZoom();
}
}
/// <summary>
/// Gets the field of view of the <see cref="GameplayCamera"/>.
/// </summary>
public static float FieldOfView
{
get
{
return API.GetGameplayCamFov();
}
}
/// <summary>
/// Gets a value indicating whether the <see cref="GameplayCamera"/> is rendering.
/// </summary>
/// <value>
/// <c>true</c> if the <see cref="GameplayCamera"/> is rendering; otherwise, <c>false</c>.
/// </value>
public static bool IsRendering
{
get
{
return API.IsGameplayCamRendering();
}
}
/// <summary>
/// Gets a value indicating whether the aiming camera is rendering.
/// </summary>
/// <value>
/// <c>true</c> if the aiming camera is rendering; otherwise, <c>false</c>.
/// </value>
public static bool IsAimCamActive
{
get
{
return API.IsAimCamActive();
}
}
/// <summary>
/// Gets a value indicating whether the first person aiming camera is rendering.
/// </summary>
/// <value>
/// <c>true</c> if the aiming camera is rendering; otherwise, <c>false</c>.
/// </value>
public static bool IsFirstPersonAimCamActive
{
get
{
return API.IsFirstPersonAimCamActive();
}
}
/// <summary>
/// Gets a value indicating whether the <see cref="GameplayCamera"/> is looking behind.
/// </summary>
/// <value>
/// <c>true</c> if the <see cref="GameplayCamera"/> is looking behind; otherwise, <c>false</c>.
/// </value>
public static bool IsLookingBehind
{
get
{
return API.IsGameplayCamLookingBehind();
}
}
/// <summary>
/// Shakes the <see cref="GameplayCamera"/>.
/// </summary>
/// <param name="shakeType">Type of the shake to apply.</param>
/// <param name="amplitude">The amplitude of the shaking.</param>
public static void Shake(CameraShake shakeType, float amplitude)
{
API.ShakeGameplayCam(Camera._shakeNames[(int)shakeType], amplitude);
}
/// <summary>
/// Stops shaking the <see cref="GameplayCamera"/>.
/// </summary>
public static void StopShaking()
{
API.StopGameplayCamShaking(true);
}
/// <summary>
/// Gets a value indicating whether the <see cref="GameplayCamera"/> is shaking.
/// </summary>
/// <value>
/// <c>true</c> if the <see cref="GameplayCamera"/> is shaking; otherwise, <c>false</c>.
/// </value>
public static bool IsShaking
{
get
{
return API.IsGameplayCamShaking();
}
}
/// <summary>
/// Sets the shake amplitude for the <see cref="GameplayCamera"/>.
/// </summary>
public static float ShakeAmplitude
{
set
{
API.SetGameplayCamShakeAmplitude(value);
}
}
}
}