【问题标题】:Accessing Color Frames with Unity and Tango 'Leibniz'使用 Unity 和 Tango 'Leibniz' 访问色框
【发布时间】:2015-04-25 23:43:13
【问题描述】:

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

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

我现在要做的就是让“OnTangoImageAvailableEventHandler”回调工作,但我无法弄清楚。我在 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 直接访问相机(禁用深度)。但我想先学习“正确的方法”。

感谢您的宝贵时间!

更新 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!
    }
}

【问题讨论】:

标签: unity3d google-project-tango


【解决方案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();
    }
}

希望现在有效!

【讨论】:

  • 非常感谢您的帮助!我之前已经转换过 YUV,一旦一切正常,我将发布一个完整的示例脚本。它似乎已连接,但我仍然无法让回调事件正常工作。我收到警告消息:Assets/TangoSDK/Core/Scripts/TangoWrappers/VideoOverlayProvider.cs(38,31): warning CS0649: Field Tango.VideoOverlayProvider.callbackContext is never assigned to, and will always have its default value null 这可能与回调不起作用有关吗?我已经用我的脚本的当前版本更新了我最初的问题。
  • 啊哈!所以脚本也必须附加到 Unity 相机。非常感谢你的帮助!我从没想过我会这么高兴看到血红色的屏幕。一旦我让 RGB 转换工作,我会用说明更新我的代码,以供任何有兴趣的人使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多