1
1
namespace BFI_VRCFT_Module
2
2
{
3
3
using Microsoft . Extensions . Logging ;
4
+ using System . Text . Json ;
5
+ using System . Text . Json . Serialization ;
4
6
using VRCFaceTracking ;
5
7
using VRCFaceTracking . Core . Library ;
6
8
using VRCFaceTracking . Core . Params . Data ;
9
11
10
12
public class BFI_VRCFT_Module : ExtTrackingModule
11
13
{
12
- public static bool debug = true ;
14
+
15
+ private static string tagEyeClosed = "eyeclosed" ;
16
+ private static string tagSmile = "smile" ;
17
+ private static string tagFrown = "frown" ;
18
+ private static string tagAnger = "anger" ;
19
+ private static string tagCringe = "cringe" ;
20
+ private static string tagCheekPuff = "cheekpuff" ;
21
+ private static string tagApeShape = "apeshape" ;
22
+
23
+ public static bool debug = false ;
13
24
OscReceiver reciever = new OscReceiver ( 8999 ) ;
14
25
15
26
UnifiedExpressionShape frown = new UnifiedExpressionShape ( ) ;
16
27
UnifiedExpressionShape mouthUpperUp = new UnifiedExpressionShape ( ) ;
17
28
UnifiedExpressionShape mouthLowerDown = new UnifiedExpressionShape ( ) ;
18
29
UnifiedExpressionShape MouthStretch = new UnifiedExpressionShape ( ) ;
30
+ UnifiedExpressionShape browDown = new UnifiedExpressionShape ( ) ;
31
+ UnifiedExpressionShape cheekPuff = new UnifiedExpressionShape ( ) ;
32
+ UnifiedExpressionShape apeShape = new UnifiedExpressionShape ( ) ;
19
33
20
34
21
- UnifiedExpressionShape browDown = new UnifiedExpressionShape ( ) ;
22
35
// What your interface is able to send as tracking data.
23
36
public override ( bool SupportsEye , bool SupportsExpression ) Supported => ( true , true ) ;
24
37
@@ -40,7 +53,38 @@ public override (bool eyeSuccess, bool expressionSuccess) Initialize(bool eyeAva
40
53
ModuleInformation . StaticImages = stream != null ? new List < Stream > { stream } : ModuleInformation . StaticImages ;
41
54
if ( debug ) Logger . LogInformation ( "is stream to picture null: " + ( stream == null ) . ToString ( ) ) ;
42
55
//... Initializing module. Modify state tuple as needed (or use bool contexts to determine what should be initialized).
56
+
57
+
58
+ //parsing json file for expressions
59
+ try
60
+ {
61
+
62
+ JsonParser parser = new JsonParser ( ) ;
63
+ SupportedExpressions expressions = parser . ParseJson ( ) ;
64
+ reciever . expressions = expressions ;
65
+ if ( expressions != null && expressions . Expressions != null )
66
+ {
67
+ if ( expressions . Expressions != null )
68
+ {
69
+ foreach ( var expression in expressions . Expressions )
70
+ {
71
+ Logger . LogInformation ( $ "Expression: { expression . Key } , Id: { expression . Value . Id } , Weight: { expression . Value . Weight } ") ;
72
+ }
73
+ }
74
+ }
75
+ else
76
+ {
77
+ Logger . LogInformation ( $ "No expressions found in the JSON file") ;
78
+ }
79
+ }
80
+ catch ( Exception ex )
81
+ {
82
+ Logger . LogInformation ( $ "Error parsing JSON file: { ex . Message } ") ;
83
+ return ( false , false ) ;
84
+ }
43
85
return state ;
86
+
87
+
44
88
}
45
89
46
90
// Polls data from the tracking interface.
@@ -57,7 +101,8 @@ public override void Update()
57
101
58
102
if ( debug ) Logger . LogInformation ( reciever . OSCDebugData ) ;
59
103
60
- UpdateValues ( ) ;
104
+ //UpdateValues();
105
+ UpdateValuesExpressions ( ) ;
61
106
62
107
if ( reciever . EvaluateTimout ( ) )
63
108
{
@@ -72,8 +117,19 @@ public override void Update()
72
117
UnifiedTracking . Data . Eye . Left . Gaze = new Vector2 ( 0 , 0 ) ;
73
118
UnifiedTracking . Data . Eye . Right . Gaze = new Vector2 ( 0 , 0 ) ;
74
119
75
- UnifiedTracking . Data . Eye . Left . Openness = 1 - reciever . eyeClosed ;
76
- UnifiedTracking . Data . Eye . Right . Openness = 1 - reciever . eyeClosed ;
120
+ if ( reciever . expressions . Expressions . ContainsKey ( tagEyeClosed ) )
121
+ {
122
+
123
+ UnifiedTracking . Data . Eye . Left . Openness = 1 - reciever . expressions . Expressions [ tagEyeClosed ] . Weight ;
124
+ UnifiedTracking . Data . Eye . Right . Openness = 1 - reciever . expressions . Expressions [ tagEyeClosed ] . Weight ;
125
+
126
+ }
127
+ else
128
+ {
129
+
130
+ UnifiedTracking . Data . Eye . Left . Openness = 1f ;
131
+ UnifiedTracking . Data . Eye . Right . Openness = 1f ;
132
+ }
77
133
}
78
134
79
135
UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . MouthFrownRight ] = frown ;
@@ -90,6 +146,12 @@ public override void Update()
90
146
UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . MouthStretchLeft ] = MouthStretch ;
91
147
UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . MouthStretchRight ] = MouthStretch ;
92
148
149
+ UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . CheekPuffRight ] = cheekPuff ;
150
+ UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . CheekPuffLeft ] = cheekPuff ;
151
+
152
+ UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . JawOpen ] = apeShape ;
153
+ UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . MouthClosed ] = apeShape ;
154
+
93
155
}
94
156
95
157
// Add a delay or halt for the next update cycle for performance. eg:
@@ -109,6 +171,60 @@ private void UpdateValues()
109
171
MouthStretch . Weight = reciever . cringe ;
110
172
}
111
173
174
+ private void UpdateValuesExpressions ( )
175
+ {
176
+ try
177
+ {
178
+
179
+ if ( reciever . expressions . Expressions . ContainsKey ( tagSmile ) )
180
+ {
181
+ frown . Weight = ( - reciever . expressions . Expressions [ tagSmile ] . Weight ) ;
182
+ mouthUpperUp . Weight = reciever . expressions . Expressions [ tagSmile ] . Weight ;
183
+ mouthLowerDown . Weight = reciever . expressions . Expressions [ tagSmile ] . Weight ;
184
+ }
185
+ if ( reciever . expressions . Expressions . ContainsKey ( tagFrown ) )
186
+ {
187
+ if ( reciever . expressions . Expressions . ContainsKey ( tagSmile ) )
188
+ {
189
+ frown . Weight = ( - reciever . expressions . Expressions [ tagSmile ] . Weight ) + reciever . expressions . Expressions [ tagFrown ] . Weight ;
190
+ }
191
+ else
192
+ {
193
+ frown . Weight = reciever . expressions . Expressions [ tagFrown ] . Weight ;
194
+ }
195
+ }
196
+ if ( reciever . expressions . Expressions . ContainsKey ( tagCringe ) )
197
+ {
198
+ if ( reciever . expressions . Expressions . ContainsKey ( tagSmile ) )
199
+ {
200
+ mouthLowerDown . Weight = Math . Clamp ( reciever . expressions . Expressions [ tagSmile ] . Weight + reciever . expressions . Expressions [ tagCringe ] . Weight , 0 , 1 ) ;
201
+ }
202
+ else
203
+ {
204
+ mouthLowerDown . Weight = reciever . expressions . Expressions [ tagCringe ] . Weight ;
205
+ }
206
+ MouthStretch . Weight = reciever . expressions . Expressions [ tagCringe ] . Weight ;
207
+ }
208
+ if ( reciever . expressions . Expressions . ContainsKey ( tagAnger ) )
209
+ {
210
+ browDown . Weight = reciever . expressions . Expressions [ tagAnger ] . Weight ;
211
+ }
212
+ if ( reciever . expressions . Expressions . ContainsKey ( tagCheekPuff ) )
213
+ {
214
+ cheekPuff . Weight = reciever . expressions . Expressions [ tagCheekPuff ] . Weight ;
215
+ }
216
+ if ( reciever . expressions . Expressions . ContainsKey ( tagApeShape ) )
217
+ {
218
+ apeShape . Weight = reciever . expressions . Expressions [ tagCheekPuff ] . Weight ;
219
+ }
220
+
221
+ }
222
+ catch ( Exception ex )
223
+ {
224
+ Logger . LogInformation ( $ "Error trying to acces values: { ex . Message } ") ;
225
+ }
226
+ }
227
+
112
228
// Called when the module is unloaded or VRCFaceTracking itself tears down.
113
229
public override void Teardown ( )
114
230
{
@@ -123,6 +239,7 @@ public override void Teardown()
123
239
mouthLowerDown . Weight = 0 ;
124
240
browDown . Weight = 0 ;
125
241
MouthStretch . Weight = 0 ;
242
+ cheekPuff . Weight = 0 ;
126
243
127
244
UnifiedTracking . Data . Eye . Left . Openness = 1 ;
128
245
UnifiedTracking . Data . Eye . Right . Openness = 1 ;
@@ -140,6 +257,10 @@ public override void Teardown()
140
257
141
258
UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . MouthStretchLeft ] = MouthStretch ;
142
259
UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . MouthStretchRight ] = MouthStretch ;
260
+
261
+ UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . CheekPuffRight ] = cheekPuff ;
262
+ UnifiedTracking . Data . Shapes [ ( int ) UnifiedExpressions . CheekPuffLeft ] = cheekPuff ;
263
+
143
264
}
144
265
145
266
float map ( float x , float in_min , float in_max , float out_min , float out_max )
@@ -148,4 +269,5 @@ float map(float x, float in_min, float in_max, float out_min, float out_max)
148
269
}
149
270
150
271
}
272
+
151
273
}
0 commit comments