ARCore Unity AugmentedImage:视频无法缩放到图像目标

时间:2019-03-22 10:39:35

标签: c# unity3d arcore

我一直在尝试各种方法来实现这一目标,但我的AugmentedImageVisualizer似乎还是有问题–脚本或预制件。视频无法缩放到图像目标尺寸。这是我的设置:

我使用了ARCore增强图像示例场景,并创建了一个图像目标和平面数据库,作为AugmentedImageVisualizer预制件的子级。

结构是这样的:带有控制器脚本的示例控制器 1)AugmentedImageVisualizer预制 2)预制清单(带有VideoPlayer和AugmentedImageVisualizer脚本的飞机)

到目前为止,视频可以正常播放,但是我很难将它们分别缩放到跟踪的图像。在Controller和AugmentedImageVisualizer中进行了更改。

任何帮助和想法都非常感谢!

这是控制器:

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

还有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);

        }
    }
}

0 个答案:

没有答案