15 const string ACTION_WHEEL =
"WeaponChangeMagnification";
16 const string ACTION_ILLUMINATION =
"WeaponToggleSightsIllumination";
18 const float REFERENCE_FOV = 38;
19 const float OPACITY_INITIAL = 0.75;
21 const float NEAR_PLANE_DEFAULT = 0.05;
22 const float NEAR_PLANE_ZOOMED = 0.05;
25 [
Attribute(
"", UIWidgets.ResourcePickerThumbnail,
"Layout used for 2D sights HUD\n[OBSOLETE]",
params:
"layout", category:
"Resources")]
26 protected ResourceName m_sLayoutResource;
28 [
Attribute(
"", UIWidgets.ResourceNamePicker,
desc:
"Texture of reticle\n",
params:
"edds imageset", category:
"Resources")]
29 protected ResourceName m_sReticleTexture;
31 [
Attribute(
"", UIWidgets.ResourceNamePicker,
desc:
"Texture of reticle under glow\n",
params:
"edds imageset", category:
"Resources")]
32 protected ResourceName m_sReticleGlowTexture;
35 protected bool m_bHasIllumination;
37 [
Attribute(
"0 0 0 1", UIWidgets.ColorPicker, category:
"Resources")]
38 protected ref Color m_ReticleColor;
40 [
Attribute(
"1 1 1 1", UIWidgets.ColorPicker, category:
"Resources")]
41 protected ref Color m_ReticleOutlineColor;
43 [
Attribute(
"1 0 0 1", UIWidgets.ColorPicker, category:
"Resources")]
44 protected ref Color m_cReticleTextureIllumination;
46 [
Attribute(
"0.05", UIWidgets.Slider,
desc:
"Reticle glow texture alpha\n[ % ]",
params:
"0 1 0.001", category:
"Resources", precision: 3)]
47 protected float m_fReticleTextureGlowAlpha;
49 [
Attribute(
"", UIWidgets.ResourceNamePicker,
desc:
"Texture of lens filter\n",
params:
"edds imageset", category:
"Resources")]
50 protected ResourceName m_sFilterTexture;
52 [
Attribute(
"10", UIWidgets.EditBox,
desc:
"Optic magnification\n[ x ]",
params:
"0.1 200 0.001", category:
"Sights", precision: 3)]
53 protected float m_fMagnification;
55 [
Attribute(
"0", UIWidgets.Slider,
"Angular size of reticle\n[ ° ]",
params:
"0 60 0.001", category:
"Sights", precision: 3)]
56 protected float m_fReticleAngularSize;
58 [
Attribute(
"1", UIWidgets.Slider,
"Portion of reticle with specified angular size\n[ % ]",
params:
"0 10 0.00001", category:
"Sights", precision: 5)]
59 protected float m_fReticlePortion;
61 [
Attribute(
"0", UIWidgets.Slider,
"POSITIVE:\n Rear focal plane - magnification at which reticle markings are valid\n\nZERO:\n Front focal plane reticle scaling with magnification.\n\nNEGATIVE:\n Enforced base FOV in degrees\n",
params:
"-60 200 0.001", category:
"Sights", precision: 5)]
62 protected float m_fReticleBaseZoom;
64 [
Attribute(
"0", UIWidgets.CheckBox,
"Check if the optic is attached to a turret\n", category:
"Sights")]
65 protected bool m_bIsTurretOptic;
67 [
Attribute(
"10", UIWidgets.EditBox,
desc:
"Field of view of the objective\n[ ° ]",
params:
"0.001 60 0.001", category:
"2DSights", precision: 3)]
68 protected float m_fObjectiveFov;
70 [
Attribute(
"1", UIWidgets.EditBox,
desc:
"Ocular texture scale\n",
params:
"0.001 10 0.001", category:
"2DSights", precision: 3)]
71 protected float m_fObjectiveScale;
73 [
Attribute(
"1.1", UIWidgets.EditBox,
desc:
"Vignette texture scale\n",
params:
"0.001 10 0.001", category:
"2DSights", precision: 3)]
74 protected float m_fVignetteScale;
76 [
Attribute(
"1", UIWidgets.CheckBox,
"Should hide parent object when using 2D sights\n", category:
"2DSights")]
77 protected bool m_bShouldHideParentObject;
79 [
Attribute(
"0", UIWidgets.CheckBox,
"Should hide parent character when using 2D sights\n", category:
"2DSights")]
80 protected bool m_bShouldHideParentCharacter;
83 [
Attribute(
"0", UIWidgets.Slider,
"Time before entering animation starts",
params:
"0 2000 1", category:
"Animations")]
84 protected int m_iAnimationActivationDelay;
86 [
Attribute(
"0", UIWidgets.Slider,
"Animation time for canceling optic.",
params:
"0 2000 1", category:
"Animations")]
87 protected int m_iAnimationDeactivationDelay;
89 [
Attribute(
"0", UIWidgets.Slider,
"Animation time for optic animation",
params:
"0 5 0.01", category:
"Animations")]
90 protected float m_fAnimationEnterTime;
92 [
Attribute(
"2", UIWidgets.Slider,
"Speed of blur fade out on optic entering",
params:
"0.01 5 0.01", category:
"Animations")]
93 protected float m_fAnimationSpeedBlur;
96 [
Attribute(
"0", UIWidgets.EditBox,
desc:
"Optic misalignment damping speed - intended for binoculars, the higher, the faster the misalignment gets zeroed",
params:
"0 60 0.1", category:
"Effects")]
97 protected float m_fMisalignmentDampingSpeed;
99 [
Attribute(
"0.1", UIWidgets.EditBox,
desc:
"Rotation effect scaling",
params:
"-100 100 0.01", category:
"Effects")]
100 protected float m_fRotationScale;
102 [
Attribute(
"4", UIWidgets.EditBox,
desc:
"Rotation damping speed - the higher, the faster the rotation speed gets zeroed",
params:
"0 60 0.1", category:
"Effects")]
103 protected float m_fRotationDampingSpeed;
105 [
Attribute(
"0.01", UIWidgets.EditBox,
desc:
"Movement effect scaling",
params:
"-100 100 0.01", category:
"Effects")]
106 protected float m_fMovementScale;
108 [
Attribute(
"2", UIWidgets.EditBox,
desc:
"Movement damping speed - the higher, the faster the movement speed gets zeroed",
params:
"0 60 0.1", category:
"Effects")]
109 protected float m_fMovementDampingSpeed;
111 [
Attribute(
"1", UIWidgets.EditBox,
desc:
"Roll effect scaling",
params:
"-100 100 0.01", category:
"Effects")]
112 protected float m_fRollScale;
114 [
Attribute(
"0", UIWidgets.EditBox,
desc:
"Roll damping speed - the higher, the faster the optic gets uprighted",
params:
"0 60 0.1", category:
"Effects")]
115 protected float m_fRollDampingSpeed;
117 [
Attribute(
"1", UIWidgets.EditBox,
desc:
"Optic misalignment scale - for turrets and binoculars",
params:
"-60 60 0.1", category:
"Effects")]
118 protected float m_fMisalignmentScale;
120 [
Attribute(
"1", UIWidgets.EditBox,
desc:
"Vignette adjustment speed - the faster, the less inertia there is",
params:
"0 60 0.1", category:
"Effects")]
121 protected float m_fVignetteMoveSpeed;
123 [
Attribute(
"1", UIWidgets.EditBox,
desc:
"Motion blur effect scaling",
params:
"0 100", category:
"Effects")]
124 protected float m_fMotionBlurScale;
126 [
Attribute(
"0.1", UIWidgets.EditBox,
desc:
"Max level of motion blur intensity",
params:
"0 1 0.01", category:
"Effects")]
127 protected float m_fMotionBlurMax;
130 [
Attribute(
"1", UIWidgets.Slider,
"Percentage of camera transition at which sights activate.",
params:
"0.0 1.0 0.01", category:
"Sights")]
131 protected float m_fADSActivationPercentage;
134 [
Attribute(
"0", UIWidgets.Slider,
"Percentage of camera transition at which sights deactivate.",
params:
"0.0 1.0 0.01", category:
"Sights")]
135 protected float m_fADSDeactivationPercentage;
137 [
Attribute(
"0", UIWidgets.ComboBox,
"Type of zeroing for this sights.",
"", ParamEnumArray.FromEnum(SCR_EPIPZeroingType), category:
"Sights")]
138 protected SCR_EPIPZeroingType m_eZeroingType;
140 [
Attribute(
"0", UIWidgets.Slider,
"Reticle Offset of scope center in X",
params:
"-1 1 0.0001", precision: 5, category:
"Sights")]
141 protected float m_fReticleOffsetX;
143 [
Attribute(
"0", UIWidgets.Slider,
"Reticle Offset of scope center in Y",
params:
"-1 1 0.0001", precision: 5, category:
"Sights")]
144 protected float m_fReticleOffsetY;
146 [
Attribute(
"25", UIWidgets.Slider,
"Interpolation speed for zeroing interpolation",
params:
"1 100.0 0.1", category:
"Sights")]
147 protected float m_fReticleOffsetInterpSpeed;
149 [
Attribute(
"0 0 0", UIWidgets.EditBox,
"Camera offset when looking through scope", category:
"Sights",
params:
"inf inf 0 purpose=coords space=entity coordsVar=m_vCameraPoint")]
150 protected vector m_vCameraOffset;
152 [
Attribute(
"0 0 0", UIWidgets.EditBox,
"Camera angles when looking through scope", category:
"Sights",
params:
"inf inf 0 purpose=angles space=entity anglesVar=m_vCameraAngles")]
153 protected vector m_vCameraAngles;
155 [
Attribute(
"1 1 1", UIWidgets.EditBox,
"Camera misalignment scale for 2D optics", category:
"Sights")]
156 protected vector m_vCameraMisalignmentScale;
158 protected float m_fCurrentReticleOffsetY;
159 protected float m_fCurrentCameraPitch;
162 override float GetADSActivationPercentageScript()
164 return m_fADSActivationPercentage;
168 override float GetADSDeactivationPercentageScript()
170 return m_fADSDeactivationPercentage;
175 float GetReticleOffsetX()
177 return m_fReticleOffsetX;
180 void GetReticleTextures(out ResourceName reticleTexture, out ResourceName reticleTextureGlow, out ResourceName filterTexture)
182 reticleTexture = m_sReticleTexture;
183 reticleTextureGlow = m_sReticleGlowTexture;
184 filterTexture = m_sFilterTexture;
188 void GetReticleData(out
float reticleAngularSize, out
float reticlePortion, out
float reticleBaseZoom)
190 reticleAngularSize = m_fReticleAngularSize;
191 reticlePortion = m_fReticlePortion;
192 reticleBaseZoom = m_fReticleBaseZoom;
196 float GetObjectiveFov()
198 return m_fObjectiveFov;
202 float GetObjectiveScale()
204 return m_fObjectiveScale;
208 float GetVignetteScale()
210 return m_fVignetteScale;
214 float GetMisalignmentDampingSpeed()
216 return m_fMisalignmentDampingSpeed;
220 float GetRotationScale()
222 return m_fRotationScale;
226 float GetRotationDampingSpeed()
228 return m_fRotationDampingSpeed;
232 float GetMovementScale()
234 return m_fMovementScale;
238 float GetMovementDampingSpeed()
240 return m_fMovementDampingSpeed;
250 float GetRollDampingSpeed()
252 return m_fRollDampingSpeed;
256 float GetMisalignmentScale()
258 return m_fMisalignmentScale;
262 float GetVignetteMoveSpeed()
264 return m_fVignetteMoveSpeed;
268 float GetMotionBlurScale()
270 return m_fMotionBlurScale;
274 float GetMotionBlurMax()
276 return m_fMotionBlurMax;
281 SCR_EPIPZeroingType GetZeroType()
283 return m_eZeroingType;
287 float GetMagnification()
289 return m_fMagnification;
293 bool GetIsOpticsHidden()
295 return m_bIsOpticsHidden;
304 protected ref array<float> m_aReticleSizes = {};
305 protected int m_iSelectedZoomLevel = 0;
308 protected float m_fScratchesRoll;
311 protected float m_fFovZoomed;
312 protected float m_fDefaultSize;
313 protected float m_fCurrentReticleSize;
314 protected float m_fNearPlaneCurrent;
317 protected bool m_bIsMoving;
318 protected bool m_bIsRotating;
319 protected bool m_bZoomed =
false;
320 protected bool m_bWasEntityHidden =
false;
321 protected int m_iHeadBoneId = -1;
323 protected bool m_bIsOpticsHidden;
324 protected bool m_bIsIlluminationOn;
327 protected ChimeraCharacter m_ParentCharacter =
null;
329 static ref ScriptInvokerBase<OnSightsADSChanged> s_OnSightsADSChanged =
new ScriptInvokerBase<OnSightsADSChanged>();
330 static ref ScriptInvokerBase<On2DOpticsADSChange> s_On2DOpticADSChanged =
new ScriptInvokerBase<On2DOpticsADSChange>();
332 protected ref ScriptInvokerBase<On2DOpticsIlluminationChange> s_OnIlluminationChange =
new ScriptInvokerBase<On2DOpticsIlluminationChange>();
337 return s_OnSetupOpticImage;
341 ScriptInvokerBase<On2DOpticsIlluminationChange> OnIlluminationChange()
343 return s_OnIlluminationChange;
352 if (m_fFovZoomed < 0)
353 m_fFovZoomed = CalculateZoomFOV(-m_fFovZoomed);
360 void SetFovZoomed(
float value)
362 m_fFovZoomed = value;
368 return m_fNearPlaneCurrent;
372 int GetAnimationActivationDelay()
374 return m_iAnimationActivationDelay;
378 int GetAnimationDeactivationDelay()
380 return m_iAnimationDeactivationDelay;
384 float GetAnimationEnterTime()
386 return m_fAnimationEnterTime;
390 float GetAnimationSpeedBlur()
392 return m_fAnimationSpeedBlur;
404 return m_bIsRotating;
408 float GetScratchesRoll()
410 return m_fScratchesRoll;
418 override void OnInit(IEntity owner)
420 m_fScratchesRoll = Math.RandomFloat(0, 360);
424 m_fFovZoomed = -m_fMagnification;
428 override void OnSightADSActivated()
430 m_ParentCharacter = ChimeraCharacter.Cast(
GetOwner().GetParent());
432 super.OnSightADSActivated();
434 s_On2DOpticADSChanged.Invoke(
this,
true);
435 s_OnSightsADSChanged.Invoke(
true, GetFovZoomed());
438 m_fNearPlaneCurrent = NEAR_PLANE_DEFAULT;
439 GetGame().GetCallqueue().CallLater(HideObjects, m_iAnimationActivationDelay,
false);
443 m_bIsOpticsHidden =
false;
446 EnableReticleIllumination(m_bIsIlluminationOn);
450 override void OnSightADSDeactivated()
452 super.OnSightADSDeactivated();
453 s_On2DOpticADSChanged.Invoke(
this,
false);
454 s_OnSightsADSChanged.Invoke(
false, 0);
457 m_fCurrentReticleOffsetY = GetReticleOffsetYTarget();
459 if (m_bWasEntityHidden)
463 owner.SetFlags(EntityFlags.VISIBLE,
false);
465 if (m_ParentCharacter)
466 m_ParentCharacter.SetFlags(EntityFlags.VISIBLE,
false);
468 m_bWasEntityHidden =
false;
471 GetGame().GetCallqueue().Remove(HideObjects);
474 m_fNearPlaneCurrent = NEAR_PLANE_DEFAULT;
477 m_bIsOpticsHidden =
true;
480 EnableReticleIllumination(
false);
484 protected override void OnSightADSPostFrame(IEntity owner,
float timeSlice)
486 float interp = Math.Min(1, timeSlice * m_fReticleOffsetInterpSpeed);
488 float reticleTarget = GetReticleOffsetYTarget();
489 m_fCurrentReticleOffsetY = Math.Lerp(m_fCurrentReticleOffsetY, reticleTarget, interp);
490 float pitchTarget = GetCameraPitchTarget();
491 m_fCurrentCameraPitch = Math.Lerp(m_fCurrentCameraPitch, pitchTarget, interp);
496 protected void HideObjects()
499 m_fNearPlaneCurrent = NEAR_PLANE_ZOOMED;
502 if (m_bShouldHideParentObject)
506 owner.ClearFlags(EntityFlags.VISIBLE,
false);
509 m_bWasEntityHidden =
true;
513 if (m_bShouldHideParentCharacter)
515 if (m_ParentCharacter)
516 m_ParentCharacter.ClearFlags(EntityFlags.VISIBLE,
false);
519 m_bWasEntityHidden =
true;
525 static float CalculateZoomFOV(
float magnification)
527 CameraManager cameraManager =
GetGame().GetCameraManager();
532 PlayerCamera camera = PlayerCamera.Cast(cameraManager.CurrentCamera());
534 referenceFOV = camera.GetFocusFOV();
536 referenceFOV = cameraManager.GetFirstPersonFOV();
538 float referenceTan = Math.Tan(Math.DEG2RAD * (referenceFOV * 0.5));
539 return Math.RAD2DEG * 2 * Math.Atan2(referenceTan, magnification);
544 bool IsMoveCloseToLimit(vector move,
float limitMin)
549 return (
float.AlmostEqual(x, 0, limitMin) &&
float.AlmostEqual(y, 0, limitMin));
557 vector GetMisalignmentAngles(notnull CameraBase camera)
561 vector parentToSightMat[4];
562 GetParentToLocalTransform(parentToSightMat, camera.GetParent());
566 camera.GetLocalTransform(cameraMat);
570 vector sightAngles = {-m_vCameraAngles[1], - m_vCameraAngles[0] - m_fCurrentCameraPitch, 0};
571 Math3D.AnglesToMatrix(sightAngles, sightMat);
574 Math3D.MatrixMultiply3(sightMat, parentToSightMat, sightMat);
577 Math3D.MatrixMultiply3(sightMat, cameraMat, sightMat);
582 Math3D.MatrixInvMultiply3(sightMat, turretMat, sightMat);
585 vector angles = Math3D.MatrixToAngles(sightMat);
588 angles[2] = angles[2] + m_vCameraAngles[2];
591 angles[0] = angles[0] * m_vCameraMisalignmentScale[0];
592 angles[1] = angles[1] * m_vCameraMisalignmentScale[1];
593 angles[2] = angles[2] * m_vCameraMisalignmentScale[2];
603 vector GetRotation(out vector previousDir,
float timeSlice)
608 CameraManager cameraManager =
GetGame().GetCameraManager();
612 CameraBase camera = cameraManager.CurrentCamera();
616 vector currentDir = camera.GetWorldTransformAxis(2);
618 bool rotated = !
float.AlmostEqual(previousDir[0], currentDir[0]);
619 rotated |= !
float.AlmostEqual(previousDir[1], currentDir[1]);
623 m_bIsRotating =
true;
624 vector
rotation = (currentDir - previousDir) / timeSlice;
625 previousDir = currentDir;
626 return camera.VectorToLocal(
rotation);
629 m_bIsRotating =
false;
635 vector GetMovement(out vector previousPos,
float timeSlice)
640 CameraManager cameraManager =
GetGame().GetCameraManager();
644 CameraBase camera = cameraManager.CurrentCamera();
648 vector currentPos = camera.GetWorldTransformAxis(3);
650 bool moved = !
float.AlmostEqual(previousPos[0], currentPos[0]);
651 moved &= !
float.AlmostEqual(previousPos[1], currentPos[1]);
652 moved &= !
float.AlmostEqual(previousPos[2], currentPos[2]);
657 vector movement = (currentPos - previousPos) / timeSlice;
658 previousPos = currentPos;
659 return camera.VectorToLocal(movement);
667 protected vector GetSightsRelPosition()
670 if (m_ParentCharacter && owner)
671 return m_ParentCharacter.CoordToLocal(owner.GetOrigin());
677 bool HasIllumination()
679 return m_bHasIllumination;
683 Color GetReticleColor()
685 return Color.FromInt(m_ReticleColor.PackToInt());
689 Color GetReticleOutlineColor()
691 return Color.FromInt(m_ReticleOutlineColor.PackToInt());
695 Color GetReticleIlluminationColor()
697 return Color.FromInt(m_cReticleTextureIllumination.PackToInt());
702 protected void EnableReticleIllumination(
bool enable)
707 if (m_bHasIllumination && enable)
709 reticleTint = GetReticleIlluminationColor();
710 glowTint = GetReticleIlluminationColor();
714 reticleTint = GetReticleColor();
715 glowTint = GetReticleOutlineColor();
718 glowTint.SetA(m_fReticleTextureGlowAlpha * glowTint.A());
719 s_OnIlluminationChange.Invoke(reticleTint, glowTint);
723 protected float GetReticleOffsetYTarget()
729 float GetCurrentReticleOffsetY()
731 return m_fCurrentReticleOffsetY;
735 protected float GetCameraPitchTarget()
737 if (m_eZeroingType == SCR_EPIPZeroingType.EPZ_CAMERA_TURN)
744 float GetCurrentCameraPitchOffset()
746 return m_fCurrentCameraPitch;
750 vector GetCameraOffset()
752 return m_vCameraOffset;
756 vector GetCameraAngles()
758 return m_vCameraAngles;
767 IEntity GetParentToLocalTransform(out vector result[4], IEntity parent =
null)
770 Math3D.MatrixIdentity4(temp);
777 if (parent == lastNode)
780 vector localTransform[4];
781 lastNode.GetLocalTransform(localTransform);
784 TNodeId pivotIndex = lastNode.GetPivot();
785 if (pivotIndex != -1)
788 IEntity parentNode = lastNode.GetParent();
792 parentNode.GetAnimation().GetBoneMatrix(pivotIndex, pivotTM);
798 Math3D.MatrixMultiply4(pivotTM, localTransform, localTransform);
802 Math3D.MatrixMultiply4(localTransform, temp, result);
803 Math3D.MatrixCopy(result, temp);
805 lastNode = lastNode.GetParent();
808 Math3D.MatrixGetInverse4(temp, result);
819 IEntity GetCameraLocalTransform(out vector matrix[4])
823 vector yawPitchRoll = Vector(m_vCameraAngles[1], m_vCameraAngles[0] + m_fCurrentCameraPitch, m_vCameraAngles[2]);
826 Math3D.AnglesToMatrix(yawPitchRoll, matrix);
831 Math3D.MatrixCopy(matrix, temp);
834 IEntity lastNode = parent;
846 vector localTransform[4];
847 lastNode.GetLocalTransform(localTransform);
850 TNodeId pivotIndex = lastNode.GetPivot();
851 if (pivotIndex != -1)
854 IEntity parentNode = lastNode.GetParent();
858 parentNode.GetAnimation().GetBoneMatrix(pivotIndex, pivotTM);
864 Math3D.MatrixMultiply4(pivotTM, localTransform, localTransform);
867 Math3D.MatrixMultiply4(temp, localTransform, temp);
868 lastNode = lastNode.GetParent();
874 Math3D.MatrixCopy(temp, matrix);
882 void SetObjectiveFov(
float objectiveFov)
884 m_fObjectiveFov = objectiveFov;
888 void SetObjectiveScale(
float objectiveScale)
890 m_fObjectiveScale = objectiveScale;
894 void SetReticleBaseZoom(
float reticleBaseZoom)
896 m_fReticleBaseZoom = reticleBaseZoom;
900 void SetReticleAngularSize(
float reticleAngularSize)
902 m_fReticleAngularSize = reticleAngularSize;
906 void SetReticlePortion(
float reticlePortion)
908 m_fReticlePortion = reticlePortion;
912 void SetReticleOffsetX(
float reticleOffsetX)
914 m_fReticleOffsetX = reticleOffsetX;
918 void SetReticleOffsetY(
float reticleOffsetY)
920 m_fReticleOffsetY = reticleOffsetY;