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.

186 lines
9.3 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. using UnityEngine.Assertions;
  4. namespace UnityEngine.XR.ARSubsystems
  5. {
  6. /// <summary>
  7. /// A subsystem for detecting and tracking a preconfigured set of images in the environment.
  8. /// </summary>
  9. /// <typeparam name="XRTrackedImage">Low level data describing a tracked image.</typeparam>
  10. /// <typeparam name="XRImageTrackingSubsystemDescriptor">The descriptor used by implementations to describe the feature set of the image tracking subsystem.</typeparam>
  11. public abstract class XRImageTrackingSubsystem
  12. : TrackingSubsystem<XRTrackedImage, XRImageTrackingSubsystemDescriptor>
  13. {
  14. /// <summary>
  15. /// Constructs a subsystem. Do not invoked directly; call <c>Create</c> on the <see cref="XRImageTrackingSubsystemDescriptor"/> instead.
  16. /// </summary>
  17. public XRImageTrackingSubsystem() => m_Provider = CreateProvider();
  18. /// <summary>
  19. /// Starts the subsystem, that is, start detecting images in the scene. <see cref="imageLibrary"/> must not be null.
  20. /// </summary>
  21. /// <exception cref="System.InvalidOperationException">Thrown if <see cref="imageLibrary"/> is null.</exception>
  22. protected override void OnStart()
  23. {
  24. if (m_ImageLibrary == null)
  25. throw new InvalidOperationException("Cannot start image tracking without an image library.");
  26. m_Provider.imageLibrary = m_ImageLibrary;
  27. }
  28. /// <summary>
  29. /// Stops the subsystem, that is, stops detecting and tracking images.
  30. /// </summary>
  31. protected sealed override void OnStop() => m_Provider.imageLibrary = null;
  32. /// <summary>
  33. /// Destroys the subsystem.
  34. /// </summary>
  35. protected sealed override void OnDestroyed() => m_Provider.Destroy();
  36. /// <summary>
  37. /// Get or set the reference image library. This is the set of images to look for in the environment.
  38. /// </summary>
  39. /// <remarks>
  40. /// A <see cref="RuntimeReferenceImageLibrary"/> is created at runtime and may be modifiable
  41. /// (see <see cref="MutableRuntimeReferenceImageLibrary"/>). A <see cref="RuntimeReferenceImageLibrary"/>
  42. /// may be created from an <see cref="XRReferenceImageLibrary"/> using
  43. /// <see cref="CreateRuntimeLibrary(XRReferenceImageLibrary"/>.
  44. /// </remarks>
  45. /// <exception cref="System.ArgumentNullException">Thrown if the subsystem has been started, and you attempt to set the image library to null.</exception>
  46. /// <seealso cref="Start"/>
  47. /// <seealso cref="Stop"/>
  48. /// <seealso cref="XRReferenceImageLibrary"/>
  49. /// <seealso cref="MutableRuntimeReferenceImageLibrary"/>
  50. public RuntimeReferenceImageLibrary imageLibrary
  51. {
  52. get => m_ImageLibrary;
  53. set
  54. {
  55. if (m_ImageLibrary == value)
  56. return;
  57. if (running && value == null)
  58. throw new ArgumentNullException("Cannot set imageLibrary to null while subsystem is running.");
  59. m_ImageLibrary = value;
  60. if (running)
  61. m_Provider.imageLibrary = m_ImageLibrary;
  62. }
  63. }
  64. /// <summary>
  65. /// Creates a <see cref="RuntimeReferenceImageLibrary"/> from an existing <see cref="XRReferenceImageLibrary"/>
  66. /// or an empty library if <paramref name="serializedLibrary"/> is <c>null</c>.
  67. /// Use this to construct the runtime representation of an <see cref="XRReferenceImageLibrary"/>.
  68. /// </summary>
  69. /// <remarks>
  70. /// If the subsystem supports runtime mutable libraries
  71. /// (see <see cref="XRImageTrackingSubsystemDescriptor.supportsMutableLibrary"/>), then the returned
  72. /// library will be a <see cref="MutableRuntimeReferenceImageLibrary"/>.
  73. /// </remarks>
  74. /// <param name="serializedLibrary">An existing <see cref="XRReferenceImageLibrary"/> created at edit time, or <c>null</c> to create an empty image library.</param>
  75. /// <returns>A new <see cref="RuntimeReferenceImageLibrary"/> representing the deserialized version of <paramref name="serializedLibrary"/>
  76. /// or an empty library if <paramref name="serializedLibrary"/> is <c>null</c>.</returns>
  77. /// <seealso cref="RuntimeReferenceImageLibrary"/>
  78. public RuntimeReferenceImageLibrary CreateRuntimeLibrary(XRReferenceImageLibrary serializedLibrary)
  79. {
  80. var library = m_Provider.CreateRuntimeLibrary(serializedLibrary);
  81. Assert.IsFalse(ReferenceEquals(library, null));
  82. return library;
  83. }
  84. /// <summary>
  85. /// Retrieve the changes in the state of tracked images (added, updated, removed) since the last call to <c>GetChanges</c>.
  86. /// </summary>
  87. /// <param name="allocator">The allocator to use for the returned set of changes.</param>
  88. /// <returns>The set of tracked image changes (added, updated, removed) since the last call to this method.</returns>
  89. public override TrackableChanges<XRTrackedImage> GetChanges(Allocator allocator)
  90. {
  91. var changes = m_Provider.GetChanges(XRTrackedImage.defaultValue, allocator);
  92. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  93. m_ValidationUtility.ValidateAndDisposeIfThrown(changes);
  94. #endif
  95. return changes;
  96. }
  97. /// <summary>
  98. /// The maximum number of moving images to track.
  99. /// </summary>
  100. /// <exception cref="System.NotSupportedException"/>
  101. /// Thrown if the subsystem does not support tracking moving images.
  102. /// Check for support of this feature with <see cref="XRImageTrackingSubsystemDescriptor.supportsMovingImages"/>.
  103. /// </exception>
  104. public int maxNumberOfMovingImages
  105. {
  106. set => m_Provider.maxNumberOfMovingImages = value;
  107. }
  108. /// <summary>
  109. /// Methods to implement by the implementing provider.
  110. /// </summary>
  111. protected abstract class Provider
  112. {
  113. /// <summary>
  114. /// Called when the subsystem is destroyed.
  115. /// </summary>
  116. public virtual void Destroy() {}
  117. /// <summary>
  118. /// Get the changes (added, updated, removed) to the tracked images since the last call to this method.
  119. /// </summary>
  120. /// <param name="defaultTrackedImage">An <see cref="XRTrackedImage"/> populated with default values.
  121. /// The implementation should first fill arrays of added, updated, and removed with copies of this
  122. /// before copying in its own values. This guards against addtional fields added to the <see cref="XRTrackedImage"/> in the future.</param>
  123. /// <param name="allocator">The allocator to use for the returned data.</param>
  124. /// <returns>The set of changes (added, updated, removed) tracked images since the last call to this method.</returns>
  125. public abstract TrackableChanges<XRTrackedImage> GetChanges(XRTrackedImage defaultTrackedImage, Allocator allocator);
  126. /// <summary>
  127. /// Sets the set of images to search for in the environment.
  128. /// </summary>
  129. /// <remarks>
  130. /// Setting this to <c>null</c> implies the subsystem should stop detecting and tracking images.
  131. /// </remarks>
  132. public abstract RuntimeReferenceImageLibrary imageLibrary { set; }
  133. /// <summary>
  134. /// Creates a <see cref="RuntimeReferenceImageLibrary"/> from an existing <see cref="XRReferenceImageLibrary"/>,
  135. /// or an empty library if <paramref name="serializedLibrary"/> is <c>null</c>.
  136. /// </summary>
  137. /// <param name="serializedLibrary">A <see cref="XRReferenceImageLibrary"/> to deserialize.</param>
  138. /// <returns>The runtime version of <paramref name="serializedLibrary"/> or an empty library if <paramref name="serializedLibrary"/> is <c>null</c>.</returns>
  139. public abstract RuntimeReferenceImageLibrary CreateRuntimeLibrary(XRReferenceImageLibrary serializedLibrary);
  140. /// <summary>
  141. /// The maximum number of moving images to track in realtime.
  142. /// </summary>
  143. /// <remarks>
  144. /// Must be implemented if <see cref="XRImageTrackingSubsystemDescriptor.supportsMovingImages"/> is <c>true</c>;
  145. /// otherwise, this property will never be set and need not be implemented.
  146. /// </remarks>
  147. /// <exception cref="System.NotSupportedException">Thrown if not overridden by the derived class.</exception>
  148. public virtual int maxNumberOfMovingImages
  149. {
  150. set => throw new NotSupportedException("This subsystem does not track moving images.");
  151. }
  152. }
  153. /// <summary>
  154. /// Create an implementation of the <see cref="Provider"/> class. This will only be called once.
  155. /// </summary>
  156. /// <returns>An instance of the <see cref="Provider"/> interface.</returns>
  157. protected abstract Provider CreateProvider();
  158. RuntimeReferenceImageLibrary m_ImageLibrary;
  159. Provider m_Provider;
  160. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  161. ValidationUtility<XRTrackedImage> m_ValidationUtility =
  162. new ValidationUtility<XRTrackedImage>();
  163. #endif
  164. }
  165. }