Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Storing alpha values in temporary RenderTexture

Discussion in 'Image Effects' started by Benjamin_Overgaard, Jun 17, 2018.

  1. Benjamin_Overgaard

    Benjamin_Overgaard

    Joined:
    Jul 20, 2015
    Posts:
    17
    I've written an edge detection image effect, and I want to apply additional effects such as blur (only to the edges) before I blit them to the camera's destination RenderTexture, but I've run into some problems.

    In the shader, I set the alpha of all non-edge fragments to 0. This way, I can get the edges in their own texture and I can do what i want with them. If I blit the effect as in the following code, I get the original image + edge detection, as expected:

    Code (CSharp):
    1. void OnRenderImage(RenderTexture src, RenderTexture dst)
    2. {
    3.         Graphics.Blit(src, dst);
    4.         Graphics.Blit(src, dst, _edgeDetectionMat);
    5. }
    If I only do the second blit (using _edgeDetectionMat), I only get edge detection - everything else in the image is black, also as expected.

    However, I still want to separate the edge detection into its own RenderTexture. So I tried the following:

    Code (CSharp):
    1. void OnRenderImage(RenderTexture src, RenderTexture dst)
    2. {
    3.        RenderTexture tmp = RenderTexture.GetTemporary(src.width, src.height);
    4.  
    5.        Graphics.Blit(src, dst);
    6.        Graphics.Blit(src, tmp, _edgeDetectionMat);
    7.        Graphics.Blit(tmp, dst);
    8.  
    9.        tmp.Release();
    10. }
    Here, I expect to achieve the same result as in the first block of code, where I get the original image + edge detection. But the image only shows the edge detection and everything else is black. Maybe I lose all alpha data once I make the temporary RenderTexture?