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.

87 lines
4.3 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. using Unity.Jobs;
  4. using UnityEngine.XR.ARSubsystems;
  5. namespace UnityEngine.XR.ARFoundation
  6. {
  7. /// <summary>
  8. /// Extension methods for <c>UnityEngine.XR.ARSubsystems.MutableRuntimeReferenceImageLibrary</c>.
  9. /// </summary>
  10. public static class MutableRuntimeReferenceImageLibraryExtensions
  11. {
  12. /// <summary>
  13. /// Asynchronously adds <paramref name="texture"/> to <paramref name="library"/>.
  14. /// </summary>
  15. /// <remarks>
  16. /// <para>
  17. /// Image addition can take some time (several frames) due to extra processing that
  18. /// must occur to insert the image into the library. This is done using
  19. /// the [Unity Job System](https://docs.unity3d.com/Manual/JobSystem.html). The returned
  20. /// [JobHandle](https://docs.unity3d.com/ScriptReference/Unity.Jobs.JobHandle.html) can be used
  21. /// to chain together multiple tasks or to query for completion, but may be safely discarded if you do not need it.
  22. /// </para><para>
  23. /// This job, like all Unity jobs, can have dependencies (using the <paramref name="inputDeps"/>). If you are adding multiple
  24. /// images to the library, it is not necessary to pass a previous <c>ScheduleAddImageJob</c> JobHandle as the input
  25. /// dependency to the next <c>ScheduleAddImageJob</c>; they can be processed concurrently.
  26. /// </para><para>
  27. /// The bytes of the <paramref name="texture"/> are copied, so the texture may be safely
  28. /// destroyed after this method returns.
  29. /// </para>
  30. /// </remarks>
  31. /// <param name="library">The <c>MutableRuntimeReferenceImageLibrary</c> being extended.</param>
  32. /// <param name="texture">The <c>Texture2D</c> to use as image target.</param>
  33. /// <param name="name">The name of the image.</param>
  34. /// <param name="widthInMeters">The physical width of the image, in meters.</param>
  35. /// <param name="inputDeps">Input job dependencies (optional).</param>
  36. /// <returns>A [JobHandle](https://docs.unity3d.com/ScriptReference/Unity.Jobs.JobHandle.html) which can be used
  37. /// to chain together multiple tasks or to query for completion. May be safely discarded.</returns>
  38. /// <exception cref="System.InvalidOperationException">Thrown if <see cref="library"/> is <c>null</c>.</exception>
  39. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="texture"/> is <c>null</c>.</exception>
  40. /// <exception cref="System.InvalidOperationException">Thrown if <paramref name="texture"/> is not readable.</exception>
  41. public static JobHandle ScheduleAddImageJob(
  42. this MutableRuntimeReferenceImageLibrary library,
  43. Texture2D texture,
  44. string name,
  45. float? widthInMeters,
  46. JobHandle inputDeps = default(JobHandle))
  47. {
  48. if (ReferenceEquals(library, null))
  49. throw new ArgumentNullException(nameof(library));
  50. if (texture == null)
  51. throw new ArgumentNullException(nameof(texture));
  52. if (!texture.isReadable)
  53. throw new InvalidOperationException("The texture must be readable to be used as the source for a reference image.");
  54. var imageBytesCopy = new NativeArray<byte>(texture.GetRawTextureData<byte>(), Allocator.Persistent);
  55. try
  56. {
  57. Nullable<Vector2> size = null;
  58. if (widthInMeters.HasValue)
  59. {
  60. size = new Vector2(widthInMeters.Value, widthInMeters.Value * (float)texture.height / (float)texture.width);
  61. }
  62. var referenceImage = new XRReferenceImage(SerializableGuid.empty, SerializableGuid.empty, size, name, texture);
  63. var jobHandle = library.ScheduleAddImageJob(imageBytesCopy, new Vector2Int(texture.width, texture.height), texture.format, referenceImage, inputDeps);
  64. new DeallocateJob { data = imageBytesCopy }.Schedule(jobHandle);
  65. return jobHandle;
  66. }
  67. catch
  68. {
  69. imageBytesCopy.Dispose();
  70. throw;
  71. }
  72. }
  73. struct DeallocateJob : IJob
  74. {
  75. [DeallocateOnJobCompletion]
  76. public NativeArray<byte> data;
  77. public void Execute() {}
  78. }
  79. }
  80. }