0

I have been trying various ways for achieving this and still something seems wrong with my AugmentedImageVisualizer – either the script or the prefab. The Videos do not scale to the image target size. Here is my setup:

I used the ARCore Augmented Image Example scene and created a database of image targets and planes as child of an AugmentedImageVisualizer prefab.

Structure is this: Example Controller with the controller script 1) The AugmentedImageVisualizer prefab 2) a list of prefabs(Planes with VideoPlayer and AugmentedImageVisualizer Script)

So far, the videos play correctly but I am having a hard time to scale them each to the tracked Images. Changes were made in the Controller and AugmentedImageVisualizer.

Any help and ideas greatly appreciated!

This is the Controller:

namespace GoogleARCore.Examples.AugmentedImage
{
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using GoogleARCore;
    using UnityEngine;
    using UnityEngine.UI;

    /// <summary>
    /// Controller for AugmentedImage example.
    /// </summary>
    public class AugmentedImageExampleController : MonoBehaviour
    {
        /// <summary>
        /// A prefab for visualizing an AugmentedImage.
        /// </summary>
        public AugmentedImageVisualizer AugmentedImageVisualizerPrefab;
        public List<AugmentedImageVisualizer> prefabs = new List<AugmentedImageVisualizer>();

        /// <summary>
        /// The overlay containing the fit to scan user guide.
        /// </summary>
        public GameObject FitToScanOverlay;


        private Dictionary<int, AugmentedImageVisualizer> m_Visualizers
            = new Dictionary<int, AugmentedImageVisualizer>();

        private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();

        /// <summary>
        /// 
        /// 
        public void Start()
        {
            AugmentedImageVisualizerPrefab.gameObject.SetActive(false);
        }
        /// The Unity Update method.
        /// </summary>
        public void Update()
        {

            // Exit the app when the 'back' button is pressed.
            if (Input.GetKey(KeyCode.Escape))
            {
                Application.Quit();
            }

            // Check that motion tracking is tracking.
            if (Session.Status != SessionStatus.Tracking)
            {
                return;
            }

            // Get updated augmented images for this frame.
            Session.GetTrackables<AugmentedImage>(m_TempAugmentedImages, TrackableQueryFilter.Updated);

            // Create visualizers and anchors for updated augmented images that are tracking and do not previously
            // have a visualizer. Remove visualizers for stopped images.
            foreach (var image in m_TempAugmentedImages)
            {
                AugmentedImageVisualizer visualizer = null;
                m_Visualizers.TryGetValue(image.DatabaseIndex, out visualizer);
                if (image.TrackingState == TrackingState.Tracking && visualizer == null)
                {
                    AugmentedImageVisualizerPrefab.gameObject.SetActive(true);
                    // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                    Anchor anchor = image.CreateAnchor(image.CenterPose);
                    visualizer = (AugmentedImageVisualizer)Instantiate(prefabs[image.DatabaseIndex], anchor.transform);
                    visualizer.Image = image;
                    m_Visualizers.Add(image.DatabaseIndex, visualizer);
                }
                else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
                {
                    m_Visualizers.Remove(image.DatabaseIndex);
                    GameObject.Destroy(visualizer.gameObject);
                }
            }

            foreach (var visualizer in m_Visualizers.Values)
            {
                // if image is Updated TimePassed is assigned to zero for that image
                if (m_TempAugmentedImages.Count != 0 && visualizer.Image == m_TempAugmentedImages[0])
                {

                    visualizer.TimePassed = 0;

                }
                else
                {
                    visualizer.TimePassed += Time.deltaTime;
                }

                if (visualizer.TimePassed > 2.0f)
                {
                    Debug.Log("Destroy is called");
                    m_Visualizers.Remove(visualizer.Image.DatabaseIndex);
                    GameObject.Destroy(visualizer.gameObject);
                }
            }

            // Show the fit-to-scan overlay if there are no images that are Tracking.
            foreach (var visualizer in m_Visualizers.Values)
            {
                if (visualizer.Image.TrackingState == TrackingState.Tracking)
                {
                    FitToScanOverlay.SetActive(false);
                    return;
                }
            }

            FitToScanOverlay.SetActive(true);
        }
    }
}

And the AugmentedImageVisualizer:

namespace GoogleARCore.Examples.AugmentedImage
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using GoogleARCore;
    using GoogleARCoreInternal;
    using UnityEngine;

    
    public class AugmentedImageVisualizer : MonoBehaviour
    {
        /// <summary>
        /// The AugmentedImage to visualize.
        /// </summary>
        public AugmentedImage Image;
        public List<GameObject> prefabs;
        public float TimePassed;



        /// <summary>
        /// The Unity Update method.
        /// </summary>
        public void Update()
        {
            if (Image == null || Image.TrackingState != TrackingState.Tracking)
            {
                prefabs[Image.DatabaseIndex].SetActive(false);

                return;
            }

            transform.localScale = new Vector3(Image.ExtentX, Image.ExtentZ, 1);

            prefabs[Image.DatabaseIndex].SetActive(true);

        }
    }
}
RefraktAR
  • 1
  • 1
  • I think the problem is in the last two lines of AI visualizer. You do not need several plane prefabs. Just have one plane prefab with a clip change the video clip based on Database index. Now you scale the plane prefab but there is another list of prefabs which does nothing. – Ali Kanat Mar 22 '19 at 12:00
  • Ok well I followed this thread: https://stackoverflow.com/a/51300819/11233534 and was happy that the videos work fine. Now even when I delete the prefabs list from the AugmentedImageVisualizer Script and have the videos child of the prefab, scaling does not seem to work. Or how would you change video clips based on the Index? – RefraktAR Mar 26 '19 at 15:58
  • You can do it following this [thread](https://gamedev.stackexchange.com/questions/149338/unity-how-to-make-an-array-of-video-clips-and-play-them) which is a similar approach to yours but instead of list of prefabs you have list of clips. Then there is just one `AIvisualizer` prefab so you can scale it. – Ali Kanat Mar 26 '19 at 16:02
  • Ah ok well I think I wasn't clear that I need to play 50 videos on 50 different image targets, which us why i had so many prefabs in the first place. Now the method above plays videos on one plane/image target. – RefraktAR Mar 27 '19 at 13:41
  • Yes but you can check id's of each image in your database so you can say for image with id 49 play video clip from video clip array with index 49 – Ali Kanat Mar 27 '19 at 14:40
  • 1
    Thanks so much for the help Ali! In the end I had to remove the list in the AugmentedVisualizer and write the update as follows – now it works fine: public void Update() { var f = Image.ExtentX / Image.ExtentZ; if (Image.ExtentX > Image.ExtentZ) { transform.localScale = new Vector3(f * Image.ExtentZ / 10, 1, Image.ExtentZ / 10); } else { transform.localScale = new Vector3(Image.ExtentX / 10, 1, f * Image.ExtentX / 10); } } – RefraktAR Apr 04 '19 at 15:57

0 Answers0