-
Notifications
You must be signed in to change notification settings - Fork 1
/
VRCPimaxEyeTracker.cs
228 lines (192 loc) · 10 KB
/
VRCPimaxEyeTracker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using VRCPimaxEyeTracker;
using MelonLoader;
using UnityEngine;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using VRC.SDK3.Avatars.ScriptableObjects;
[assembly: MelonInfo(typeof(VRCPimaxEyeTrackerMod), "VRCPimaxEyeTracker", "1.0.0", "Genesis", "https://github.com/NGenesis/VRCPimaxEyeTracker")]
[assembly: MelonGame("VRChat", "VRChat")]
namespace VRCPimaxEyeTracker
{
public static class DependencyManager
{
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr LoadLibrary(string lpFileName);
public static bool Init() => LoadDLL("VRCPimaxEyeTracker.PimaxEyeTracker", "PimaxEyeTracker.dll");
private static bool LoadDLL(string resourcePath, string dllName)
{
var dllPath = Path.Combine(Path.GetTempPath(), dllName);
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath + "." + dllName))
{
if (resource == null) return false;
using (Stream dllFile = File.Create(dllPath))
{
resource.Seek(0, SeekOrigin.Begin);
resource.CopyTo(dllFile);
}
}
return LoadLibrary(dllPath) != IntPtr.Zero;
}
}
public static class ExpressionParameterManager
{
private static AvatarAnimParamController AnimParamController => VRCPlayer.field_Internal_Static_VRCPlayer_0?.field_Private_AnimatorControllerManager_0?.field_Private_AvatarAnimParamController_0;
private static AvatarPlayableController PlayableController => AnimParamController?.field_Private_AvatarPlayableController_0;
private static VRCExpressionParameters.Parameter[] ExpressionParameterDescriptors => VRCPlayer.field_Internal_Static_VRCPlayer_0?.prop_VRCAvatarManager_0?.prop_VRCAvatarDescriptor_0?.expressionParameters?.parameters;
private static UnhollowerBaseLib.Il2CppReferenceArray<AvatarPlayableController.ObjectNPublicInObInPaInUnique> AnimatorParameters => PlayableController?.field_Private_ArrayOf_ObjectNPublicInObInPaInUnique_0;
public static void SetParameter(string name, float value, bool prioritise = false)
{
if (ExpressionParameterDescriptors == null || PlayableController == null) return;
for (var i = 0; i < ExpressionParameterDescriptors.Length; ++i)
{
var expressionParameter = ExpressionParameterDescriptors[i];
if (expressionParameter.name == name)
{
switch(expressionParameter.valueType) {
case VRCExpressionParameters.ValueType.Bool:
value = value <= float.Epsilon ? 0.0f : 1.0f;
break;
case VRCExpressionParameters.ValueType.Int:
value = (int)value;
break;
}
PlayableController?.Method_Public_Boolean_Int32_Single_0(i, value); // Set parameter at index
if (prioritise) PlayableController?.Method_Public_Void_Int32_0(i); // Set parameter priority
return;
}
}
}
public static void SetParameter(string name, bool value, bool prioritise = false) => SetParameter(name, value ? 1.0f : 0.0f, prioritise);
public static void SetParameter(string name, int value, bool prioritise = false) => SetParameter(name, (float)value, prioritise);
public static bool GetBoolParameter(string paramName)
{
if (AnimatorParameters != null && !string.IsNullOrEmpty(paramName))
{
foreach (var animatorParameter in AnimatorParameters)
{
VRCExpressionParameters.Parameter expressionParameter = animatorParameter.field_Public_Parameter_0;
if (expressionParameter.name == paramName && expressionParameter.valueType == VRCExpressionParameters.ValueType.Bool) return animatorParameter?.field_Public_AvatarParameter_0?.prop_Boolean_0 ?? false;
}
}
return false;
}
public static float GetFloatParameter(string paramName)
{
if (AnimatorParameters != null && !string.IsNullOrEmpty(paramName))
{
foreach (var animatorParameter in AnimatorParameters)
{
VRCExpressionParameters.Parameter expressionParameter = animatorParameter.field_Public_Parameter_0;
if (expressionParameter.name == paramName && expressionParameter.valueType == VRCExpressionParameters.ValueType.Float) return animatorParameter?.field_Public_AvatarParameter_0?.prop_Single_0 ?? 0.0f;
}
}
return 0.0f;
}
public static int GetIntParameter(string paramName)
{
if (AnimatorParameters != null && !string.IsNullOrEmpty(paramName))
{
foreach (var animatorParameter in AnimatorParameters)
{
VRCExpressionParameters.Parameter expressionParameter = animatorParameter.field_Public_Parameter_0;
if (expressionParameter.name == paramName && expressionParameter.valueType == VRCExpressionParameters.ValueType.Int) return animatorParameter?.field_Public_AvatarParameter_0?.prop_Int32_0 ?? 0;
}
}
return 0;
}
}
public class VRCPimaxEyeTrackerMod : MelonMod
{
private static Pimax.EyeTracking.EyeTracker eyeTracker;
private static bool isChangingActiveStatus = false;
private static bool needsExpressionUpdate = false;
private static DateTime changeActiveStateTimer;
private const float TIMER_CHANGE_ACTIVE_STATE = 3.0f;
public override void OnApplicationStart()
{
DependencyManager.Init();
eyeTracker = new Pimax.EyeTracking.EyeTracker();
eyeTracker.OnUpdate += OnEyeTrackerUpdate;
eyeTracker.OnStart += OnEyeTrackerStart;
eyeTracker.OnStop += OnEyeTrackerStop;
MelonCoroutines.Start(UpdateEyeTrackerState());
}
public override void OnApplicationQuit()
{
if (eyeTracker?.Active ?? false) eyeTracker.Stop();
}
private static void OnEyeTrackerStart()
{
MelonLogger.Msg("Eye Tracker Started");
isChangingActiveStatus = false;
}
private static void OnEyeTrackerStop()
{
MelonLogger.Msg("Eye Tracker Stopped");
isChangingActiveStatus = false;
}
private static void OnEyeTrackerUpdate()
{
needsExpressionUpdate = true;
}
private static void UpdateExpressionParameters()
{
if (eyeTracker?.Active ?? false)
{
ExpressionParameterManager.SetParameter("LeftEyeBlink", eyeTracker.LeftEye.Expression.Blink, true);
ExpressionParameterManager.SetParameter("RightEyeBlink", eyeTracker.RightEye.Expression.Blink, true);
ExpressionParameterManager.SetParameter("LeftEyeLid", eyeTracker.LeftEye.Expression.Openness, true);
ExpressionParameterManager.SetParameter("RightEyeLid", eyeTracker.RightEye.Expression.Openness, true);
ExpressionParameterManager.SetParameter("LeftEyeX", eyeTracker.LeftEye.Expression.PupilCenter.x);
ExpressionParameterManager.SetParameter("RightEyeX", eyeTracker.RightEye.Expression.PupilCenter.x);
ExpressionParameterManager.SetParameter("EyesX", eyeTracker.RightEye.Expression.Blink ? eyeTracker.LeftEye.Expression.PupilCenter.x : eyeTracker.RightEye.Expression.PupilCenter.x);
ExpressionParameterManager.SetParameter("LeftEyeY", eyeTracker.LeftEye.Expression.PupilCenter.y);
ExpressionParameterManager.SetParameter("RightEyeY", eyeTracker.RightEye.Expression.PupilCenter.y);
ExpressionParameterManager.SetParameter("EyesY", eyeTracker.RightEye.Expression.Blink ? eyeTracker.LeftEye.Expression.PupilCenter.y : eyeTracker.RightEye.Expression.PupilCenter.y);
}
}
private static IEnumerator UpdateEyeTrackerState()
{
while (true)
{
// Start or stop eye tracker from avatar quick menu
var useEyeTracker = ExpressionParameterManager.GetBoolParameter("UseEyeTracker");
var isEyeTrackerActive = eyeTracker?.Active ?? false;
if (isEyeTrackerActive && needsExpressionUpdate)
{
needsExpressionUpdate = false;
UpdateExpressionParameters();
}
if (!isChangingActiveStatus)
{
if (isEyeTrackerActive && !useEyeTracker)
{
MelonLogger.Msg("Eye Tracker Stopping");
isChangingActiveStatus = true;
changeActiveStateTimer = DateTime.Now;
eyeTracker.Stop();
}
else if (!isEyeTrackerActive && useEyeTracker)
{
MelonLogger.Msg("Eye Tracker Starting");
isChangingActiveStatus = eyeTracker.Start();
changeActiveStateTimer = DateTime.Now;
}
}
else
{
if ((DateTime.Now - changeActiveStateTimer).TotalSeconds >= TIMER_CHANGE_ACTIVE_STATE)
{
MelonLogger.Msg("Eye Tracker State Change Timed Out");
isChangingActiveStatus = false;
ExpressionParameterManager.SetParameter("UseEyeTracker", isEyeTrackerActive, true);
}
}
yield return new WaitForSeconds(isEyeTrackerActive ? 0.01f : 0.5f);
}
}
}
}