Search code examples
c#unity-game-enginearcore

Can't add images to AugmentedImageDatabase in ARCore


I'm writing an application in unity. In the future I need to be able to pull images from the web and use them as trackers, so I'm currently writing code that adds images from a list to the AugmentedImageDatabase at runtime.

Every time I add an image however, the AugmentedImageDatabase.AddImage() method returns a -1. This means there was an error adding the image, but it doesn't say what kind of error. I've checked the API docs, but they don't add any information either.

Why doesn't my code add images to the AugmentedImageDatabase?

public class DataBaseGenerator : MonoBehaviour {
    [SerializeField]
    ARCoreSessionConfig session;

    [SerializeField]
    AugmentedImageDatabase imageDatabase;

    [SerializeField]
    List<Texture2D> image;

    private int ErrorIndex = 0;

    // What happens on the first frame
    void Start ()
    {
        CreateDatabase();
    }
    private void CreateDatabase()
    {
        int i = 0;
        foreach (Texture2D texture in image)
        {
            string name = "Tracker";
            Texture2D rgbTexture = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
            rgbTexture.SetPixels(texture.GetPixels());
            rgbTexture.Apply();

            ErrorIndex = imageDatabase.AddImage(name, rgbTexture, 0);
            GameUtility.ShowToastMessage(ErrorIndex.ToString());
            Debug.Log(name + ": " + ErrorIndex);
            i++;
        }
        session.AugmentedImageDatabase = imageDatabase;
    }
}

All the images in the list have been saved as Sprites. All variables with the [SerializeField] tag are defined in the unity editor.


Solution

  • Since OP did not reply back i am assuming my comment solved the problem and converting my comment to an answer.

    ARCore only supports two texture formats(RGBA32 or RGB24) for Augmented Image Database. Therefore, the texture have to be converted first in order to be able to add images to database.

    Second problem in OP's code was he was trying to add images to database in Start which obviously gets executed when the application is started. Therefore, the session status is either None or Initializing which results in LifecycleManager.Instance.NativeSession returning null. Since there is no Session at the moment you can not add images to the Database.