FaceDetectorManager.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using FFalcon.XR.Runtime;
  2. using UnityEngine;
  3. namespace RayNeo
  4. {
  5. public class FaceDetectorManager
  6. {
  7. private static FaceDetectorManager ins = new FaceDetectorManager();
  8. public static FaceDetectorManager Ins
  9. {
  10. get
  11. {
  12. return ins;
  13. }
  14. }
  15. float[] m_position = new float[3];
  16. Vector3 m_posVec3 = Vector3.zero;
  17. private long m_faceHandle = 0;
  18. /// <summary>
  19. /// 获取脸部位置.
  20. /// 调用即代表初始化.需要在适当时机调用StopFaceDectector
  21. /// </summary>
  22. /// <param name="suc">代表有没有获取到数据.</param>
  23. /// <returns>如果是Vector3.zero则是没有获取到.</returns>
  24. public Vector3 GetFacePosition(out bool suc)
  25. {
  26. #if UNITY_EDITOR
  27. //编辑器不执行. 后续可以考虑加入debug
  28. suc = false;
  29. return Vector3.zero;
  30. #endif
  31. if (m_faceHandle == 0)
  32. {
  33. m_faceHandle = Api.CreateFaceDetector();
  34. if (m_faceHandle != 0)
  35. {
  36. Api.InitFaceDetector();
  37. }
  38. }
  39. if (m_faceHandle != 0)
  40. {
  41. Api.GetFaceInCamera(m_position);
  42. //Api.GetFacePosition(m_position);
  43. if (m_position[0] == 0 && m_position[1] == 0 && m_position[2] == 0)
  44. {
  45. suc = false;
  46. return Vector3.zero;
  47. }
  48. m_posVec3.Set(m_position[0], m_position[1], m_position[2]);
  49. }
  50. else
  51. {
  52. suc = false;
  53. return Vector3.zero;
  54. }
  55. suc = true;
  56. return m_posVec3;
  57. }
  58. public void StopFaceDectector()
  59. {
  60. #if UNITY_EDITOR
  61. return;
  62. #endif
  63. Api.DestroyFaceDetector();
  64. m_faceHandle = 0;
  65. }
  66. public void Recenter()
  67. {
  68. #if UNITY_EDITOR
  69. return;
  70. #endif
  71. Api.Recenter();
  72. }
  73. }
  74. }