使用Unity和Tango'Leibniz'访问颜色框架

时间:2015-04-25 23:43:13

标签: unity3d google-project-tango

我刚刚开始修补Tango和Unity。遗憾的是,似乎没有关于如何使用最新版本访问Unity中的颜色数据的任何文档或示例。

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

我现在要做的就是让“OnTangoImageAvailableEventHandler”回调正常工作,我无法弄明白。我在Tango Manager上检查了“启用视频覆盖”,并将以下脚本连接到GUI Text对象进行调试。

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直接访问摄像头(禁用深度)。但我想首先学习“正确的方法”。

感谢您的时间!

更新04/28/15 - 最新版本的脚本,回调有效!仍然需要转换为RGB颜色

此脚本是作为GitHub上Google的Tango Motion Tracking示例的补充而编写的。将脚本附加到Unity摄像头,然后将公共字段“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!
    }
}

1 个答案:

答案 0 :(得分:1)

从我所能找到的,他们改变了整个系统,因此更容易访问图像,其中所有图像抓取都由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材质上的纹理:

在抓住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();
    }
}

希望现在有效!