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);
}
}
}