Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to downscale and upscale a grab pass texture in a shader

Discussion in 'Shaders' started by Martin_Luna, Dec 16, 2022.

  1. Martin_Luna

    Martin_Luna

    Joined:
    Sep 28, 2021
    Posts:
    2
    hey all. I'm working on a custom bloom shader.
    It requires down sample and up sample steps.

    I know this can be done in C#, eg:

    Code (CSharp):
    1. int width = source.width / 2;
    2. int height = source.height / 2;
    3.  
    4. RenderTexture currentDestination = RenderTexture.GetTemporary(
    5.             width, height, 0
    6.         );
    7.        
    8.  
    9. Graphics.Blit(source, currentDestination, bloom, BoxDownPrefilterPass);
    But i need this to be done in the shader.
    Something like this:

    Code (CSharp):
    1. GrabPass
    2. {
    3.      "_BackgroundTexture"
    4. }
    5.  
    6. //this next line is just an example of what i'd like to do. is this possible?
    7. _BackgroundTexture.setSize = halfSize;
    8. col = tex2D(_BackgroundTexture, i.grabPos);
    Thanks for any help all :)
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Not possible.
     
  3. Martin_Luna

    Martin_Luna

    Joined:
    Sep 28, 2021
    Posts:
    2
    hey @bgolus, thanks for the reply.

    Ending up doing this in the end:

    Code (CSharp):
    1.             half4 frag(v2f i) : SV_Target
    2.             {
    3.                 half4 col = 0;
    4.  
    5.                 float2 newUVPos;
    6.  
    7.                 //Sample the grab pass texture at the same point for multiple
    8.                 //fragments. Eg, if fragment is 0.0, 0.1, and 0.2, then sample
    9.                 //from the texture at uv 0.0. if 0.3, 0.4, 0.5, then sample
    10.                 //at 0.3 etc. this gives the effect of lower resolution.
    11.                
    12.                 //uv coords start out 0 to 1. Multiply by 10X or more so
    13.                 //we get an integer. eg, uv.x = 0.51. X by 10 = 5.1;
    14.                 newUVPos.x = i.grabPos.x * _SmootherFactor;
    15.                 newUVPos.y = i.grabPos.y * _SmootherFactor;
    16.  
    17.                 //then round to the nearest int. Eg 5.1 will be 5.0
    18.                 newUVPos.x = round(newUVPos.x);
    19.                 newUVPos.y  = round(newUVPos.y );
    20.  
    21.                 //then convert back into a 0 to 1 uv coords range.
    22.                 //Eg 5.0 will become 0.5
    23.                 newUVPos.x /= _SmootherFactor;
    24.                 newUVPos.y  /= _SmootherFactor;
    25.  
    26.                 //sample from the grab pass texture
    27.                 col = tex2D(_BackgroundTexture, newUVPos);
    28.                
    29.                 return col;
    30.                
    31.             }
    upload_2022-12-19_15-37-54.png