首页 > 解决方案 > 如何从多个渲染纹理中读取并设置纹理 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, TextureFormat.RGB24, 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, 0, cameras[i].width, cameras[i].height), 0, 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;
       
    }
}

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

我在这里先向您的帮助表示感谢!

标签: unity3drenderingtexturestexture2drender-to-texture

解决方案


推荐阅读