ARFoundation from scratch 5-AR image tracking

Taking an object or medium in the real world as a reference object, fixing the AR content to the relative position of the reference object, and changing the relative position with the movement of the reference object is the so-called AR tracking.

2D image tracking (ARKit and ARCore), by detecting specific 2D images in the environment, Tracked Image Manager can automatically create GameObjects representing all recognized images, allowing you to modify the AR experience based on specific images.

Sample source code for this project:

https://github.com/sueleeyu/ar-augment

1. Create a project project

1. Create a new project

2. Rename it to ImagesScene, select Window-Package Manager- Unity Register, and download in sequence:

XR Plugin Management 4.0.7

AR Foundation 4.1.10

ARCore XR Plugin 4.1.10

ARKit XR Plugin 4.1.10

3. As follows:

2. Add AR components

1. Delete the original camera, click "+" to add Session Origin, AR Session:

2. Select AR Session Origin, click Add Component in the Inspector, enter art in the search box, and select AR Tracked Image Manager to add.

3. Add interface UI and model

1.log Text, used to display log information: Hierarchy-'+'- UI –Text, create a new Text component, name it Log, set:

2. Add Button, right click Hierarchy-Canvas, UI-Button, set:

Button-Text settings:

4. Create a reference library

1. Right-click on Project –Assets, Create-Folder, create a new folder, and name it Images:

2. Create a reference library, right click on Project –Assets –Images, Create-XR-Reference Image Library:

3. Copy multiple pictures to the Images directory:

4. Add a reference image, select ReferenceImageLibrary, and click Add Image on the right Inspector

5. Drag the picture to the texture on the right, and complete the addition of reference pictures in sequence:

Add the following:

 

The meaning of the attributes in the reference figure:

Name: Each reference image has a unique guid and a name. Name can be used to match detected images and reference images in real time

Specify Size: Specify the size of the picture, which needs to be specified to be recognized under some platforms, so it is best to select all of them.

Keep Texture at Runtime: Whether to use texture, if you want to modify the appearance of Prefab, you can select it.

6. Add the Reference Library attribute to AR Tracked Image Manager: Select Hierarchy-AR Session Origin, select the dot on the right side of Serialized Library in the AR Tracked Image Manager item of the Inspector on the right, and click the corresponding item in the list to add:

5. Required pictures and prefabs

1. Create a new Cube under the Hierarchy, name it One (corresponding to the name of the image in the reference library), and drag One.png under Images to the Cube in the scene:

 

2. Create a new Resources/Prefabs folder under Assets, drag cube One to Prefabs, delete One in the scene, and make others in sequence.

6. Write scripts

1. Right click on Project-Assets, create a new Scripts folder, and create a new Logger.cs to view the log

 

2. Logger mount

code:

using System.Collections.Generic;
using System.Text;
using UnityEngine.UI;

namespace UnityEngine.XR.ARFoundation.Samples
{
    public class Logger : MonoBehaviour
    {
        [SerializeField]
        Text m_LogText;//挂载到Log Text组件
        public Text logText
        {
            get => m_LogText;
            set => m_LogText = value;
        }

        [SerializeField]
        int m_VisibleMessageCount = 40;
        public int visibleMessageCount
        {
            get => m_VisibleMessageCount;
            set => m_VisibleMessageCount = value;
        }

        int m_LastMessageCount;

        static List<string> s_Log = new List<string>();

        static StringBuilder m_StringBuilder = new StringBuilder();

        void Awake()
        {
            if (m_LogText == null)
            {
                m_LogText = GetComponent<Text>();
            }

            lock (s_Log)
            {
                s_Log?.Clear();
            }

            Log("Log console initialized.");
        }

        void Update()
        {
            lock (s_Log)
            {
                if (m_LastMessageCount != s_Log.Count)
                {
                    m_StringBuilder.Clear();
                    var startIndex = Mathf.Max(s_Log.Count - m_VisibleMessageCount, 0);
                    for (int i = startIndex; i < s_Log.Count; ++i)
                    {
                        m_StringBuilder.Append($"{i:000}> {s_Log[i]}\n");
                    }

                    var text = m_StringBuilder.ToString();

                    if (m_LogText)
                    {
                        m_LogText.text = text;
                    }
                    else
                    {
                        Debug.Log(text);
                    }
                }

                m_LastMessageCount = s_Log.Count;
            }
        }

        public static void Log(string message)
        {
            lock (s_Log)
            {
                if (s_Log == null)
                    s_Log = new List<string>();

                s_Log.Add(message);
            }
        }
    }
}

 

do the association:

 

3. Create a new MultiImageTracking.cs and write the code.

Load the prefab, in Strat:

void Start()

        {

            // load the prefab

            GameObject one = Resources.Load<GameObject>("Prefabs/One");

            prefabs.Add("One", one);

            GameObject two= Resources.Load<GameObject>("Prefabs/Two");

            prefabs.Add("Two", two);

            GameObject qrcode = Resources.Load<GameObject>("Prefabs/QRCode");

            prefabs.Add("QRCode", qrcode);

            Logger.Log("MultiImageTracking-Start");

        }

4. Register the ARTrackedImagesChangedEventArgs event:

private void OnEnable()

        {

            ImgTrackedManager.trackedImagesChanged += OnTrackedImagesChanged;

        }

Implement loading in OnTrackedImagesChanged :

void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)

        {

            foreach (var trackedImage in eventArgs.added)

            {

                // Give the initial image a reasonable default scale

                var minLocalScalar = Mathf.Min(trackedImage.size.x, trackedImage.size.y) / 2;

                trackedImage.transform.localScale = new Vector3(minLocalScalar, minLocalScalar, minLocalScalar); // Scale the model

                Instantiate(prefabs[trackedImage.referenceImage.name], trackedImage.transform); // Instantiate the prefab

                //OnImagesChanged(trackedImage);

            }           

        }

5. Mount MultiImageTracking.cs to AR Session Origin, select AR Session Origin, click Add Component in the Inspector, enter and select MultiImageTracking.cs, and drag the Button-Text component to the Toggle Text property bar of MultiImageTracking:

 

Seven, android packaging

1. Run:

   

8. Frequently asked questions

1. ARFoundration packaging will report an error: "Failded to generated ARCore reference image library"

The reason for the error should be that the image quality does not meet the requirements.

9. References

1. Unity api:

Unity - Manual: Unity User Manual 2021.3 (LTS)

2. ARFoundation example:

GitHub - Unity-Technologies/arfoundation-samples: Example content for Unity projects based on AR Foundation

3. ARCore documentation

Adding Dimensions to Images | ARCore | Google Developers

4. Sample source code of this project:

sueleeyu/ar-augment · GitHub

Guess you like

Origin blog.csdn.net/weixin_40239288/article/details/125299614