// Adds a reference image to the library. public async void AddReferenceImage(MutableRuntimeReferenceImageLibrary referenceImageLibrary) { //Debug.Log("Adding the reference texture: " + imageName); Texture2D readableTexture = Utils.GameUtils.CreateReadableTexture(this.textureImage); AddReferenceImageJobState job = referenceImageLibrary.ScheduleAddImageWithValidationJob( readableTexture, this.imageName, 1.0f); await Task.Run(() => { while (!job.jobHandle.IsCompleted) { ; } Debug.Log("Finished adding reference image: " + this.imageName); Debug.Log("Number Of Reference Images: " + referenceImageLibrary.count); }); }
/// <summary> /// Asynchronously adds <paramref name="texture"/> to <paramref name="library"/>. /// </summary> /// <remarks> /// Image addition can take some time (several frames) due to extra processing that must occur to insert the /// image into the library. This is done using the [Unity Job System](xref:JobSystem). The returned /// [AddReferenceImageJobState](xref:UnityEngine.XR.ARSubsystems.AddReferenceImageJobState) can be used to query /// for job completion and whether the addition was successful. /// /// The bytes of the <paramref name="texture"/> are copied, so the texture may be safely /// destroyed after this method returns. /// </remarks> /// <param name="library">The <see cref="MutableRuntimeReferenceImageLibrary"/> being extended.</param> /// <param name="texture">The [Texture2D](xref:UnityEngine.Texture2D) to use as image target.</param> /// <param name="name">The name of the image.</param> /// <param name="widthInMeters">The physical width of the image, in meters.</param> /// <param name="inputDeps">Input job dependencies (optional).</param> /// <returns>Returns an [AddReferenceImageJobState](xref:UnityEngine.XR.ARSubsystems.AddReferenceImageJobState) /// that can be used to query the status of the job. The `AddReferenceImageJobState` can be used to /// determine whether the image was successfully added. If image validity can be determined, invalid images /// will be not be added to the reference image library.</returns> /// <exception cref="System.InvalidOperationException">Thrown if <paramref cref="library"/> is `null`.</exception> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="texture"/> is `null`.</exception> /// <exception cref="System.InvalidOperationException">Thrown if <paramref name="texture"/> is not readable.</exception> public static AddReferenceImageJobState ScheduleAddImageWithValidationJob( this MutableRuntimeReferenceImageLibrary library, Texture2D texture, string name, float?widthInMeters, JobHandle inputDeps = default) { if (ReferenceEquals(library, null)) { throw new ArgumentNullException(nameof(library)); } if (texture == null) { throw new ArgumentNullException(nameof(texture)); } if (!texture.isReadable) { throw new InvalidOperationException("The texture must be readable to be used as the source for a reference image."); } var imageBytesCopy = new NativeArray <byte>(texture.GetRawTextureData <byte>(), Allocator.Persistent); try { Vector2?size = null; if (widthInMeters.HasValue) { size = new Vector2(widthInMeters.Value, widthInMeters.Value * texture.height / texture.width); } var referenceImage = new XRReferenceImage(SerializableGuid.empty, SerializableGuid.empty, size, name, texture); var state = library.ScheduleAddImageWithValidationJob(imageBytesCopy, new Vector2Int(texture.width, texture.height), texture.format, referenceImage, inputDeps); new DeallocateJob { data = imageBytesCopy }.Schedule(state.jobHandle); return(state); } catch { imageBytesCopy.Dispose(); throw; } }