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.

134 lines
4.6 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. namespace UnityEngine.XR.ARKit
  5. {
  6. /// <summary>
  7. /// The space-mapping state and set of planes and anchors from
  8. /// an AR session. This is a wrapper for
  9. /// <a href="https://developer.apple.com/documentation/arkit/arworldmap">ARKit's ARWorldMap</a>
  10. /// Note: The <c>ARWorldMap</c> must be explicitly disposed to avoid leaking native resources.
  11. /// </summary>
  12. public struct ARWorldMap : IDisposable, IEquatable<ARWorldMap>
  13. {
  14. /// <summary>
  15. /// Disposes of the world map. This releases the native [ARWorldMap](https://developer.apple.com/documentation/arkit/arworldmap) resource.
  16. /// </summary>
  17. public void Dispose()
  18. {
  19. Api.UnityARKit_disposeWorldMap(nativeHandle);
  20. nativeHandle = k_InvalidHandle;
  21. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  22. AtomicSafetyHandle.Release(m_SafetyHandle);
  23. #endif
  24. }
  25. /// <summary>
  26. /// Use this to determine whether this <c>ARWorldMap</c> is valid, or
  27. /// if it has been disposed.
  28. /// </summary>
  29. public bool valid
  30. {
  31. get
  32. {
  33. return
  34. (nativeHandle != k_InvalidHandle) &&
  35. Api.UnityARKit_isWorldMapValid(nativeHandle);
  36. }
  37. }
  38. /// <summary>
  39. /// Serialize the <c>ARWorldMap</c> to an array of bytes. This array may be saved to disk
  40. /// and loaded at another time to persist the session, or sent over a network
  41. /// to another ARKit-enabled app to share the session.
  42. /// It is an error to call this method after the <c>ARWorldMap</c> has been disposed.
  43. /// </summary>
  44. /// <returns>An array of bytes representing the serialized <c>ARWorldMap</c>.</returns>
  45. public unsafe NativeArray<byte> Serialize(Allocator allocator)
  46. {
  47. if (!valid)
  48. throw new InvalidOperationException("ARWorldMap has been disposed.");
  49. IntPtr nsdata;
  50. int length;
  51. if (!Api.UnityARKit_trySerializeWorldMap(nativeHandle, out nsdata, out length))
  52. throw new InvalidOperationException("Internal error.");
  53. var serializedWorldMap = new NativeArray<byte>(
  54. length, allocator, NativeArrayOptions.UninitializedMemory);
  55. Api.UnityARKit_copyAndReleaseNsData(
  56. new IntPtr(serializedWorldMap.GetUnsafePtr()), nsdata, length);
  57. return serializedWorldMap;
  58. }
  59. /// <summary>
  60. /// Create a new <c>ARWorldMap</c> from a <c>NativeArray</c> of bytes produced
  61. /// from <see cref="Serialize"/>. Use this to create an <c>ARWorldMap</c> from
  62. /// a serialized array of bytes, either loaded from disk or sent from another device.
  63. /// </summary>
  64. /// <param name="serializedWorldMap">An array of bytes representing a serialized <c>ARWorldMap</c>,
  65. /// produced by <see cref="Serialize"/>.</param>
  66. public static unsafe bool TryDeserialize(NativeArray<byte> serializedWorldMap, out ARWorldMap worldMap)
  67. {
  68. var nativeHandle = Api.UnityARKit_deserializeWorldMap(
  69. new IntPtr(serializedWorldMap.GetUnsafePtr()), serializedWorldMap.Length);
  70. if (nativeHandle == k_InvalidHandle)
  71. {
  72. worldMap = default(ARWorldMap);
  73. return false;
  74. }
  75. worldMap = new ARWorldMap(nativeHandle);
  76. return true;
  77. }
  78. public override int GetHashCode()
  79. {
  80. return nativeHandle.GetHashCode();
  81. }
  82. public override bool Equals(object obj)
  83. {
  84. if (!(obj is ARWorldMap))
  85. return false;
  86. return Equals((ARWorldMap)obj);
  87. }
  88. public bool Equals(ARWorldMap other)
  89. {
  90. return (nativeHandle == other.nativeHandle);
  91. }
  92. public static bool operator ==(ARWorldMap lhs, ARWorldMap rhs)
  93. {
  94. return lhs.Equals(rhs);
  95. }
  96. public static bool operator !=(ARWorldMap lhs, ARWorldMap rhs)
  97. {
  98. return !lhs.Equals(rhs);
  99. }
  100. internal ARWorldMap(int nativeHandle)
  101. {
  102. this.nativeHandle = nativeHandle;
  103. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  104. m_SafetyHandle = AtomicSafetyHandle.Create();
  105. #endif
  106. }
  107. internal const int k_InvalidHandle = 0;
  108. internal int nativeHandle { get; private set; }
  109. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  110. AtomicSafetyHandle m_SafetyHandle;
  111. #endif
  112. }
  113. }