仅从Unity编辑器获取ARcore相机纹理

时间:2019-03-15 03:05:12

标签: unity3d camera textures arcore

我正在使用ARcore开发项目。

我需要一个现实世界的屏幕,该屏幕以前在ARcore相机上可见,以前使用的是擦除UI和捕获的方法。

但是它是如此之慢,以至于我在Arcore API中找到了Frame.CameraImage.Texture

Texture2D snap = (Texture2D)Frame.CameraImage.Texture;

它在Unity编辑器环境中正常工作。

但是,如果您将其构建在手机上并签出,则Texture为null。

我知道这个问题是因为在手机上无法访问CPU。

所以我找到了另一种方法,然后找到了AcquireCameraImageBytes()。

LoadRawTextureData() not enough data provided error in Unity

存在与上一个问题一样多的问题,但是当YUV格式转换为RGB格式时,问题得以解决。

但是,它只能在Unity Editor中正常工作,并且当用手机构建时,会输出灰色图像。

enter image description here

这是我的完整代码

namespace GoogleARCore
{
    using System;
    using UnityEngine;
    using UnityEngine.UI;

    public class TestFrameCamera : MonoBehaviour
    {
        private Texture2D m_TextureRender;

        private void Update()
        {
            using (var image = Frame.CameraImage.AcquireCameraImageBytes())     { 
                if(!image.IsAvailable)
                {
                    return;
                }

                _OnImageAvailable(image.Width, image.Height, image.Y, image.Width * image.Height);
            }

        }
        private void _OnImageAvailable(int width, int height, IntPtr pixelBuffer, int bufferSize)
        {

            m_TextureRender = new Texture2D(width, height, TextureFormat.RGBA32, false, false);
            byte[] bufferYUV = new byte[width * height * 3 / 2];
            bufferSize = width * height * 3 / 2;
            System.Runtime.InteropServices.Marshal.Copy(pixelBuffer, bufferYUV, 0, bufferSize);
            Color color = new Color();
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {

                    float Yvalue = bufferYUV[y * width + x];
                    float Uvalue = bufferYUV[(y / 2) * (width / 2) + x / 2 + (width * height)];
                    float Vvalue = bufferYUV[(y / 2) * (width / 2) + x / 2 + (width * height) + (width * height) / 4];
                    color.r = Yvalue + (float)(1.37705 * (Vvalue - 128.0f));
                    color.g = Yvalue - (float)(0.698001 * (Vvalue - 128.0f)) - (float)(0.337633 * (Uvalue - 128.0f));
                    color.b = Yvalue + (float)(1.732446 * (Uvalue - 128.0f));

                    color.r /= 255.0f;
                    color.g /= 255.0f;
                    color.b /= 255.0f;

                    if (color.r < 0.0f)
                        color.r = 0.0f;
                    if (color.g < 0.0f)
                        color.g = 0.0f;
                    if (color.b < 0.0f)
                        color.b = 0.0f;

                    if (color.r > 1.0f)
                        color.r = 1.0f;
                    if (color.g > 1.0f)
                        color.g = 1.0f;
                    if (color.b > 1.0f)
                        color.b = 1.0f;

                    color.a = 1.0f;
                    m_TextureRender.SetPixel(width - 1 - x, y, color);
                }
            }


            m_TextureRender.Apply();
            this.GetComponent<RawImage>().texture = m_TextureRender;
        }
    }
}

我只是通过将该脚本添加到ARCore示例代码中包含Rawimage的对象中来对该脚本进行测试。

我该怎么办?

0 个答案:

没有答案