后处理效果有问题 [英] Having Trouble With Post Processing Effects

查看:130
本文介绍了后处理效果有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write a post processing effect that alters what is eventually drawn to the screen. However, that requires getting what's currently being drawn, modifying it, and then drawing it back.

前两个步骤似乎工作正常.第三步..把所有的颜色都变成灰色.

The first two steps seem to work just fine. The third step....renders all gray.

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图像的红色像素在纹理中(通过调试行)可见,而在该列顶部的黑色像素是正确的(这是正确的:摄像机渲染了)稳定的黑色背景,而不是天空盒).但是,将纹理拖回屏幕会导致无用的灰色内容.

My goal is to eventually take the line pulled from the Texture2D and place it back into the texture at another point, creating a distortion. What I'm currently getting though is useless. I can see that the Texture2D.ReadPixels() call performs flawlessly as I can see that the red pixels of that UI image are visible in the texture (via the debug line) and black for the top of that column (which is correct: the camera renders a solid black background, not the skybox). However, blitting that texture back to the screen results in useless gray nothing.

如果我改用Graphics.Blit(src, dest);,那么它将使场景很好.

If I do Graphics.Blit(src, dest); instead, then it renders the scene just fine.

更新

添加proc.Apply()后,屏幕不再是灰色.但是,它严重失真:

After adding proc.Apply() the screen is no longer gray. It is however, heavily distorted:

场景视图近似于相机应渲染的内容,而相机视图则显示实际渲染的内容(再次,没有天空框,尽管将其打开会使整个视图变成红色和紫色的暴动).

Scene view approximates what the camera should render, while the camera view shows what's actually rendered (again, no skybox, although turning it on makes the entire view a riot of reds and purples).

更新2

我在运行不同(较旧)版本的Unity annd的另一台计算机上工作了……这很有效.我应该已经意识到,这首先可能是Unity错误(这是我在5.6中找到的第三,而本周的第二次 ).

I gave things a whack on a different machine running a different (older) version of Unity annd...it works. I should have realized that it was likely a Unity bug first and foremost (this is the third one I've found in 5.6 and the second this week).

推荐答案

我没有尝试过,但是好像您在proc.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);

I have not tried it, but it looks like you forgot to proc.Apply(); after 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()所做的更改,以便将其实际保存.

ReadPixels will take the screen pixels from screen into the saved texture data. You need to Apply() the changes after that so they are actually saved.

应用是一个潜在的昂贵操作,因此您需要在两次应用调用之间更改尽可能多的像素.

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

来源: Texture2D.Apply

我想您应该只在Apply()和Blit()之前进行行操作

I guess you should just do your line operations before Apply() and Blit()

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

Idealy, you should do these operations in a shader.

这篇关于后处理效果有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