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.

153 lines
7.1 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. using UnityEngine.Scripting;
  4. using UnityEngine.XR.ARSubsystems;
  5. namespace UnityEngine.XR.ARKit
  6. {
  7. /// <summary>
  8. /// An internal class with only static methods to register the environment probe subsystem before the scene is
  9. /// loaded.
  10. /// </summary>
  11. internal static class ARKitEnvironmentProbeRegistration
  12. {
  13. /// <summary>
  14. /// Create and register the environment probe subsystem descriptor to advertise a providing implementation for
  15. /// environment probe functionality.
  16. /// </summary>
  17. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  18. static void Register()
  19. {
  20. #if UNITY_IOS && !UNITY_EDITOR
  21. var iOSVersion = OSVersion.Parse(UnityEngine.iOS.Device.systemVersion);
  22. if (iOSVersion < new OSVersion(12))
  23. return;
  24. const string subsystemId = "ARKit-EnvironmentProbe";
  25. XREnvironmentProbeSubsystemCinfo environmentProbeSubsystemInfo = new XREnvironmentProbeSubsystemCinfo()
  26. {
  27. id = subsystemId,
  28. implementationType = typeof(ARKitEnvironmentProbeSubsystem),
  29. supportsManualPlacement = true,
  30. supportsRemovalOfManual = true,
  31. supportsAutomaticPlacement = true,
  32. supportsRemovalOfAutomatic = true,
  33. supportsEnvironmentTexture = true,
  34. supportsEnvironmentTextureHDR = iOSVersion >= new OSVersion(13),
  35. };
  36. if (!XREnvironmentProbeSubsystem.Register(environmentProbeSubsystemInfo))
  37. {
  38. Debug.LogErrorFormat("Cannot register the {0} subsystem", subsystemId);
  39. }
  40. #endif
  41. }
  42. }
  43. /// <summary>
  44. /// This subsystem provides implementing functionality for the <c>XREnvironmentProbeSubsystem</c> class.
  45. /// </summary>
  46. [Preserve]
  47. class ARKitEnvironmentProbeSubsystem : XREnvironmentProbeSubsystem
  48. {
  49. protected override Provider CreateProvider() => new ARKitProvider();
  50. class ARKitProvider : Provider
  51. {
  52. public ARKitProvider() => EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_Construct();
  53. public override void Start() => EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_Start();
  54. /// <summary>
  55. /// Stops the environment probe subsystem by disabling the environment probe state.
  56. /// </summary>
  57. public override void Stop() => EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_Stop();
  58. /// <summary>
  59. /// Destroy the environment probe subsystem by first ensuring that the subsystem has been stopped and then
  60. /// destroying the provider.
  61. /// </summary>
  62. public override void Destroy() => EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_Destruct();
  63. /// <summary>
  64. /// Enable or disable automatic placement of environment probes by the provider.
  65. /// </summary>
  66. /// <param name='value'><c>true</c> if the provider should automatically place environment probes in the scene.
  67. /// Otherwise, <c>false</c></param>.
  68. public override void SetAutomaticPlacement(bool value)
  69. {
  70. EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_SetAutomaticPlacementEnabled(value);
  71. }
  72. /// <summary>
  73. /// Set the state of HDR environment texture generation.
  74. /// </summary>
  75. /// <param name="value">Whether HDR environment texture generation is enabled (<c>true</c>) or disabled
  76. /// (<c>false</c>).</param>
  77. /// <returns>
  78. /// Whether the HDR environment texture generation state was set.
  79. /// </returns>
  80. public override bool TrySetEnvironmentTextureHDREnabled(bool value)
  81. {
  82. return EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_TrySetEnvironmentTextureHDREnabled(value);
  83. }
  84. public override bool TryAddEnvironmentProbe(Pose pose, Vector3 scale, Vector3 size, out XREnvironmentProbe environmentProbe)
  85. {
  86. return EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_TryAddEnvironmentProbe(pose, scale, size, out environmentProbe);
  87. }
  88. /// <summary>
  89. /// Remove the environment probe matching the trackable ID from the AR session..
  90. /// </summary>
  91. /// <param name='trackableId'>The trackable ID for the environment probe to be removed.</param>
  92. /// <returns>
  93. /// <c>true</c> if an environment probe matching the trackable ID is found and will be removed from the AR
  94. /// session. Otherwise, <c>false</c>.
  95. /// </returns>
  96. public override bool RemoveEnvironmentProbe(TrackableId trackableId)
  97. {
  98. return EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_TryRemoveEnvironmentProbe(trackableId);
  99. }
  100. public override TrackableChanges<XREnvironmentProbe> GetChanges(XREnvironmentProbe defaultEnvironmentProbe,
  101. Allocator allocator)
  102. {
  103. int numAddedEnvironmentProbes;
  104. IntPtr addedEnvironmentProbesPointer;
  105. int numUpdatedEnvironmentProbes;
  106. IntPtr updatedEnvironmentProbesPointer;
  107. int numRemovedEnvironmentProbeIds;
  108. IntPtr removedEnvironmentProbeIdsPointer;
  109. int stride;
  110. var context = EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_AcquireChanges(out numAddedEnvironmentProbes, out addedEnvironmentProbesPointer,
  111. out numUpdatedEnvironmentProbes, out updatedEnvironmentProbesPointer,
  112. out numRemovedEnvironmentProbeIds, out removedEnvironmentProbeIdsPointer,
  113. out stride);
  114. try
  115. {
  116. unsafe
  117. {
  118. // Wrap the navite pointers into a native array and then copy them into a separate native array enabled
  119. // with temporary allocations.
  120. return new TrackableChanges<XREnvironmentProbe>(
  121. (void*)addedEnvironmentProbesPointer, numAddedEnvironmentProbes,
  122. (void*)updatedEnvironmentProbesPointer, numUpdatedEnvironmentProbes,
  123. (void*)removedEnvironmentProbeIdsPointer, numRemovedEnvironmentProbeIds,
  124. defaultEnvironmentProbe, stride,
  125. allocator);
  126. }
  127. }
  128. finally
  129. {
  130. EnvironmentProbeApi.UnityARKit_EnvironmentProbeProvider_ReleaseChanges(context);
  131. }
  132. }
  133. }
  134. }
  135. }