如何从多个渲染纹理中读取并设置纹理 2d 值 Unity 3D

问题描述

所以我的想法是在统一的纹理上创建一个实时安全摄像头系统。这个想法是使用分散在整个场景中的多个统一相机,并将它们全部渲染到 RenderTexture。然后组合到 1 个 2D 纹理上,然后我可以用它覆盖屏幕或附加到场景中的对象。 (很可能只是覆盖屏幕)

那么我的问题就变成了如何从 Unity 脚本中的多个 RenderTexture 中读取?然后如何将图像块写入 scipt 中的 texture2D 以及图像的片段?

这就是我目前正在做的

public class Cameras : MonoBehavIoUr
{
    public RawImage outText;
    public RenderTexture[] cameras;
    public int imgWidth;
    public int imgHeight;

    void Start()
    {
        Texture2D[] textures = new Texture2D[cameras.length];
        for(int i = 0; i < cameras.length; ++i)
        {
            textures[i] = new Texture2D(cameras[i].width,cameras[i].height,TextureFormat.RGB24,false);
        }
        Texture2D Combined = new Texture2D(imgWidth,imgHeight,false);
    }
    void Update()
    {
        for(int i = 0; i < cameras.length; ++i)
        {
            //is this a decent way?
            RenderTexture.active = cameras[i];
            textures[i].ReadPixels (new Rect (0,cameras[i].width,cameras[i].height),0);
            textures[i].Apply ();
            RenderTexture.active = null;
            Color32[] camPixels= textures[i].GetPixels32(0);
            /* someway to combine it?
            for(int i = camOffset; i < camBlock.width; ++i)
            {
                for(int j = camOffset; j < camBlock.height; ++j)
                {
                Combined.SetPixel(i,j,camPixels);
                }
            }
            */
        }
       
        outText.texture = Combined;
       
    }
}

作为一个后续问题,假设我想做一些效果。我将如何单独写入组合纹理的红色通道或绿色通道?

提前感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)