使用 Unity 和 Tango 访问颜色帧 'Leibniz'

Accessing Color Frames with Unity and Tango 'Leibniz'

我刚刚开始研究 Tango 和 Unity。不幸的是,似乎没有关于如何在最新版本的 Unity 中访问颜色数据的任何文档或示例。

我一直在使用 GitHub (https://github.com/googlesamples/tango-examples-unity) 中的运动跟踪示例作为起点,尝试以与读取姿势和深度数据相同的方式读取传入的颜色帧。我假设最好的方法是通过 "ITangoVideoOverlay" 接口和 "OnTangoImageAvailableEventHandler" 回调。

我现在想做的就是让 "OnTangoImageAvailableEventHandler" 回调正常工作,但我不太明白。我已经 "Enable Video Overlay" 检查了 Tango 管理器和以下连接到 GUI 文本对象以进行调试的脚本。

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Tango;
using System;

public class VideoController : MonoBehaviour, ITangoVideoOverlay
{

    private TangoApplication m_tangoApplication;

    private bool debugB = false;
    public Text debugText;

    // Use this for initialization
    void Start () {
        m_tangoApplication = FindObjectOfType<TangoApplication>();
        m_tangoApplication.Register(this);
    }

    // Update is called once per frame
    void Update () {
        if (debugB)
            debugText.text = "TRUE";
        else
            debugText.text = "FALSE";
    }

    // No Unity API
    public void OnTangoImageAvailableEventHandler(Tango.TangoEnums.TangoCameraId id, Tango.TangoUnityImageData image)
    {
        debugB = true;
    }
}

我是否缺少一些相机初始化?还是首选方法仍然像在这个旧代码中那样使用 VideoOverlayListener:Getting color data in Unity

我知道也可以通过 Unity 直接访问相机(禁用深度)。但是我想先学习"proper way"。

感谢您的宝贵时间!

2015 年 4 月 28 日更新 - 脚本的最新版本,回调有效!仍然需要转换为 RGB 颜色

此脚本是作为 Google 在 GitHub 上的 Tango 运动跟踪示例的补充编写的。将脚本附加到 Unity 摄像机,然后 link public 字段 "m_viewScreen" 到网格对象(如平面)以显示视频纹理。

using System.Collections;
using UnityEngine;
using Tango;
using System;

public class VideoController : MonoBehaviour
{
    private TangoApplication m_tangoApplication;
    private Texture2D m_texture;
    private Material m_screenMaterial;
    private MeshFilter m_meshFilter;
    private bool m_readyToDraw = false;

    // Link to a mesh object for displaying texture
    public GameObject m_viewScreen;

    // Use this for initialization
    void Start ()
    {
        // Tango initilization
        m_tangoApplication = FindObjectOfType<TangoApplication>();
        m_tangoApplication.Register(this);
        m_tangoApplication.RegisterPermissionsCallback(_OnTangoApplicationPermissionsEvent);

        // Initialize view object Material
        m_meshFilter = m_viewScreen.GetComponent<MeshFilter> ();
        m_screenMaterial = new Material(Shader.Find("Mobile/Unlit (Supports Lightmap)"));

        // Begin to texture to webcam
        m_texture = m_tangoApplication.GetVideoOverlayTexture();
        m_texture.Apply();

        if (m_screenMaterial != null)
        {
            // Apply the texture
            m_screenMaterial.mainTexture = m_texture;
            m_meshFilter.GetComponent<MeshRenderer>().material = m_screenMaterial;

            // Connect the texture to the camera
            if (m_tangoApplication.m_useExperimentalVideoOverlay)
            {
                VideoOverlayProvider.ExperimentalConnectTexture(TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR, m_texture.GetNativeTextureID(), _OnUnityFrameAvailable);
            }
            else
            {
                VideoOverlayProvider.ConnectTexture(TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR, m_texture.GetNativeTextureID());
            }
        }
    }

    private void _OnTangoApplicationPermissionsEvent(bool permissionsGranted)
    {
        m_readyToDraw = true;
    }

    private void _OnUnityFrameAvailable(System.IntPtr callbackContext, Tango.TangoEnums.TangoCameraId cameraId)
    {
        // Do fun stuff here!

    }

    void OnPreRender()
    {
        if (m_readyToDraw)
        {
            VideoOverlayProvider.RenderLatestFrame(TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR);
            GL.InvalidateState();
        }
    }

    void Update ()
    {
        // Do other fun stuff here!
    }
}

据我所知,他们更改了整个系统,因此更容易访问图像,其中所有图像抓取都由 Tango 对象处理。

获取 Tango 应用程序后,在“开始”中尝试以下操作:

m_texture = m_tangoApplication.GetVideoOverlayTexture();
m_texture.Apply();

if (m_screenMaterial != null)
{
    // Apply the texture
    m_screenMaterial.mainTexture = m_texture;
    m_meshFilter.GetComponent<MeshRenderer>().material = m_screenMaterial;

    // Connect the texture to the camera
    if (m_tangoApplication.m_useExperimentalVideoOverlay)
    {
        VideoOverlayProvider.ExperimentalConnectTexture(TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR, m_texture.GetNativeTextureID(), _OnUnityFrameAvailable);
    }
    else
    {
        VideoOverlayProvider.ConnectTexture(TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR, m_texture.GetNativeTextureID());
    }

}

你在其他地方需要这个回调事件:

private void _OnUnityFrameAvailable(System.IntPtr callbackContext, Tango.TangoEnums.TangoCameraId cameraId)
{
    // Do fun stuff here!
}

但它实际上不需要做任何事情。当然,图像仍然是 YUV-NV12 格式,因此您需要将其转换为 RGB(或者等到他们的下一个版本应该修复它)。

编辑:糟糕!忘记了您需要再调用一次才能实际更新 AR 上的纹理 material:

获取 TangoApp 后在 Start() 中:

m_tangoApplication.RegisterPermissionsCallback(_OnTangoApplicationPermissionsEvent);

然后:

private void _OnTangoApplicationPermissionsEvent(bool permissionsGranted)
{
    m_readyToDraw = true;
}

void OnPreRender()
{
    if (m_readyToDraw)
    {
        VideoOverlayProvider.RenderLatestFrame(TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR);
        GL.InvalidateState();
    }
}

希望现在有用!