147 lines
5.5 KiB
C#
147 lines
5.5 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using UnityEngine;
|
|
|
|
namespace GazeLab.InputSystem
|
|
{
|
|
/// <summary>
|
|
/// Modular Tobii 4C Eye Tracker adapter.
|
|
/// Automatically binds to Tobii.Gaming.TobiiAPI.GetGazePoint() at runtime if the Tobii SDK is present,
|
|
/// while compiling cleanly before the SDK is imported. Also supports external PushSample() calls.
|
|
/// </summary>
|
|
public sealed class TobiiGazeInputAdapter : MonoBehaviour, IAimInputSource
|
|
{
|
|
[Header("Tobii 4C Gaze Filtering")]
|
|
[Tooltip("Grace window (seconds) before an invalid blink/sample drops tracking.")]
|
|
[Range(0.05f, 0.5f)] public float sampleTimeout = 0.25f;
|
|
|
|
[Tooltip("Exponential smoothing factor to reduce eye-tracking micro-saccade jitter.")]
|
|
[Range(0.05f, 1.0f)] public float gazeSmoothing = 0.28f;
|
|
|
|
[Tooltip("If true and no Tobii hardware is detected, falls back to Mouse cursor so testing never locks up.")]
|
|
public bool fallbackToMouseWhenUnavailable = true;
|
|
|
|
private Vector2 smoothedGazeScreenPos;
|
|
private bool hasInitializedSmoothPos;
|
|
private float lastValidSampleTime = float.NegativeInfinity;
|
|
private bool lastSampleValid;
|
|
|
|
// Cached reflection handles for Tobii.Gaming.TobiiAPI.GetGazePoint()
|
|
private bool reflectionChecked;
|
|
private MethodInfo getGazePointMethod;
|
|
private PropertyInfo isValidProperty;
|
|
private PropertyInfo isRecentProperty;
|
|
private PropertyInfo screenProperty;
|
|
|
|
public AimControlMode ControlMode => AimControlMode.TobiiEyeTracker;
|
|
public bool IsTobiiSdkDetected => getGazePointMethod != null;
|
|
|
|
private void Awake()
|
|
{
|
|
TryBindTobiiReflection();
|
|
}
|
|
|
|
private void TryBindTobiiReflection()
|
|
{
|
|
if (reflectionChecked) return;
|
|
reflectionChecked = true;
|
|
|
|
try
|
|
{
|
|
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
|
|
{
|
|
Type apiType = asm.GetType("Tobii.Gaming.TobiiAPI");
|
|
if (apiType != null)
|
|
{
|
|
getGazePointMethod = apiType.GetMethod("GetGazePoint", BindingFlags.Public | BindingFlags.Static);
|
|
if (getGazePointMethod != null)
|
|
{
|
|
Type gazePointType = getGazePointMethod.ReturnType;
|
|
isValidProperty = gazePointType.GetProperty("IsValid", BindingFlags.Public | BindingFlags.Instance);
|
|
isRecentProperty = gazePointType.GetProperty("IsRecent", BindingFlags.Public | BindingFlags.Instance);
|
|
screenProperty = gazePointType.GetProperty("Screen", BindingFlags.Public | BindingFlags.Instance);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
getGazePointMethod = null;
|
|
}
|
|
}
|
|
|
|
public void PushSample(Vector2 screenPixelPosition, bool valid)
|
|
{
|
|
if (valid)
|
|
{
|
|
if (!hasInitializedSmoothPos)
|
|
{
|
|
smoothedGazeScreenPos = screenPixelPosition;
|
|
hasInitializedSmoothPos = true;
|
|
}
|
|
else
|
|
{
|
|
smoothedGazeScreenPos = Vector2.Lerp(smoothedGazeScreenPos, screenPixelPosition, gazeSmoothing);
|
|
}
|
|
lastValidSampleTime = Time.unscaledTime;
|
|
lastSampleValid = true;
|
|
}
|
|
}
|
|
|
|
private void PollTobiiSdkIfAvailable()
|
|
{
|
|
if (getGazePointMethod == null) return;
|
|
|
|
try
|
|
{
|
|
object gazePointObj = getGazePointMethod.Invoke(null, null);
|
|
if (gazePointObj == null) return;
|
|
|
|
bool isValid = isValidProperty != null && (bool)isValidProperty.GetValue(gazePointObj, null);
|
|
if (isRecentProperty != null && isRecentProperty.GetIndexParameters().Length == 0)
|
|
{
|
|
isValid &= (bool)isRecentProperty.GetValue(gazePointObj, null);
|
|
}
|
|
|
|
if (isValid && screenProperty != null)
|
|
{
|
|
Vector2 rawScreen = (Vector2)screenProperty.GetValue(gazePointObj, null);
|
|
if (!float.IsNaN(rawScreen.x) && !float.IsNaN(rawScreen.y))
|
|
{
|
|
PushSample(rawScreen, true);
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Ignore runtime SDK exceptions
|
|
}
|
|
}
|
|
|
|
public bool TryGetScreenPosition(out Vector2 screenPixelPosition)
|
|
{
|
|
PollTobiiSdkIfAvailable();
|
|
|
|
bool withinGrace = lastSampleValid && (Time.unscaledTime - lastValidSampleTime) <= sampleTimeout;
|
|
if (withinGrace)
|
|
{
|
|
screenPixelPosition = smoothedGazeScreenPos;
|
|
return screenPixelPosition.x >= 0f && screenPixelPosition.x <= Screen.width &&
|
|
screenPixelPosition.y >= 0f && screenPixelPosition.y <= Screen.height;
|
|
}
|
|
|
|
if (fallbackToMouseWhenUnavailable)
|
|
{
|
|
Vector3 m = UnityEngine.Input.mousePosition;
|
|
screenPixelPosition = new Vector2(m.x, m.y);
|
|
return m.x >= 0f && m.x <= Screen.width && m.y >= 0f && m.y <= Screen.height;
|
|
}
|
|
|
|
screenPixelPosition = smoothedGazeScreenPos;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|