AddARTrackedManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.Collections;
  4. using Unity.Jobs;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using UnityEngine.XR.ARFoundation;
  8. using UnityEngine.XR.ARSubsystems;
  9. public class AddARTrackedManager : MonoBehaviour
  10. {
  11. [SerializeField]
  12. ARTrackedImageManager m_ARTrackedImageManager;
  13. [SerializeField]
  14. Text text;
  15. // [SerializeField]
  16. List<Texture2D> image;
  17. public ImageLibraryTest imageLibraryTest;
  18. private void Start()
  19. {
  20. image = new List<Texture2D>();
  21. Texture2D texture = (Texture2D)Resources.Load<Texture>("Image/1");
  22. image.Add(texture);
  23. //Debug.Log(texture.name);
  24. //for (int i = 1; i < 6; i++)
  25. //{
  26. // string url = "Image/" + i.ToString();
  27. // Debug.Log(url);
  28. // Texture2D texture = (Texture2D)Resources.Load<Texture>(url);
  29. // image.Add(texture);
  30. // Debug.Log(texture.name);
  31. //}
  32. StartCoroutine(ADDTrackedImage(10f));
  33. // AddImage(texture.EncodeToJPG(), texture.width, texture.height, 10);
  34. }
  35. IEnumerator ADDTrackedImage(float times)
  36. {
  37. text.text = " 即将动态添加TrackedImage ";
  38. yield return times;
  39. text.text = "动态添加TrackedImage中";
  40. if (m_ARTrackedImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
  41. {
  42. for (int i = 0; i < image.Count; i++)
  43. {
  44. Debug.Log(image[i].name);
  45. mutableLibrary.ScheduleAddImageWithValidationJob(image[i], image[i].name, image[i].width);
  46. }
  47. text.text = " 添加完毕 ";
  48. }
  49. else
  50. text.text = " 添加失败 ";
  51. //imageLibraryTest.enabled = true;
  52. }
  53. public void Add(List<Texture2D> image)
  54. {
  55. Debug.Log(image.Count);
  56. if (m_ARTrackedImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
  57. {
  58. for (int i = 0; i < image.Count; i++)
  59. {
  60. Debug.Log(image[i].name);
  61. mutableLibrary.ScheduleAddImageWithValidationJob(image[i], image[i].name, image[i].width);
  62. }
  63. }
  64. }
  65. void AddImage(Texture2D imageToAdd)
  66. {
  67. Debug.Log(imageToAdd.name);
  68. var library = m_ARTrackedImageManager.CreateRuntimeLibrary();
  69. if (library is MutableRuntimeReferenceImageLibrary mutableLibrary)
  70. {
  71. Debug.Log(imageToAdd.name);
  72. mutableLibrary.ScheduleAddImageWithValidationJob(
  73. imageToAdd,
  74. "my new image",
  75. 0.5f /* 50 cm */);
  76. }
  77. }
  78. struct DeallocateJob : IJob
  79. {
  80. [DeallocateOnJobCompletion]
  81. public NativeArray<byte> data;
  82. public void Execute() { }
  83. }
  84. void AddImage(NativeArray<byte> grayscaleImageBytes,
  85. int widthInPixels, int heightInPixels,
  86. float widthInMeters)
  87. {
  88. if (m_ARTrackedImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
  89. {
  90. var aspectRatio = (float)widthInPixels / (float)heightInPixels;
  91. var sizeInMeters = new Vector2(widthInMeters, widthInMeters * aspectRatio);
  92. var referenceImage = new XRReferenceImage(
  93. // Guid is assigned after image is added
  94. SerializableGuid.empty,
  95. // No texture associated with this reference image
  96. SerializableGuid.empty,
  97. sizeInMeters, "My Image", null);
  98. var jobState = mutableLibrary.ScheduleAddImageWithValidationJob(
  99. grayscaleImageBytes,
  100. new Vector2Int(widthInPixels, heightInPixels),
  101. TextureFormat.R8,
  102. referenceImage);
  103. // Schedule a job that deallocates the image bytes after the image
  104. // is added to the reference image library.
  105. new DeallocateJob { data = grayscaleImageBytes }.Schedule(jobState.jobHandle);
  106. }
  107. else
  108. {
  109. // Cannot add the image, so dispose its memory.
  110. grayscaleImageBytes.Dispose();
  111. }
  112. }
  113. }