Unity:特定Display的Texture2D ReadPixels
Unity: Texture2D ReadPixels for specific Display
Unity 支持多个显示输出已有一段时间了(最多 8 个)。
使用ReadPixels
函数,您可以指定要读取的区域和原点坐标。但是我无法指定显示 编号来执行读取。
我需要能够从具有特定区域和原点的特定显示器 (1-8) 读取像素。
请问我该怎么做?
您可以实现特定 screen/display 的 ReadPixels。您必须执行以下操作:
在开始之前,我假设您有许多摄像机,每个摄像机都渲染到不同的显示器。相机不得附加 RenderTexture 才能输出到显示器。
定义一个执行以下操作的函数:
- 为所需的相机分配一个临时 RenderTexture
- 使用
RenderTexture.active = *temporary render texture*
使当前活动的渲染纹理等于您刚刚创建的临时渲染纹理
- 使用
ReadPixels
使用适当的 Rect
将像素读入临时 texture2d
。这将从当前活动的 RenderTexture
读取
- 在
texture2d
上致电 Apply()
- 将
RenderTexture.active
和相机 RenderTexture
设置为 null
想法是 ReadPixels 在当前活动的 RenderTexture 上工作。
代码应如下所示:
outputCam.targetTexture = outputCamRenderTexture;
RenderTexture.active = outputCamRenderTexture;
outputCam.Render ();
tempResidualTex.ReadPixels (screenRect, 0, 0);
tempResidualTex.Apply ();
RenderTexture.active = null;
outputCam.targetTexture = null;
Unity 支持多个显示输出已有一段时间了(最多 8 个)。
使用ReadPixels
函数,您可以指定要读取的区域和原点坐标。但是我无法指定显示 编号来执行读取。
我需要能够从具有特定区域和原点的特定显示器 (1-8) 读取像素。
请问我该怎么做?
您可以实现特定 screen/display 的 ReadPixels。您必须执行以下操作:
在开始之前,我假设您有许多摄像机,每个摄像机都渲染到不同的显示器。相机不得附加 RenderTexture 才能输出到显示器。
定义一个执行以下操作的函数:
- 为所需的相机分配一个临时 RenderTexture
- 使用
RenderTexture.active = *temporary render texture*
使当前活动的渲染纹理等于您刚刚创建的临时渲染纹理 - 使用
ReadPixels
使用适当的Rect
将像素读入临时texture2d
。这将从当前活动的RenderTexture
读取
- 在
texture2d
上致电 - 将
RenderTexture.active
和相机RenderTexture
设置为null
Apply()
想法是 ReadPixels 在当前活动的 RenderTexture 上工作。
代码应如下所示:
outputCam.targetTexture = outputCamRenderTexture;
RenderTexture.active = outputCamRenderTexture;
outputCam.Render ();
tempResidualTex.ReadPixels (screenRect, 0, 0);
tempResidualTex.Apply ();
RenderTexture.active = null;
outputCam.targetTexture = null;