Rozšířené sledování očí v Unity

Přístup k úložišti GitHub pro ukázku rozšířeného sledování očí:

Rozšířené sledování očí je novou funkcí v HoloLens 2. Je to nadmnožina standardního sledování očí, která poskytuje pouze kombinovaná data o pohledu. Rozšířené sledování očí také poskytuje data o jednotlivých pohledech a umožňuje aplikacím nastavit různé snímkové frekvence pro data pohledu, například 30, 60 a 90 snímků za sekundu. HoloLens 2 v tuto chvíli nepodporují jiné funkce, jako je například otevřenost očí a vzruchy očí.

Sada Extended Eye Tracking SDK umožňuje aplikacím přístup k datům a funkcím rozšířeného sledování očí. Dá se použít společně s rozhraními OpenXR API nebo staršími rozhraními WinRT API.

Tento článek popisuje způsoby použití rozšířené sady SDK pro sledování očí v Unity společně s modulem plug-in Mixed Reality OpenXR.

Nastavení projektu

  1. Nastavte projekt Unity pro vývoj v HoloLensu.
    • Výběr funkce Gaze Input
  2. Importujte modul plug-in Mixed Reality OpenXR z nástroje funkcí MRTK.
  3. Naimportujte balíček NuGet sady Eye Tracking SDK do projektu Unity.
    1. Stáhněte a nainstalujte balíček NuGetForUnity .
    2. V editoru Unity přejděte na NuGeta>Manage NuGet Packages vyhledejte Microsoft.MixedReality.EyeTracking
    3. Kliknutím na tlačítko Nainstalovat naimportujte nejnovější verzi balíčku NuGet.
      Snímek obrazovky s balíčkem Nuget sdk pro sledování očí
  4. Přidejte pomocné skripty Unity.
    1. Přidejte skript odsudExtendedEyeGazeDataProvider.cs do projektu Unity.
    2. Vytvořte scénu a pak připojte skript k libovolnému objektu ExtendedEyeGazeDataProvider.cs GameObject.
  5. Využívání funkcí ExtendedEyeGazeDataProvider.cs a implementace logiky
  6. Sestavení a nasazení do HoloLensu

Využití funkcí ExtendedEyeGazeDataProvider

Poznámka

Skript ExtendedEyeGazeDataProvider závisí na některých rozhraních API z modulu plug-in Mixed Reality OpenXR pro převod souřadnic dat pohledu. Nemůže fungovat, pokud váš projekt Unity používá zastaralý modul plug-in Windows XR nebo starší integrovaný modul XR ve starší verzi Unity. Pokud chcete, aby rozšířené sledování očí fungovalo i v těchto scénářích:

  • Pokud potřebujete jenom přístup k nastavení snímkové frekvence, Mixed Reality modul plug-in OpenXR není nutný a mohli byste ho ExtendedEyeGazeDataProvider upravit tak, aby se zachovala pouze logika související s snímkovou frekvencí.
  • Pokud stále potřebujete přístup k datům jednotlivých pohledů z očí, musíte v Unity použít rozhraní API WinRT. Pokud chcete zjistit, jak používat rozšířenou sadu SDK pro sledování očí s rozhraními WinRT API, přečtěte si část Viz také.

Třída ExtendedEyeGazeDataProvider zabalí rozšířená rozhraní SDK pro sledování očí. Poskytuje funkce pro čtení pohledu v prostoru Světa Unity nebo vzhledem k hlavní kameře.

Tady jsou ukázky kódu, které je potřeba využít ExtendedEyeGazeDataProvider k získání dat pohledu.

