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.

173 lines
6.9 KiB

4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Serialization;
  4. using UnityEngine.XR.ARSubsystems;
  5. namespace UnityEngine.XR.ARFoundation
  6. {
  7. /// <summary>
  8. /// Manages anchors.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>Use this component to programmatically add, remove, or query for
  12. /// anchors. Anchors are <c>Pose</c>s in the world
  13. /// which will be periodically updated by an AR device as its understanding
  14. /// of the world changes.</para>
  15. /// <para>Subscribe to changes (added, updated, and removed) via the
  16. /// <see cref="ARAnchorManager.anchorsChanged"/> event.</para>
  17. /// </remarks>
  18. /// <seealso cref="ARTrackableManager{TSubsystem, TSubsystemDescriptor, TSessionRelativeData, TTrackable}"/>
  19. [DefaultExecutionOrder(ARUpdateOrder.k_AnchorManager)]
  20. [DisallowMultipleComponent]
  21. [RequireComponent(typeof(ARSessionOrigin))]
  22. [HelpURL("https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.0/api/UnityEngine.XR.ARFoundation.ARAnchorManager.html")]
  23. public sealed class ARAnchorManager : ARTrackableManager<
  24. XRAnchorSubsystem,
  25. XRAnchorSubsystemDescriptor,
  26. XRAnchor,
  27. ARAnchor>
  28. {
  29. [SerializeField]
  30. [Tooltip("If not null, instantiates this prefab for each instantiated anchor.")]
  31. [FormerlySerializedAs("m_ReferencePointPrefab")]
  32. GameObject m_AnchorPrefab;
  33. /// <summary>
  34. /// Getter/setter for the Anchor Prefab.
  35. /// </summary>
  36. public GameObject anchorPrefab
  37. {
  38. get { return m_AnchorPrefab; }
  39. set { m_AnchorPrefab = value; }
  40. }
  41. /// <summary>
  42. /// Invoked once per frame to communicate changes to anchors, including
  43. /// new anchors, the update of existing anchors, and the removal
  44. /// of previously existing anchors.
  45. /// </summary>
  46. public event Action<ARAnchorsChangedEventArgs> anchorsChanged;
  47. /// <summary>
  48. /// Attempts to add an <see cref="ARAnchor"/> with the given <c>Pose</c>.
  49. /// </summary>
  50. /// <remarks>
  51. /// If <see cref="ARTrackableManager{TSubsystem, TSubsystemDescriptor, TSessionRelativeData, TTrackable}.prefab"/>
  52. /// is not null, a new instance of that prefab will be instantiated. Otherwise, a
  53. /// new <c>GameObject</c> will be created. In either case, the resulting
  54. /// <c>GameObject</c> will have an <see cref="ARAnchor"/> component on it.
  55. /// </remarks>
  56. /// <param name="pose">The pose, in Unity world space, of the <see cref="ARAnchor"/>.</param>
  57. /// <returns>A new <see cref="ARAnchor"/> if successful, otherwise <c>null</c>.</returns>
  58. public ARAnchor AddAnchor(Pose pose)
  59. {
  60. if (!enabled)
  61. throw new InvalidOperationException("Cannot create a anchor from a disabled anchor manager.");
  62. if (subsystem == null)
  63. throw new InvalidOperationException("Anchor manager has no subsystem. Enable the manager first.");
  64. var sessionRelativePose = sessionOrigin.trackablesParent.InverseTransformPose(pose);
  65. // Add the anchor to the XRAnchorSubsystem
  66. XRAnchor sessionRelativeData;
  67. if (subsystem.TryAddAnchor(sessionRelativePose, out sessionRelativeData))
  68. return CreateTrackableImmediate(sessionRelativeData);
  69. return null;
  70. }
  71. /// <summary>
  72. /// Attempts to create a new anchor that is attached to an existing <see cref="ARPlane"/>.
  73. /// </summary>
  74. /// <param name="plane">The <see cref="ARPlane"/> to which to attach.</param>
  75. /// <param name="pose">The initial <c>Pose</c>, in Unity world space, of the anchor.</param>
  76. /// <returns>A new <see cref="ARAnchor"/> if successful, otherwise <c>null</c>.</returns>
  77. public ARAnchor AttachAnchor(ARPlane plane, Pose pose)
  78. {
  79. if (!enabled)
  80. throw new InvalidOperationException("Cannot create a anchor from a disabled anchor manager.");
  81. if (subsystem == null)
  82. throw new InvalidOperationException("Anchor manager has no subsystem. Enable the manager first.");
  83. if (plane == null)
  84. throw new ArgumentNullException("plane");
  85. var sessionRelativePose = sessionOrigin.trackablesParent.InverseTransformPose(pose);
  86. XRAnchor sessionRelativeData;
  87. if (subsystem.TryAttachAnchor(plane.trackableId, sessionRelativePose, out sessionRelativeData))
  88. return CreateTrackableImmediate(sessionRelativeData);
  89. return null;
  90. }
  91. /// <summary>
  92. /// Attempts to remove an <see cref="ARAnchor"/>.
  93. /// </summary>
  94. /// <param name="anchor">The anchor you wish to remove.</param>
  95. /// <returns>
  96. /// <c>True</c> if the anchor was successfully removed.
  97. /// <c>False</c> usually means the anchor is not longer tracked by the system.
  98. /// </returns>
  99. public bool RemoveAnchor(ARAnchor anchor)
  100. {
  101. if (!enabled)
  102. throw new InvalidOperationException("Cannot create a anchor from a disabled anchor manager.");
  103. if (subsystem == null)
  104. throw new InvalidOperationException("Anchor manager has no subsystem. Enable the manager first.");
  105. if (anchor == null)
  106. throw new ArgumentNullException("anchor");
  107. if (subsystem.TryRemoveAnchor(anchor.trackableId))
  108. {
  109. DestroyPendingTrackable(anchor.trackableId);
  110. return true;
  111. }
  112. return false;
  113. }
  114. /// <summary>
  115. /// Gets the <see cref="ARAnchor"/> with given <paramref name="trackableId"/>,
  116. /// or <c>null</c> if it does not exist.
  117. /// </summary>
  118. /// <param name="trackableId">The <see cref="TrackableId"/> of the <see cref="ARAnchor"/> to retrieve.</param>
  119. /// <returns>The <see cref="ARAnchor"/> with <paramref name="trackableId"/> or <c>null</c> if it does not exist.</returns>
  120. public ARAnchor GetAnchor(TrackableId trackableId)
  121. {
  122. ARAnchor anchor;
  123. if (m_Trackables.TryGetValue(trackableId, out anchor))
  124. return anchor;
  125. return null;
  126. }
  127. protected override GameObject GetPrefab()
  128. {
  129. return m_AnchorPrefab;
  130. }
  131. protected override string gameObjectName
  132. {
  133. get { return "Anchor"; }
  134. }
  135. protected override void OnTrackablesChanged(
  136. List<ARAnchor> addedPoints,
  137. List<ARAnchor> updatedPoints,
  138. List<ARAnchor> removedPoints)
  139. {
  140. if (anchorsChanged != null)
  141. {
  142. anchorsChanged(
  143. new ARAnchorsChangedEventArgs(
  144. addedPoints,
  145. updatedPoints,
  146. removedPoints));
  147. }
  148. }
  149. }
  150. }