Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.02k stars 1.12k forks source link

Help with MutableRuntimeReferenceImageLibrary #976

Closed ArnaudBBRI closed 2 years ago

ArnaudBBRI commented 2 years ago

Hello!

I'm trying to use a MutableRuntimeReferenceImageLibrary so that a user can upload a new QR code image at runtime, assign it to a geometry, and then the app would be able to recognize the QR code later. As said, I use for that a MutableRuntimeReferenceImageLibrary, and I'm trying to get inspiration from the BasicImageTracking sample, and the DynamicLibrary.cs script.

My problem is that the ScheduleAddImageWithValidationJob job ends up with an error that I can't understand (job.status.ToString() says "ErrorUnknown"). As far as I know, my image manager supports mutable libraries (as reported by trackedImageManager.descriptor.supportsMutableLibrary;), and the image I'm trying to add is the image 'one' taken from the BasicImageTracking sample, passed via a serialized field. That image is readable (and the error thrown by ScheduleAddImageWithValidationJob is not InvalidOperationException anyways, it is ErrorUnknown).

I wanted to go and add some debug.log inside ScheduleAddImageWithValidationJob, but for some reason when I compile, the code goes back to its original state and my debug.lo lines are erased (sorry, I'm not a developer and this is beyond my knowledge).

So, I don't know why it's not working. I'll just add here the few lines that are creating the image manager and library, maybe something's wrong there:

`// Create a ARTrackedImageManager at runtime trackedImageManager = gameObject.AddComponent(); trackedImageManager.trackedImagePrefab = null; trackedImageManager.requestedMaxNumberOfMovingImages = 2;

// Create empty XRReferenceImageLibrary et runtime RuntimeReferenceImageLibrary runtimeLibrary = trackedImageManager.CreateRuntimeLibrary(); trackedImageManager.enabled = true; // Convert it to a MutableRuntimeReferenceImageLibrary mutableRuntimeImageLibrary = runtimeLibrary as MutableRuntimeReferenceImageLibrary;`

What could cause the error? How could I track it down?

Thanks a lot!

ArnaudBBRI commented 2 years ago

I've tried to use directly the DynamicLibrary.cs in my project (I just removed the OnGUI method, and directly set State m_State = State.AddImagesRequested;), with the exact same set of Image[], and I still have the ErrorUnknow.

So I suspect it's not really an error in the code, but rather 'something' related to the library or the image manager that makes it fail. Or can it be related to the smartphone I've deployed on?

ankur-unity commented 2 years ago

can you share the stack trace of the error?

ArnaudBBRI commented 2 years ago

Hello! Thank you for your help and sorry for my lack of responsiveness.

How can I produce the stack trace of the error please? In the sense that all I can get so far is "job.status", but the code doesn't throw an exception. Here is what I got from doing the following:

if (job.status.IsError()) { Debug.Log($"Job has completed with error {job.status.ToString()}"); Debug.Log($"StackTrace: {System.Environment.StackTrace}"); }

Job has completed with error ErrorUnknown MarkerSelectionManager:AddImage(Texture2D)

d__10:MoveNext() UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) StackTrace: at System.Environment.get_StackTrace () <0x00000 + 0xffffffff> 0 in <00000000000000000000000000000000>:0 at MarkerSelectionManager.AddImage (UnityEngine.Texture2D imageToAdd) <0x00000 + 0xffffffff> 0 in <00000000000000000000000000000000>:0 at MarkerSelectionManager+d__10.MoveNext () <0x00000 + 0xffffffff> 0 in <00000000000000000000000000000000>:0 at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) <0x00000 + 0xffffffff> 0 in <00000000000000000000000000000000>:0 MarkerSelectionManager:AddImage(Texture2D) d__10:MoveNext() UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
tdmowrer commented 2 years ago

the image I'm trying to add is the image 'one' taken from the BasicImageTracking sample

Does the unmodified sample work?

ErrorUnknown probably means the ARCore or ARKit function rejected the image in a way that doesn't map to an error code in AR Foundation. There might be more specific error information in logcat (Android) or Xcode console (iOS). That output might help Unity track it down (instead of a stack trace).

ArnaudBBRI commented 2 years ago

Does the unmodified sample work?

Not really. It runs and prints "All images added", but they are not really being added. If I add a few log lines like this in the Update method of DynamicLibrary: image

... it will stay in the while loop until the end, then only print the 3 initial images that were in the Library. But it's different from my problem indeed, since the job status is not Error!

I will try to get some more log info via logcat as well.

ArnaudBBRI commented 2 years ago

To add to this: the sample works correctly when using the obsolete method 'ScheduleAddImageJob' instead of 'ScheduleAddImageWithValidationJob'

tdmowrer commented 2 years ago

It runs and prints "All images added", but they are not really being added.

I don't know what you mean by "not really being added". Can your device detect the added images or not? If it can't detect them when running the unmodified sample, then it should be easy for Unity to reproduce the issue.

If I add a few log lines like this in the Update method of DynamicLibrary...

What is this meant to test? The job hasn't necessarily completed, just a certain number of iterations of a spin-wait, which I don't think tells you anything useful. You should use JobHandle.Complete if your intention is to wait for the job to complete before continuing main thread execution.

I will try to get some more log info via logcat as well.

This is probably the only way to get any meaningful information about the issue from your end. You should do that :)

ArnaudBBRI commented 2 years ago

Well.. You're right 🥲 So, the sample was working correctly, indeed.

And I solved my problem, which was due to other errors on my side.

Thanks a lot for your help, and sorry for the inconvenience! Arnaud

tdmowrer commented 2 years ago

Glad you solved it! For future readers of this thread, could you provide more details? What was the problem and what did you do to fix it?

ArnaudBBRI commented 2 years ago

I had tweaked the sample a bit, and I was actually playing with 2 ImageLibraries in my project. One of these libraries was being correctly filled, and I was checking the other one 🤦‍♂️ . So nothing was wrong, I was just doing stupid things. Hopefully nobody else will do that 😄

tdmowrer commented 2 years ago

Hmm, I don't think that explains your original post:

My problem is that the ScheduleAddImageWithValidationJob job ends up with an error that I can't understand (job.status.ToString() says "ErrorUnknown").

That sounds like it was really rejecting your image for some reason. Did you ever find out why?

ArnaudBBRI commented 2 years ago

You're right! I did not find why yet. I admit I changed a lot of things a bit erratically, I'd need to have a second look to find out what wasn't working. I'll post it here asap.