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.

132 lines
6.1 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. namespace UnityEngine.XR.ARSubsystems
  4. {
  5. /// <summary>
  6. /// Base class for a raycast subsystem.
  7. /// </summary>
  8. /// <remarks>
  9. /// This abstract class should be implemented by an XR provider and instantiated using the <c>SubsystemManager</c>
  10. /// to enumerate the available <see cref="XRRaycastSubsystemDescriptor"/>s.
  11. /// </remarks>
  12. public abstract class XRRaycastSubsystem : XRSubsystem<XRRaycastSubsystemDescriptor>
  13. {
  14. /// <summary>
  15. /// Constructor. Do not invoke directly; use the <c>SubsystemManager</c>
  16. /// to enumerate the available <see cref="XRRaycastSubsystemDescriptor"/>s
  17. /// and call <c>Create</c> on the desired descriptor.
  18. /// </summary>
  19. public XRRaycastSubsystem() => m_Provider = CreateProvider();
  20. /// <summary>
  21. /// Starts the subsystem.
  22. /// </summary>
  23. protected sealed override void OnStart() => m_Provider.Start();
  24. /// <summary>
  25. /// Stops the subsystem.
  26. /// </summary>
  27. protected sealed override void OnStop() => m_Provider.Stop();
  28. /// <summary>
  29. /// Destroys the subsystem.
  30. /// </summary>
  31. protected sealed override void OnDestroyed() => m_Provider.Destroy();
  32. /// <summary>
  33. /// Casts <paramref name="ray"/> against trackables specified with <paramref name="trackableTypeMask"/>.
  34. /// </summary>
  35. /// <param name="ray">A ray in session space.</param>
  36. /// <param name="trackableTypeMask">The types of trackables to test for ray intersections.</param>
  37. /// <param name="allocator">The <c>Allocator</c> used to allocate the returned <c>NativeArray</c>.</param>
  38. /// <returns>A <c>NativeArray</c> of all the resulting ray intersections.</returns>
  39. public NativeArray<XRRaycastHit> Raycast(
  40. Ray ray,
  41. TrackableType trackableTypeMask,
  42. Allocator allocator)
  43. {
  44. return m_Provider.Raycast(XRRaycastHit.defaultValue, ray, trackableTypeMask, allocator);
  45. }
  46. /// <summary>
  47. /// Casts a ray originating from <paramref name="screenPoint"/> against trackables specified with <paramref name="trackableTypeMask"/>.
  48. /// </summary>
  49. /// <param name="screenPoint">A point on the screen in normalized screen coordinates (0, 0) - (1, 1)</param>
  50. /// <param name="trackableTypeMask">The types of trackables to test for ray intersections.</param>
  51. /// <param name="allocator">The <c>Allocator</c> used to allocate the returned <c>NativeArray</c>.</param>
  52. /// <returns>A <c>NativeArray</c> of all the resulting ray intersections.</returns>
  53. public NativeArray<XRRaycastHit> Raycast(
  54. Vector2 screenPoint,
  55. TrackableType trackableTypeMask,
  56. Allocator allocator)
  57. {
  58. return m_Provider.Raycast(XRRaycastHit.defaultValue, screenPoint, trackableTypeMask, allocator);
  59. }
  60. /// <summary>
  61. /// Should return an instance of <see cref="Provider"/>.
  62. /// </summary>
  63. /// <returns>The interface to the implementation-specific provider.</returns>
  64. protected abstract Provider CreateProvider();
  65. /// <summary>
  66. /// An interface to be implemented by providers of this subsystem.
  67. /// </summary>
  68. protected class Provider
  69. {
  70. /// <summary>
  71. /// Called when the subsystem is started. Will not be called again until <see cref="Stop"/>.
  72. /// </summary>
  73. public virtual void Start() { }
  74. /// <summary>
  75. /// Called when the subsystem is stopped. Will not be called before <see cref="Start"/>.
  76. /// </summary>
  77. public virtual void Stop() { }
  78. /// <summary>
  79. /// Called when the subsystem is destroyed. <see cref="Stop"/> will be called first if the subsystem is running.
  80. /// </summary>
  81. public virtual void Destroy() { }
  82. /// <summary>
  83. /// Performs a raycast from an arbitrary ray against the types
  84. /// specified by <paramref name="trackableTypeMask"/>. Results
  85. /// should be sorted by distance from the ray origin.
  86. /// </summary>
  87. /// <param name="defaultRaycastHit">The default raycast hit that should be used as a template when populating the returned <c>NativeArray</c>.</param>
  88. /// <param name="ray">A ray in session space from which to raycast.</param>
  89. /// <param name="trackableTypeMask">The types to raycast against.</param>
  90. /// <param name="allocator">The allocator with which to allocate the returned <c>NativeArray</c>.</param>
  91. public virtual NativeArray<XRRaycastHit> Raycast(
  92. XRRaycastHit defaultRaycastHit,
  93. Ray ray,
  94. TrackableType trackableTypeMask,
  95. Allocator allocator)
  96. {
  97. throw new NotSupportedException("Raycasting using a Ray is not supported.");
  98. }
  99. /// <summary>
  100. /// Performs a raycast from the camera against the types
  101. /// specified by <paramref name="trackableTypeMask"/>. Results
  102. /// should be sorted by distance from the ray origin.
  103. /// </summary>
  104. /// <param name="defaultRaycastHit">The default raycast hit that should be used as a template when populating the returned <c>NativeArray</c>.</param>
  105. /// <param name="screenPoint">A point on the screen in normalized (0...1) coordinates</param>
  106. /// <param name="trackableTypeMask">The types to raycast against.</param>
  107. /// <param name="allocator">The allocator with which to allocate the returned <c>NativeArray</c>.</param>
  108. public virtual NativeArray<XRRaycastHit> Raycast(
  109. XRRaycastHit defaultRaycastHit,
  110. Vector2 screenPoint,
  111. TrackableType trackableTypeMask,
  112. Allocator allocator)
  113. {
  114. throw new NotSupportedException("Raycasting using a screen point is not supported.");
  115. }
  116. }
  117. Provider m_Provider;
  118. }
  119. }