遇到 Post 处理效果的问题

Having Trouble With Post Processing Effects

我正在尝试编写一个 post 处理效果来改变最终绘制到屏幕上的内容。但是,这需要获取当前正在绘制的内容,对其进行修改,然后再将其绘制回来。

前两个步骤似乎工作得很好。第三步....渲染全灰

public class PostProcess : MonoBehaviour {
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        Texture2D proc = new Texture2D(src.width, src.height);
        proc.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
        Color[] line = proc.GetPixels(src.width / 2, 0, 1, src.height);
        Debug.Log(line[0] + " : "  + line[line.Length-1]);
        proc.Apply(); //per comments
        Graphics.Blit(proc, dest);
    }
}

我的目标是最终将从 Texture2D 拉出的线放入纹理的另一点,从而产生扭曲。我目前得到的是无用的。我可以看到 Texture2D.ReadPixels() 调用完美无缺,因为我可以看到 UI 图像的红色像素在纹理中可见(通过调试行),而该列顶部为黑色(是正确的:相机呈现纯黑色背景,而不是天空盒)。然而,将该纹理 blit 回屏幕会导致无用的灰色。

如果我改为 Graphics.Blit(src, dest);,那么它会很好地渲染场景。

更新

添加proc.Apply()后屏幕不再是灰色的。然而,严重扭曲:

场景视图近似于相机应该渲染的内容,而相机视图显示的是实际渲染的内容(同样,没有天空盒,尽管打开它会使整个视图变成红色和紫色的骚乱)。

更新 2

我在另一台机器上试了一下 运行 一个不同(旧)版本的 Unity 并且...它可以工作。我应该意识到这可能首先是一个 Unity 错误(这是我在 5.6 中发现的 第三个 和第二个 这周 ).

我没试过,但你好像忘了 proc.Apply();proc.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);

之后
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
    Texture2D proc = new Texture2D(src.width, src.height);
    proc.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
    //Do your line swapping on proc here
    proc.Apply();
    Color[] line = proc.GetPixels(src.width / 2, 0, 1, src.height);
    Debug.Log(line[0] + " : "  + line[line.Length-1]);
    Graphics.Blit(proc, dest);
}

ReadPixels 将从屏幕中获取屏幕像素到保存的纹理数据中。 之后您需要 Apply() 更改,以便它们真正被保存。

Apply is a potentially expensive operation, so you'll want to change as many pixels as possible between Apply calls.

来源:Texture2D.Apply

我想你应该在 Apply() 和 Blit() 之前做你的行操作

理想情况下,您应该在着色器中执行这些操作。