66using VRCFaceTracking . Core . Library ;
77using VRCFaceTracking . Core . Params . Expressions ;
88using VRCFTPicoModule . Data ;
9- using VRCFTPicoModule . Utils ;
109
11- namespace VRCFTPicoModule
10+ namespace VRCFTPicoModule . Utils
1211{
13- public class Updater
12+ public class Updater ( )
1413 {
15- private readonly UdpClient udpClient ;
16- private readonly ILogger logger ;
17- private int timeOut = 0 ;
18- private float lastMouthLeft = 0f ;
19- private float lastMouthRight = 0f ;
20- private const float smoothingFactor = 0.5f ;
21- private bool isLegecy = false ;
22- public ModuleState moduleState ;
23-
24- public Updater ( UdpClient udpClient , ILogger logger , bool isLegecy )
14+ private readonly UdpClient ? _udpClient ;
15+ private readonly ILogger ? _logger ;
16+ private readonly bool _isLegacy ;
17+ private readonly ( bool , bool ) _trackingAvailable ;
18+
19+ public Updater ( UdpClient udpClient , ILogger logger , bool isLegacy , ( bool , bool ) trackingAvailable ) : this ( )
2520 {
26- this . udpClient = udpClient ;
27- this . logger = logger ;
28- this . isLegecy = isLegecy ;
21+ _udpClient = udpClient ;
22+ _logger = logger ;
23+ _isLegacy = isLegacy ;
24+ _trackingAvailable = trackingAvailable ;
2925 }
30-
31- public void Update ( )
26+
27+ private int _timeOut ;
28+ private float _lastMouthLeft ;
29+ private float _lastMouthRight ;
30+ private const float SmoothingFactor = 0.5f ;
31+ private ModuleState _moduleState ;
32+
33+ public void Update ( ModuleState state )
3234 {
33- if ( moduleState != ModuleState . Active ) return ;
35+ if ( _udpClient == null )
36+ return ;
37+
38+ if ( _logger == null )
39+ return ;
40+
41+ _udpClient . Client . ReceiveTimeout = 100 ;
42+ _moduleState = state ;
43+
44+ if ( _moduleState != ModuleState . Active ) return ;
3445
3546 try
3647 {
3748 var endPoint = new IPEndPoint ( IPAddress . Any , 0 ) ;
38- var data = udpClient . Receive ( ref endPoint ) ;
39- var pShape = ParseData ( data , isLegecy ) ;
40-
41- UpdateEye ( pShape ) ;
42- UpdateExpression ( pShape ) ;
49+ var data = _udpClient . Receive ( ref endPoint ) ;
50+ var pShape = ParseData ( data , _isLegacy ) ;
51+
52+ if ( _trackingAvailable . Item1 )
53+ UpdateEye ( pShape ) ;
54+
55+ if ( _trackingAvailable . Item2 )
56+ UpdateExpression ( pShape ) ;
4357 }
4458 catch ( SocketException ex ) when ( ex . SocketErrorCode == SocketError . TimedOut )
4559 {
46- if ( ++ timeOut > 600 )
60+ if ( ++ _timeOut > 600 )
4761 {
48- logger . LogWarning ( "Receive data timed out." ) ;
49- timeOut = 0 ;
62+ _logger . LogWarning ( "Receive data timed out." ) ;
63+ _timeOut = 0 ;
5064 }
5165 }
5266 catch ( Exception ex )
5367 {
54- logger . LogWarning ( "Update failed with exception: {0}" , ex ) ;
68+ _logger . LogWarning ( "Update failed with exception: {0}" , ex ) ;
5569 }
5670 }
5771
@@ -62,11 +76,11 @@ private static float[] ParseData(byte[] data, bool isLegacy)
6276
6377 if ( data . Length >= Marshal . SizeOf < DataPacket . DataPackHeader > ( ) + Marshal . SizeOf < DataPacket . DataPackBody > ( ) )
6478 {
65- var header = DataPacketHelpers . ByteArrayToStructure < DataPacket . DataPackHeader > ( data , 0 ) ;
79+ var header = DataPacketHelpers . ByteArrayToStructure < DataPacket . DataPackHeader > ( data ) ;
6680 if ( header . trackingType == 2 )
6781 return DataPacketHelpers . ByteArrayToStructure < DataPacket . DataPackBody > ( data , Marshal . SizeOf < DataPacket . DataPackHeader > ( ) ) . blendShapeWeight ;
6882 }
69- return Array . Empty < float > ( ) ;
83+ return [ ] ;
7084 }
7185
7286 private static void UpdateEye ( float [ ] pShape )
@@ -84,10 +98,7 @@ private static void UpdateEye(float[] pShape)
8498 eye . Right . Gaze . x = pShape [ ( int ) BlendShape . Index . EyeLookOut_R ] - pShape [ ( int ) BlendShape . Index . EyeLookIn_R ] ;
8599 eye . Right . Gaze . y = pShape [ ( int ) BlendShape . Index . EyeLookUp_R ] - pShape [ ( int ) BlendShape . Index . EyeLookDown_R ] ;
86100 #endregion
87- }
88-
89- private void UpdateExpression ( float [ ] pShape )
90- {
101+
91102 #region Brow
92103 SetParam ( pShape , BlendShape . Index . BrowInnerUp , UnifiedExpressions . BrowInnerUpLeft ) ;
93104 SetParam ( pShape , BlendShape . Index . BrowInnerUp , UnifiedExpressions . BrowInnerUpRight ) ;
@@ -105,7 +116,10 @@ private void UpdateExpression(float[] pShape)
105116 SetParam ( pShape , BlendShape . Index . EyeWide_L , UnifiedExpressions . EyeWideLeft ) ;
106117 SetParam ( pShape , BlendShape . Index . EyeWide_R , UnifiedExpressions . EyeWideRight ) ;
107118 #endregion
119+ }
108120
121+ private void UpdateExpression ( float [ ] pShape )
122+ {
109123 #region Jaw
110124 SetParam ( pShape , BlendShape . Index . JawOpen , UnifiedExpressions . JawOpen ) ;
111125 SetParam ( pShape , BlendShape . Index . JawLeft , UnifiedExpressions . JawLeft ) ;
@@ -118,11 +132,11 @@ private void UpdateExpression(float[] pShape)
118132 SetParam ( pShape , BlendShape . Index . CheekSquint_L , UnifiedExpressions . CheekSquintLeft ) ;
119133 SetParam ( pShape , BlendShape . Index . CheekSquint_R , UnifiedExpressions . CheekSquintRight ) ;
120134
121- float mouthLeft = SmoothValue ( pShape [ ( int ) BlendShape . Index . MouthLeft ] , ref lastMouthLeft ) ;
122- float mouthRight = SmoothValue ( pShape [ ( int ) BlendShape . Index . MouthRight ] , ref lastMouthRight ) ;
135+ var mouthLeft = SmoothValue ( pShape [ ( int ) BlendShape . Index . MouthLeft ] , ref _lastMouthLeft ) ;
136+ var mouthRight = SmoothValue ( pShape [ ( int ) BlendShape . Index . MouthRight ] , ref _lastMouthRight ) ;
123137
124- float cheekPuff = pShape [ ( int ) BlendShape . Index . CheekPuff ] ;
125- float diffThreshold = 0.1f ;
138+ var cheekPuff = pShape [ ( int ) BlendShape . Index . CheekPuff ] ;
139+ const float diffThreshold = 0.1f ;
126140
127141 if ( cheekPuff > 0.1f )
128142 {
@@ -195,13 +209,13 @@ private void UpdateExpression(float[] pShape)
195209 #endregion
196210
197211 #region Tongue
198- SetParam ( pShape , BlendShape . Index . TongueOut , UnifiedExpressions . TongueOut ) ;
212+ SetParam ( pShape [ ( int ) BlendShape . Index . TongueOut ] > 0f ? 1f : 0f , UnifiedExpressions . TongueOut ) ;
199213 #endregion
200214 }
201215
202- private float SmoothValue ( float newValue , ref float lastValue )
216+ private static float SmoothValue ( float newValue , ref float lastValue )
203217 {
204- lastValue += ( newValue - lastValue ) * smoothingFactor ;
218+ lastValue += ( newValue - lastValue ) * SmoothingFactor ;
205219 return lastValue ;
206220 }
207221
0 commit comments