SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.8 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. using UnityEngine.XR.ARSubsystems;
  5. namespace UnityEngine.XR.ARFoundation
  6. {
  7. /// <summary>
  8. /// Represents a face detected by an AR device.
  9. /// </summary>
  10. /// <remarks>
  11. /// Generated by the <see cref="ARFaceManager"/> when an AR device detects
  12. /// a face in the environment.
  13. /// </remarks>
  14. [DisallowMultipleComponent]
  15. [DefaultExecutionOrder(ARUpdateOrder.k_Face)]
  16. [HelpURL("https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.0/api/UnityEngine.XR.ARFoundation.ARFace.html")]
  17. public sealed class ARFace : ARTrackable<XRFace, ARFace>
  18. {
  19. /// <summary>
  20. /// Invoked when the face is updated. If face meshes are supported, there will be
  21. /// updated <see cref="vertices"/>, <see cref="normals"/>, <see cref="indices"/>, and
  22. /// <see cref="uvs"/>.
  23. /// </summary>
  24. public event Action<ARFaceUpdatedEventArgs> updated;
  25. /// <summary>
  26. /// The vertices representing the face mesh. Check for existence with <c>vertices.IsCreated</c>.
  27. /// This array is parallel to <see cref="normals"/> and <see cref="uvs"/>. Vertices are
  28. /// provided in face space, that is, relative to this <see cref="ARFace"/>'s local
  29. /// position and rotation.
  30. /// </summary>
  31. public unsafe NativeArray<Vector3> vertices
  32. {
  33. get
  34. {
  35. return GetUndisposable(m_FaceMesh.vertices);
  36. }
  37. }
  38. /// <summary>
  39. /// The normals representing the face mesh. Check for existence with <c>normals.IsCreated</c>.
  40. /// This array is parallel to <see cref="vertices"/> and <see cref="uvs"/>.
  41. /// </summary>
  42. public unsafe NativeArray<Vector3> normals
  43. {
  44. get
  45. {
  46. return GetUndisposable(m_FaceMesh.normals);
  47. }
  48. }
  49. /// <summary>
  50. /// The indices defining the triangles of the face mesh. Check for existence with <c>indices.IsCreated</c>.
  51. /// The are three times as many indices as triangles, so this will always be a multiple of 3.
  52. /// </summary>
  53. public NativeArray<int> indices
  54. {
  55. get
  56. {
  57. return GetUndisposable(m_FaceMesh.indices);
  58. }
  59. }
  60. /// <summary>
  61. /// The texture coordinates representing the face mesh. Check for existence with <c>uvs.IsCreated</c>.
  62. /// This array is parallel to <see cref="vertices"/> and <see cref="normals"/>.
  63. /// </summary>
  64. public NativeArray<Vector2> uvs
  65. {
  66. get
  67. {
  68. return GetUndisposable(m_FaceMesh.uvs);
  69. }
  70. }
  71. /// <summary>
  72. /// Get a native pointer associated with this face.
  73. /// </summary>
  74. /// <remarks>
  75. /// The data pointed to by this member is implementation defined.
  76. /// The lifetime of the pointed to object is also
  77. /// implementation defined, but should be valid at least until the next
  78. /// <see cref="ARSession"/> update.
  79. /// </remarks>
  80. public IntPtr nativePtr => sessionRelativeData.nativePtr;
  81. /// <summary>
  82. /// The [transform](https://docs.unity3d.com/ScriptReference/Transform.html) of the left eye of the face, or `null` if there is no data for the left eye.
  83. /// </summary>
  84. public Transform leftEye { get; private set; }
  85. /// <summary>
  86. /// The [transform](https://docs.unity3d.com/ScriptReference/Transform.html) of the right eye of the face, or `null` if there is no data for the right eye.
  87. /// </summary>
  88. public Transform rightEye { get; private set; }
  89. /// <summary>
  90. /// The [transform](https://docs.unity3d.com/ScriptReference/Transform.html) representing the point at which the eyes are fixated upon or `null` if there is no fixation data.
  91. /// </summary>
  92. public Transform fixationPoint { get; private set; }
  93. void Update()
  94. {
  95. if (m_Updated && updated != null)
  96. {
  97. updated(new ARFaceUpdatedEventArgs(this));
  98. m_Updated = false;
  99. }
  100. }
  101. void OnDestroy()
  102. {
  103. m_FaceMesh.Dispose();
  104. }
  105. // Creates an alias to the same array, but the caller cannot Dispose it.
  106. unsafe NativeArray<T> GetUndisposable<T>(NativeArray<T> disposable) where T : struct
  107. {
  108. if (!disposable.IsCreated)
  109. return default(NativeArray<T>);
  110. return NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>(
  111. disposable.GetUnsafePtr(),
  112. disposable.Length,
  113. Allocator.None);
  114. }
  115. internal void UpdateMesh(XRFaceSubsystem subsystem)
  116. {
  117. subsystem.GetFaceMesh(sessionRelativeData.trackableId, Allocator.Persistent, ref m_FaceMesh);
  118. m_Updated = true;
  119. }
  120. internal void UpdateEyes()
  121. {
  122. if (leftEye == null && rightEye == null && fixationPoint == null)
  123. {
  124. leftEye = Instantiate(new GameObject(), transform).transform;
  125. rightEye = Instantiate(new GameObject(), transform).transform;
  126. fixationPoint = Instantiate(new GameObject(), transform).transform;
  127. }
  128. UpdateTransformFromPose(leftEye, sessionRelativeData.leftEyePose);
  129. UpdateTransformFromPose(rightEye, sessionRelativeData.rightEyePose);
  130. fixationPoint.localPosition = sessionRelativeData.fixationPoint;
  131. }
  132. private void UpdateTransformFromPose(Transform eyeTransform, Pose eyePose)
  133. {
  134. eyeTransform.localPosition = eyePose.position;
  135. eyeTransform.localRotation = eyePose.rotation;
  136. }
  137. XRFaceMesh m_FaceMesh;
  138. bool m_Updated;
  139. }
  140. }