Skip to content

Commit f8168e0

Browse files
committed
Refactor eyebrow emulation into a base class
1 parent d91fa75 commit f8168e0

5 files changed

Lines changed: 68 additions & 67 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.Extensions.Logging;
2+
using VRCFaceTracking.Core.Params.Data;
3+
using VRCFaceTracking.Core.Params.Expressions;
4+
5+
namespace ETVRTrackingModule.ExpressionStrategies;
6+
7+
public class BaseParamMapper : IMappingStategy
8+
{
9+
protected ILogger _logger;
10+
protected readonly Config _config;
11+
12+
public BaseParamMapper(ILogger logger, ref Config config)
13+
{
14+
_logger = logger;
15+
_config = config;
16+
}
17+
18+
public virtual void handleOSCMessage(OSCMessage message) {}
19+
20+
protected static string GetParamToMap(string oscAddress)
21+
{
22+
var oscUrlSplit = oscAddress.Split("/");
23+
return oscUrlSplit[^1];
24+
}
25+
26+
private protected void _emulateEyeBrow(
27+
ref UnifiedExpressionShape[] eyeShapes,
28+
UnifiedExpressions eyebrowExpressionLowerrer,
29+
UnifiedExpressions eyebrowExpressionUpper,
30+
float baseEyeOpenness,
31+
float riseThreshold,
32+
float lowerThreshold)
33+
{
34+
if (!_config.ShouldEmulateEyebrows)
35+
return;
36+
37+
if (baseEyeOpenness >= riseThreshold)
38+
{
39+
eyeShapes[(int)eyebrowExpressionUpper].Weight = Utils.SmoothStep(
40+
riseThreshold,
41+
1,
42+
baseEyeOpenness
43+
);
44+
}
45+
46+
if (baseEyeOpenness <= lowerThreshold)
47+
{
48+
eyeShapes[(int)eyebrowExpressionLowerrer].Weight = Utils.SmoothStep(
49+
lowerThreshold,
50+
1,
51+
baseEyeOpenness
52+
);
53+
}
54+
}
55+
}

ExpressionStrategies/IExpressionMapper.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22

33
namespace ETVRTrackingModule.ExpressionStrategies;
44

5-
public interface ImappingStategy
5+
public interface IMappingStategy
66
{
7-
public static string GetParamToMap(string oscAddress)
8-
{
9-
var oscUrlSplit = oscAddress.Split("/");
10-
return oscUrlSplit[^1];
11-
}
12-
137
public void handleOSCMessage(OSCMessage message);
148
}

ExpressionStrategies/V1Mapper.cs

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace ETVRTrackingModule.ExpressionStrategies;
88

9-
public class V1Mapper : ImappingStategy
9+
public class V1Mapper : BaseParamMapper
1010
{
1111
private Dictionary<string, float> _parameterValues = new()
1212
{
@@ -16,19 +16,12 @@ public class V1Mapper : ImappingStategy
1616
{ "RightEyeX", 0f },
1717
{ "EyesY", 0f },
1818
};
19+
20+
public V1Mapper(ILogger logger, ref Config config) : base(logger, ref config) { }
1921

20-
private ILogger _logger;
21-
private readonly Config _config;
22-
23-
public V1Mapper(ILogger logger, ref Config config)
24-
{
25-
_logger = logger;
26-
_config = config;
27-
}
28-
29-
public void handleOSCMessage(OSCMessage message)
22+
public override void handleOSCMessage(OSCMessage message)
3023
{
31-
var paramToMap = ImappingStategy.GetParamToMap(message.address);
24+
var paramToMap = GetParamToMap(message.address);
3225
if (_parameterValues.ContainsKey(paramToMap))
3326
{
3427
_parameterValues[paramToMap] = message.value;
@@ -121,39 +114,4 @@ private void EmulateEyeBrows(ref UnifiedExpressionShape[] eyeShapes)
121114
_config.SqueezeThreshold
122115
);
123116
}
124-
125-
private void _emulateEyeBrow(
126-
ref UnifiedExpressionShape[] eyeShapes,
127-
UnifiedExpressions eyebrowExpressionLowerrer,
128-
UnifiedExpressions eyebrowExpressionUpper,
129-
float baseEyeOpenness,
130-
float widenThreshold,
131-
float squeezeThreshold)
132-
{
133-
if (!_config.ShouldEmulateEyebrows)
134-
return;
135-
136-
if (baseEyeOpenness >= widenThreshold)
137-
{
138-
eyeShapes[(int)eyebrowExpressionLowerrer].Weight = Utils.SmoothStep(
139-
widenThreshold,
140-
1,
141-
baseEyeOpenness
142-
);
143-
}
144-
145-
if (baseEyeOpenness <= squeezeThreshold)
146-
{
147-
eyeShapes[(int)eyebrowExpressionUpper].Weight = Utils.SmoothStep(
148-
squeezeThreshold,
149-
1,
150-
baseEyeOpenness
151-
);
152-
eyeShapes[(int)UnifiedExpressions.BrowLowererLeft].Weight = Utils.SmoothStep(
153-
squeezeThreshold,
154-
1,
155-
baseEyeOpenness
156-
);
157-
}
158-
}
159117
}

ExpressionStrategies/V2Mapper.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using Microsoft.Extensions.Logging;
22
using VRCFaceTracking;
33
using VRCFaceTracking.Core.Params.Data;
4+
using VRCFaceTracking.Core.Params.Expressions;
45
using VRCFaceTracking.Core.Types;
56

67
namespace ETVRTrackingModule.ExpressionStrategies;
78

8-
public class V2Mapper : ImappingStategy
9+
public class V2Mapper : BaseParamMapper
910
{
1011
private readonly string[] _singleEyeParamNames =
1112
{
@@ -28,18 +29,11 @@ public class V2Mapper : ImappingStategy
2829
{ "EyeLidRight", 1f },
2930
};
3031

31-
private ILogger _logger;
32-
private readonly Config _config;
32+
public V2Mapper(ILogger logger, ref Config config) : base(logger, ref config) { }
3333

34-
public V2Mapper(ILogger logger, ref Config config)
34+
public override void handleOSCMessage(OSCMessage message)
3535
{
36-
_config = config;
37-
_logger = logger;
38-
}
39-
40-
public void handleOSCMessage(OSCMessage message)
41-
{
42-
string paramToMap = ImappingStategy.GetParamToMap(message.address);
36+
string paramToMap = GetParamToMap(message.address);
4337
if (!_parameterValues.ContainsKey(paramToMap))
4438
return;
4539

ExpressionsMapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ETVRTrackingModule
55
{
66
public class ExpressionsMapper
77
{
8-
private ImappingStategy _mappingStrategy;
8+
private IMappingStategy _mappingStrategy;
99
private Config _config;
1010
ILogger _logger;
1111
public ExpressionsMapper(ILogger logger, ref ETVRConfigManager configManager)
@@ -19,7 +19,7 @@ public void MapMessage(OSCMessage msg)
1919
if (!msg.success)
2020
return;
2121

22-
var nextStrategy = IsV2Param(msg) ? (ImappingStategy) new V2Mapper(_logger, ref _config) : new V1Mapper(_logger, ref _config);
22+
var nextStrategy = IsV2Param(msg) ? (IMappingStategy) new V2Mapper(_logger, ref _config) : new V1Mapper(_logger, ref _config);
2323

2424
if (_mappingStrategy.GetType() != nextStrategy.GetType())
2525
{

0 commit comments

Comments
 (0)