首页 > 解决方案 > 如何使用 Unity 在 ARcore-Augmented Image 场景中显示不同图像的不同预制件?

问题描述

嗨,我正在尝试为不同的图像增加不同的预制件,比如大约 20 个模型。目前在 AugmentedImage 示例场景中使用 2 个模型测试 2 个图像。我已将脚本 AugmentedImageVisualizer.cs 添加到每个预制件中。我将两个模型拖放到script.In AugmenetedImageExampleController.cs 我做了以下更改。

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> AugmentedImageVisualizerPrefab = 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>
    /// 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)
            {
                // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                Anchor anchor = image.CreateAnchor(image.CenterPose);
                visualizer = (AugmentedImageVisualizer)Instantiate(AugmentedImageVisualizerPrefab[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);

            }

        }

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

我的统一屏幕如下所示 统一屏幕视图

在 Rabbit prefab 和 Monkey prefab 的 prefabs 中添加了 Augmented Image Visualizer 脚本。下面给出的图像 Prefab-Inspector

这应该怎么做?问题一旦模型出现它就不会消失。所以当我显示下一张图像时,另一个模型会出现在它上面。当图像没有被跟踪时如何隐藏模型?

在 AugmentedImageControllerExample.cs 中,我们使用了下面的代码。我仍然不明白为什么模型在失去对图像的跟踪后没有消失。

else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
            {
                m_Visualizers.Remove(image.DatabaseIndex);
                GameObject.Destroy(visualizer.gameObject);

            }

下面给出的 AugmentedImageVisualizer.cs 代码?我已经提到了这个链接

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

/// <summary>
/// Uses 4 frame corner objects to visualize an AugmentedImage.
/// </summary>
public class AugmentedImageVisualizer : MonoBehaviour
{
    /// <summary>
    /// The AugmentedImage to visualize.
    /// </summary>
    public AugmentedImage Image;


    public GameObject[] Models;





    private void Start()
    {

    }



    /// <summary>
    /// A model for the lower left corner of the frame to place when an image is detected.
    /// </summary>
    // public GameObject FrameLowerLeft;

    /// <summary>
    /// A model for the lower right corner of the frame to place when an image is detected.
    /// </summary>
    // public GameObject FrameLowerRight;

    /// <summary>
    /// A model for the upper left corner of the frame to place when an image is detected.
    /// </summary>
    // public GameObject FrameUpperLeft;

    /// <summary>
    /// A model for the upper right corner of the frame to place when an image is detected.
    /// </summary>
    //  public GameObject FrameUpperRight;

    /// <summary>
    /// The Unity Update method.
    /// </summary>
    public void Update()
    {
         if (Image == null || Image.TrackingState != TrackingState.Tracking)

        {
            Models[Image.DatabaseIndex].SetActive(false);

            //Models[0].SetActive(false);
            //Models[1].SetActive(false);

            return;
        }

        if (Image == null || Image.TrackingState == TrackingState.Stopped)
            {
            Models[Image.DatabaseIndex].SetActive(false);

            //Models[0].SetActive(false);
            //Models[1].SetActive(false);

            return;

             }

        if (Image == null || Image.TrackingState == TrackingState.Paused)
        {
            Models[Image.DatabaseIndex].SetActive(false);

            //Models[0].SetActive(false);
            //Models[1].SetActive(false);

            return;

        }



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


    }
}
}

标签: unity3daugmented-realityarcore

解决方案


问题是,在您的更新功能中,您始终将两个模型都设置为 true。但是您应该只将您正在跟踪的模型设置为活动!所以就像评论中所说的,你应该使用 AugmentedImage DatabseIndex。例如,您Models[0]的模型对应于数据库中的第一个图像,并且Models[1]对应于第二个图像。所以而不是:

// Wrong code, because you're always setting both models active
Models[0].SetActive(true);
Models[1].SetActive(true);

你可以写:

// Only set the tracking Image active
Models[Image.DatabaseIndex].SetActive(true);

另一件事是在您的模型中if (Image != null && Image.TrackingState == TrackingState.Paused)if (Image != null && Image.TrackingState == TrackingState.Stopped)您可以在停用模型后编写一个return;,以便您退出更新功能并且不要再次将模型设置为活动状态。


推荐阅读