ExtendedEyeGazeDataProvider extendedEyeGazeDataProvider;
void Update() {
    timestamp = DateTime.Now;

    var leftGazeReadingInWorldSpace = extendedEyeGazeDataProvider.GetWorldSpaceGazeReading(extendedEyeGazeDataProvider.GazeType.Left, timestamp);
    var rightGazeReadingInWorldSpace = extendedEyeGazeDataProvider.GetWorldSpaceGazeReading(extendedEyeGazeDataProvider.GazeType.Right, timestamp);
    var combinedGazeReadingInWorldSpace = extendedEyeGazeDataProvider.GetWorldSpaceGazeReading(extendedEyeGazeDataProvider.GazeType.Combined, timestamp);

    var combinedGazeReadingInCameraSpace = extendedEyeGazeDataProvider.GetCameraSpaceGazeReading(extendedEyeGazeDataProvider.GazeType.Combined, timestamp);
}

Když se ExtendedEyeGazeDataProvider skript spustí, nastaví snímkovou frekvenci dat pohledu na nejvyšší možnost, což je aktuálně 90 snímků za sekundu.

Referenční informace k rozhraní API sady SDK pro rozšířené sledování očí

Kromě použití ExtendedEyeGazeDataProvider skriptu můžete také vytvořit vlastní skript pro přímé využívání rozhraní API sady SDK.

namespace Microsoft.MixedReality.EyeTracking
{
    /// <summary>
    /// Allow discovery of Eye Gaze Trackers connected to the system
    /// This is the only class from the Extended Eye Tracking SDK that the application will instantiate, 
    /// other classes' instances will be returned by method calls or properties.
    /// </summary>
    public class EyeGazeTrackerWatcher
    {
        /// <summary>
        /// Constructs an instance of the watcher
        /// </summary>
        public EyeGazeTrackerWatcher();

        /// <summary>
        /// Starts trackers enumeration.
        /// </summary>
        /// <returns>Task representing async action; completes when the initial enumeration is completed</returns>
        public System.Threading.Tasks.Task StartAsync();

        /// <summary>
        /// Stop listening to trackers additions and removal
        /// </summary>
        public void Stop();

        /// <summary>
        /// Raised when an Eye Gaze tracker is connected
        /// </summary>
        public event System.EventHandler<EyeGazeTracker> EyeGazeTrackerAdded;

        /// <summary>
        /// Raised when an Eye Gaze tracker is disconnected
        /// </summary>
        public event System.EventHandler<EyeGazeTracker> EyeGazeTrackerRemoved;        
    }

    /// <summary>
    /// Represents an Eye Tracker device
    /// </summary>
    public class EyeGazeTracker
    {
        /// <summary>
        /// True if Restricted mode is supported, which means the driver supports providing individual 
        /// eye gaze vector and frame rate 
        /// </summary>
        public bool IsRestrictedModeSupported;

        /// <summary>
        /// True if Vergence Distance is supported by tracker
        /// </summary>
        public bool IsVergenceDistanceSupported;

        /// <summary>
        /// True if Eye Openness is supported by the driver
        /// </summary>
        public bool IsEyeOpennessSupported;

        /// <summary>
        /// True if individual gazes are supported
        /// </summary>
        public bool AreLeftAndRightGazesSupported;

        /// <summary>
        /// Get the supported target frame rates of the tracker
        /// </summary>
        public System.Collections.Generic.IReadOnlyList<EyeGazeTrackerFrameRate> SupportedTargetFrameRates;

        /// <summary>
        /// NodeId of the tracker, used to retrieve a SpatialLocator or SpatialGraphNode to locate the tracker in the scene
        /// for the Perception API, use SpatialGraphInteropPreview.CreateLocatorForNode
        /// for the Mixed Reality OpenXR API, use SpatialGraphNode.FromDynamicNodeId
        /// </summary>
        public Guid TrackerSpaceLocatorNodeId;

        /// <summary>
        /// Opens the tracker
        /// </summary>
        /// <param name="restrictedMode">True if restricted mode active</param>
        /// <returns>Task representing async action; completes when the initial enumeration is completed</returns>
        public System.Threading.Tasks.Task OpenAsync(bool restrictedMode);

        /// <summary>
        /// Closes the tracker
        /// </summary>
        public void Close();

        /// <summary>
        /// Changes the target frame rate of the tracker
        /// </summary>
        /// <param name="newFrameRate">Target frame rate</param>
        public void SetTargetFrameRate(EyeGazeTrackerFrameRate newFrameRate);

