首页 文章

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

提问于
浏览
1

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

感谢您的时间!

Update 04/28/15 - Latest version of the script, callback works! Still needs a conversion to RGB color

这个脚本是作为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 回答

  • 1

    从我能找到的,他们改变了整个系统,因此更容易访问图像,其中所有图像抓取由Tango对象处理 .

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

    希望现在有效!

相关问题