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 can I scale an image without blur in Unity Shader?

Discussion in 'Shaders' started by junqinglang, Jun 17, 2020.

  1. junqinglang

    junqinglang

    Joined:
    May 5, 2020
    Posts:
    2
    I am a newbie of Unity shader. I'm trying to scale my image in the vertical direction. Here is the shader code I use:
    Code (Shader):
    1. Shader "Custom/PostMainCameraProcessShader"
    2. {
    3. Properties{
    4.     _MainTex("Screen Texture", 2D) = "white" {}
    5.     _Param("Param", float) = 1
    6. }
    7. SubShader{
    8.     Pass {
    9.  
    10.         CGPROGRAM
    11.         #pragma vertex vert
    12.         #pragma fragment frag
    13.         #pragma fragmentoption ARB_precision_hint_fastest
    14.         #include "UnityCG.cginc"
    15.  
    16.         struct v2f {
    17.             float4 vertex : SV_POSITION;
    18.             float2 uv     : TEXCOORD0;
    19.         };
    20.  
    21.         sampler2D _MainTex;
    22.         float4 _MainTex_TexelSize;
    23.         float _Param;
    24.  
    25.         v2f vert(appdata_img v) {
    26.             v2f o;
    27.             o.vertex = UnityObjectToClipPos(v.vertex);
    28.             o.uv = v.texcoord;
    29.  
    30.             return o;
    31.         }
    32.  
    33.         fixed4 frag(v2f i) : COLOR
    34.         {
    35.             float2 uv = i.uv;
    36.             uv.y *= _Param;
    37.             fixed4 tex_screen = tex2D(_MainTex, uv);
    38.             return tex_screen;
    39.         }
    40.  
    41.         ENDCG
    42.     }
    43. }
    44. FallBack "Diffuse"
    45. }
    So Can anybody give me some suggestions that how can I remove the top blur and make the background pure black?

    Many thanks!
     
    Last edited: Jun 18, 2020
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    You can check whether the UVs have gone out of bounds and mask the final output;

    Code (CSharp):
    1. return tex_screen * (uv.y <= 1.0);
    This just checks against the top of the screen - if you needed the other sides then you can do the same by checking both UV x and y are between 0 and 1.
     
    junqinglang likes this.
  3. junqinglang

    junqinglang

    Joined:
    May 5, 2020
    Posts:
    2
    Thank you!!!!
    You solved my question trapped me a couple of weeks!!!
    Thank you!