        /// <summary>
        /// Try to get tracker state at a given timestamp
        /// </summary>
        /// <param name="timestamp">timestamp</param>
        /// <returns>State if available, null otherwise</returns>
        public EyeGazeTrackerReading TryGetReadingAtTimestamp(DateTime timestamp);

        /// <summary>
        /// Try to get tracker state at a system relative time
        /// </summary>
        /// <param name="time">time</param>
        /// <returns>State if available, null otherwise</returns>
        public EyeGazeTrackerReading TryGetReadingAtSystemRelativeTime(TimeSpan time);

        /// <summary>
        /// Try to get first first tracker state after a given timestamp
        /// </summary>
        /// <param name="timestamp">timestamp</param>
        /// <returns>State if available, null otherwise</returns>
        public EyeGazeTrackerReading TryGetReadingAfterTimestamp(DateTime timestamp);

        /// <summary>
        /// Try to get the first tracker state after a system relative time
        /// </summary>
        /// <param name="time">time</param>
        /// <returns>State if available, null otherwise</returns>
        public EyeGazeTrackerReading TryGetReadingAfterSystemRelativeTime(TimeSpan time);
    }

    /// <summary>
    /// Represents a frame rate supported by an Eye Tracker
    /// </summary>
    public class EyeGazeTrackerFrameRate
    {
        /// <summary>
        /// Frames per second of the frame rate
        /// </summary>
        public UInt32 FramesPerSecond;
    }

    /// <summary>
    /// Snapshot of Gaze Tracker state
    /// </summary>
    public class EyeGazeTrackerReading
    {
        /// <summary>
        /// Timestamp of state
        /// </summary>
        public DateTime Timestamp;

        /// <summary>
        /// Timestamp of state as system relative time
        /// Its SystemRelativeTime.Ticks could provide the QPC time to locate tracker pose 
        /// </summary>
        public TimeSpan SystemRelativeTime;

        /// <summary>
        /// Indicates of user calibration is valid
        /// </summary>
        public bool IsCalibrationValid;

        /// <summary>
        /// Tries to get a vector representing the combined gaze related to the tracker's node
        /// </summary>
        /// <param name="origin">Origin of the gaze vector</param>
        /// <param name="direction">Direction of the gaze vector</param>
        /// <returns></returns>
        public bool TryGetCombinedEyeGazeInTrackerSpace(out System.Numerics.Vector3 origin, out System.Numerics.Vector3 direction);

        /// <summary>
        /// Tries to get a vector representing the left eye gaze related to the tracker's node
        /// </summary>
        /// <param name="origin">Origin of the gaze vector</param>
        /// <param name="direction">Direction of the gaze vector</param>
        /// <returns></returns>
        public bool TryGetLeftEyeGazeInTrackerSpace(out System.Numerics.Vector3 origin, out System.Numerics.Vector3 direction);

        /// <summary>
        /// Tries to get a vector representing the right eye gaze related to the tracker's node position
        /// </summary>
        /// <param name="origin">Origin of the gaze vector</param>
        /// <param name="direction">Direction of the gaze vector</param>
        /// <returns></returns>
        public bool TryGetRightEyeGazeInTrackerSpace(out System.Numerics.Vector3 origin, out System.Numerics.Vector3 direction);

        /// <summary>
        /// Tries to read vergence distance
        /// </summary>
        /// <param name="value">Vergence distance if available</param>
        /// <returns>bool if value is valid</returns>
        public bool TryGetVergenceDistance(out float value);

        /// <summary>
        /// Tries to get left Eye openness information
        /// </summary>
        /// <param name="value">Eye Openness if valid</param>
        /// <returns>bool if value is valid</returns>
        public bool TryGetLeftEyeOpenness(out float value);

        /// <summary>
        /// Tries to get right Eye openness information
        /// </summary>
        /// <param name="value">Eye openness if valid</param>
        /// <returns>bool if value is valid</returns>
        public bool TryGetRightEyeOpenness(out float value);
    }
}

Viz také