123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System.Collections;
- using System.Collections.Generic;
- using Unity.Collections;
- using Unity.Jobs;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.XR.ARFoundation;
- using UnityEngine.XR.ARSubsystems;
- public class AddARTrackedManager : MonoBehaviour
- {
- [SerializeField]
- ARTrackedImageManager m_ARTrackedImageManager;
- [SerializeField]
- Text text;
- // [SerializeField]
- List<Texture2D> image;
-
- public ImageLibraryTest imageLibraryTest;
-
- private void Start()
- {
- image = new List<Texture2D>();
- Texture2D texture = (Texture2D)Resources.Load<Texture>("Image/1");
- image.Add(texture);
- //Debug.Log(texture.name);
- //for (int i = 1; i < 6; i++)
- //{
- // string url = "Image/" + i.ToString();
- // Debug.Log(url);
- // Texture2D texture = (Texture2D)Resources.Load<Texture>(url);
- // image.Add(texture);
- // Debug.Log(texture.name);
- //}
- StartCoroutine(ADDTrackedImage(10f));
-
- // AddImage(texture.EncodeToJPG(), texture.width, texture.height, 10);
- }
- IEnumerator ADDTrackedImage(float times)
- {
- text.text = " 即将动态添加TrackedImage ";
- yield return times;
- text.text = "动态添加TrackedImage中";
- if (m_ARTrackedImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
- {
- for (int i = 0; i < image.Count; i++)
- {
- Debug.Log(image[i].name);
- mutableLibrary.ScheduleAddImageWithValidationJob(image[i], image[i].name, image[i].width);
- }
- text.text = " 添加完毕 ";
- }
- else
- text.text = " 添加失败 ";
- //imageLibraryTest.enabled = true;
- }
- public void Add(List<Texture2D> image)
- {
- Debug.Log(image.Count);
- if (m_ARTrackedImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
- {
- for (int i = 0; i < image.Count; i++)
- {
- Debug.Log(image[i].name);
- mutableLibrary.ScheduleAddImageWithValidationJob(image[i], image[i].name, image[i].width);
- }
- }
- }
- void AddImage(Texture2D imageToAdd)
- {
- Debug.Log(imageToAdd.name);
- var library = m_ARTrackedImageManager.CreateRuntimeLibrary();
- if (library is MutableRuntimeReferenceImageLibrary mutableLibrary)
- {
- Debug.Log(imageToAdd.name);
- mutableLibrary.ScheduleAddImageWithValidationJob(
- imageToAdd,
- "my new image",
- 0.5f /* 50 cm */);
- }
- }
- struct DeallocateJob : IJob
- {
- [DeallocateOnJobCompletion]
- public NativeArray<byte> data;
- public void Execute() { }
- }
- void AddImage(NativeArray<byte> grayscaleImageBytes,
- int widthInPixels, int heightInPixels,
- float widthInMeters)
- {
- if (m_ARTrackedImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
- {
- var aspectRatio = (float)widthInPixels / (float)heightInPixels;
- var sizeInMeters = new Vector2(widthInMeters, widthInMeters * aspectRatio);
- var referenceImage = new XRReferenceImage(
- // Guid is assigned after image is added
- SerializableGuid.empty,
- // No texture associated with this reference image
- SerializableGuid.empty,
- sizeInMeters, "My Image", null);
- var jobState = mutableLibrary.ScheduleAddImageWithValidationJob(
- grayscaleImageBytes,
- new Vector2Int(widthInPixels, heightInPixels),
- TextureFormat.R8,
- referenceImage);
- // Schedule a job that deallocates the image bytes after the image
- // is added to the reference image library.
- new DeallocateJob { data = grayscaleImageBytes }.Schedule(jobState.jobHandle);
- }
- else
- {
- // Cannot add the image, so dispose its memory.
- grayscaleImageBytes.Dispose();
- }
- }
-
- }
|