Having Trouble With Post Processing Effects

[亡魂溺海] 提交于 2020-01-06 18:11:45

问题


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);
    }
}

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.

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

Update

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).

Update 2

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).


回答1:


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 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.

Source: Texture2D.Apply

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

Idealy, you should do these operations in a shader.



来源:https://stackoverflow.com/questions/44206474/having-trouble-with-post-processing-effects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!