1+ using System ;
2+ using System . Numerics ;
3+ using OpenTabletDriver . Plugin . Attributes ;
4+ using OpenTabletDriver . Plugin . Tablet ;
5+
6+ namespace TabletDriverFilters . Devocub
7+ {
8+ using static MathF ;
9+
10+ [ PluginName ( "TabletDriver AntiChatter Filter" ) ]
11+ public class AntiChatter : IFilter
12+ {
13+ private Vector2 _lastPos ;
14+ private float _timerInterval ;
15+ private const float _threshold = 0.9f ;
16+ private float _latency = 2.0f ;
17+
18+ public Vector2 Filter ( Vector2 point )
19+ {
20+ Vector2 calcTarget = new Vector2 ( ) ;
21+ float deltaX , deltaY , distance , weightModifier , predictionModifier ;
22+
23+ if ( _lastPos == null )
24+ {
25+ _lastPos = point ;
26+ return point ;
27+ }
28+
29+ if ( PredictionEnabled )
30+ {
31+ // Calculate predicted position onNewPacket
32+ if ( _lastPos . X != point . X || _lastPos . Y != point . Y )
33+ {
34+ // Calculate distance between last 2 packets and prediction
35+ deltaX = point . X - _lastPos . X ;
36+ deltaY = point . Y - _lastPos . Y ;
37+ distance = Sqrt ( deltaX * deltaX + deltaY * deltaY ) ;
38+ predictionModifier = 1 / Cosh ( ( distance - PredictionOffsetX ) * PredictionSharpness ) * PredictionStrength + PredictionOffsetY ;
39+
40+ // Apply prediction
41+ deltaX *= predictionModifier ;
42+ deltaY *= predictionModifier ;
43+
44+ // Update predicted position
45+ calcTarget . X = ( float ) ( point . X + deltaX ) ;
46+ calcTarget . Y = ( float ) ( point . Y + deltaY ) ;
47+
48+ // Update old position for further prediction
49+ _lastPos . X = point . X ;
50+ _lastPos . Y = point . Y ;
51+ }
52+ }
53+ else
54+ {
55+ calcTarget . X = point . X ;
56+ calcTarget . Y = point . Y ;
57+ }
58+
59+ deltaX = calcTarget . X - _lastPos . X ;
60+ deltaY = calcTarget . Y - _lastPos . Y ;
61+ distance = Sqrt ( deltaX * deltaX + deltaY * deltaY ) ;
62+
63+ float stepCount = Latency / TimerInterval ;
64+ float target = 1 - _threshold ;
65+ float weight = ( float ) ( 1.0 - ( 1.0 / Pow ( ( float ) ( 1.0 / target ) , ( float ) ( 1.0 / stepCount ) ) ) ) ;
66+
67+ // Devocub smoothing
68+ // Increase weight of filter in {formula} times
69+ weightModifier = ( float ) ( Pow ( distance + AntichatterOffsetX , AntichatterStrength * - 1 ) * AntichatterMultiplier ) ;
70+
71+ // Limit minimum
72+ if ( weightModifier + AntichatterOffsetY < 0 )
73+ weightModifier = 0 ;
74+ else
75+ weightModifier += AntichatterOffsetY ;
76+
77+ weightModifier = weight / weightModifier ;
78+ weightModifier = Math . Clamp ( weightModifier , 0 , 1 ) ;
79+ _lastPos . X += ( float ) ( deltaX * weightModifier ) ;
80+ _lastPos . Y += ( float ) ( deltaY * weightModifier ) ;
81+
82+ return _lastPos ;
83+ }
84+
85+ public FilterStage FilterStage => FilterStage . PostTranspose ;
86+
87+ [ SliderProperty ( "Latency" , 0f , 5f , 2f ) ]
88+ public float Latency
89+ {
90+ set => _latency = Math . Clamp ( value , 0 , 1000 ) ;
91+ get => _latency ;
92+ }
93+
94+ [ Property ( "Timer Interval" ) , Unit ( "hz" ) ]
95+ public float TimerInterval
96+ {
97+ set => _timerInterval = 1000f / value ;
98+ get => _timerInterval ;
99+ }
100+
101+ [ Property ( "Antichatter Strength" ) ]
102+ public float AntichatterStrength { set ; get ; } = 3 ;
103+
104+ [ Property ( "Antichatter Multiplier" ) ]
105+ public float AntichatterMultiplier { set ; get ; } = 1 ;
106+
107+ [ Property ( "Antichatter Offset X" ) ]
108+ public float AntichatterOffsetX { set ; get ; }
109+
110+ [ Property ( "Antichatter Offset Y" ) ]
111+ public float AntichatterOffsetY { set ; get ; } = 1 ;
112+
113+ [ BooleanProperty ( "Prediction" , "" ) ]
114+ public bool PredictionEnabled { set ; get ; }
115+
116+ [ Property ( "Prediction Strength" ) ]
117+ public float PredictionStrength { set ; get ; } = 1.1f ;
118+
119+ [ Property ( "Prediction Sharpness" ) ]
120+ public float PredictionSharpness { set ; get ; } = 1 ;
121+
122+ [ Property ( "Prediction Offset X" ) ]
123+ public float PredictionOffsetX { set ; get ; } = 3 ;
124+
125+ [ Property ( "Prediction Offset Y" ) ]
126+ public float PredictionOffsetY { set ; get ; } = 0.3f ;
127+ }
128+ }
0 commit comments