如何使用Unity在ARcore增强图像场景中为不同图像显示不同的预制件? [英] How to show different prefabs for different images in ARcore-Augmented Image scene using Unity?

查看:115
本文介绍了如何使用Unity在ARcore增强图像场景中为不同图像显示不同的预制件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为大约20个模型的不同图像增加不同的预制件.目前在AugmentedImage示例场景中使用2个模型对2个图像进行测试.我向每个预制件中添加了脚本AugmentedImageVisualizer.cs,我将这两个模型拖放了脚本的模型.在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预制件和Monkey预制件的预制件中添加了增幅图像可视化脚本.

这是应该怎么做的?一旦模型出现,问题就不会消失.所以当我显示下一个图像时,另一个模型就放在了它上面.如何在不跟踪图像的情况下隐藏模型?/p>

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

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

            }

下面给出的AugmentedImageVisualizer.cs代码?我已经引用了这个解决方案

问题是,在更新功能中,您始终将两个模型都设置为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;,以便退出Update函数并不再将模型设置为活动状态.

Hi I am trying to augment different prefabs for different images say around 20 models.Currently testing with 2 models for 2 images in AugmentedImage sample scene.I have added the script AugmentedImageVisualizer.cs to each prefab.I drag and dropped the two models to the script.In the AugmenetedImageExampleController.cs I have made the following changes.

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

My unity screen looks like below

Added Augmented Image Visualizer script to the prefabs to Rabbit prefab and Monkey prefab.Image given below

This is how it should be done?The problem once the model appears it will not disappear.So when I show the next image anther model comes on top of it.How to hide the model when the image is not tracked?

In the AugmentedImageControllerExample.cs we are using the below code.Still I dont understand why the models are not disappearing after they lost tracking of the image.

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

            }

AugmentedImageVisualizer.cs code given below?I have referred this Link.

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


    }
}
}

解决方案

The problem is, that in your update function you set always both models active true. But you should only set the model active you are tracking! So like said in the comment you should use the AugmentedImage DatabseIndex. For example your Models[0] is the model coresponding to the first Image in the Database and the Models[1] is coresponding to the second Image. So instead of:

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

you can write:

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

Another thing is in your if (Image != null && Image.TrackingState == TrackingState.Paused) and if (Image != null && Image.TrackingState == TrackingState.Stopped) you could write a return; after deactivating your model, so that you quit your Update function and don't set the model active again.

这篇关于如何使用Unity在ARcore增强图像场景中为不同图像显示不同的预制件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