//========= Copyright 2019, HTC Corporation. All rights reserved. =========== using System; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.XR.OpenXR; namespace VIVE { namespace FacialTracking.Sample { public static class Eye { public const int WeightingCount = (int)XrEyeShapeHTC.XR_EYE_EXPRESSION_MAX_ENUM_HTC; private static XrFacialExpressionsHTC EyeExpression_; private static int LastUpdateFrame = -1; private static Error LastUpdateResult = Error.FAILED; private static Dictionary Weightings; private static float[] blendshapes = new float[60]; static Eye() { Weightings = new Dictionary(); for (int i = 0; i < WeightingCount; ++i) Weightings.Add((XrEyeShapeHTC)i, 0.0f); } private static bool UpdateData() { if (Time.frameCount == LastUpdateFrame) return LastUpdateResult == Error.WORK; else LastUpdateFrame = Time.frameCount; EyeExpression_.expressionCount = 60; EyeExpression_.type = XrStructureType.XR_TYPE_FACIAL_EXPRESSIONS_HTC; EyeExpression_.blendShapeWeightings = Marshal.AllocCoTaskMem(sizeof(float) * EyeExpression_.expressionCount); var feature = OpenXRSettings.Instance.GetFeature(); int res = feature.xrGetFacialExpressionsHTC(OpenXRSettings.Instance.GetFeature().m_expressionHandle, ref EyeExpression_); if (res == (int)XrResult.XR_SUCCESS) { Marshal.Copy(EyeExpression_.blendShapeWeightings, blendshapes, 0, EyeExpression_.expressionCount); LastUpdateResult = Error.WORK; } else { LastUpdateResult = Error.FAILED; } return LastUpdateResult == Error.WORK; } public static bool GetEyeWeightings(out Dictionary shapes, XrFacialExpressionsHTC expression) { for (int i = 0; i < WeightingCount; ++i) { Weightings[(XrEyeShapeHTC)(i)] = blendshapes[i]; } shapes = Weightings; return true; } /// /// Gets weighting values from Eye module. /// /// Weighting values obtained from Eye module. /// Indicates whether the values received are new.\ [Obsolete("Create FacialManager object and call member function GetWeightings instead")] public static bool GetEyeWeightings(out Dictionary shapes) { UpdateData(); return GetEyeWeightings(out shapes, EyeExpression_); } } } }