Search Unity

[SOLVED] UV Displacement for Sprite with Transparent pixels Possible?

Discussion in 'Shaders' started by darreney, Jul 30, 2019.

  1. darreney

    darreney

    Joined:
    Oct 9, 2018
    Posts:
    34
    Hi, i have shader for offsetting the sprite uv based on a secondary texture map.
    Here is the code:
    Code (CSharp):
    1. Shader "Custom/RippleShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _DispTex("Displacement", 2D) = "grey" {}
    7.     }
    8.     Subshader
    9.     {
    10.         Tags
    11.         {
    12.             "Queue" = "Transparent"
    13.             "IgnoreProjector" = "True"
    14.             "RenderType" = "Transparent"
    15.             "PreviewType" = "Plane"
    16.             "CanUseSpriteAtlas" = "True"
    17.         }
    18.  
    19.         Cull off
    20.         Lighting Off
    21.         ZWrite Off
    22.         Blend One OneMinusSrcAlpha
    23.  
    24.         Pass
    25.         {
    26.     CGPROGRAM
    27.             #pragma vertex vert
    28.             #pragma fragment frag
    29.             #include "UnityCG.cginc"
    30.  
    31.             sampler2D _MainTex;
    32.             sampler2D _DispTex;
    33.  
    34.             float4 _MainTex_ST;
    35.             float4 _DispTex_ST;
    36.  
    37.             struct v2f
    38.             {
    39.                 float4 vertex : SV_POSITION;
    40.                 float2 uv : TEXCOORD0;
    41.             };
    42.  
    43.             v2f vert(float4 vertex:POSITION, float2 uv : TEXCOORD0)
    44.             {
    45.                 v2f i;
    46.                 i.vertex = UnityObjectToClipPos(vertex);
    47.                 i.uv = TRANSFORM_TEX(uv, _MainTex);
    48.                 return i;
    49.             }
    50.  
    51.             float4 frag(v2f IN) : COLOR
    52.             {
    53.                 float2 uv = IN.uv.xy;
    54.                 float2 uvDisp = float2(uv.x, 0);
    55.                 half4 disp_c = tex2D(_DispTex, uvDisp);
    56.                 uv.y += disp_c.r/5.0;
    57.                 float4 c = tex2D(_MainTex, uv);
    58.                 return c;
    59.             }
    60.     ENDCG
    61.         }
    62.     }
    63.     FallBack "Diffuse"
    64. }
    65.  

    However, the problem is that this only works when the main texture is opaque with a black background.
    When I use an image with transparent background, the displacement of the uv does not affect the transparent pixels..
    How can I fix the shader such that the transparent pixels work like the opaque ones?

    Attached image of the different outcomes
     

    Attached Files:

  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    At first glance, I would imagine it's to do with the alpha blending; try changing from premultiplied alpha to traditional alpha blending to see if it makes a difference;

    Code (CSharp):
    1. //This
    2. Blend One OneMinusSrcAlpha
    3. //to this
    4. Blend SrcAlpha OneMinusSrcAlpha
    I would also be sure that your sprite object covers the full extent of the distortion too.
     
  3. darreney

    darreney

    Joined:
    Oct 9, 2018
    Posts:
    34
    Thanks for the reply @Namey5 ! however the result is also the same..

    Yes, if u see the attached, the both sprites have extra space above and below the green line. But the transparent one is giving problems.
    I am wondering does transparent pixels get clipped automatically? Is there a way to control that?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Check your sprite's mesh: try looking at the scene with wireframe enabled. I'm guessing when you add transparency to your sprite source image, Unity is likely cropping the sprite mesh down to just the area the non-invisible parts of the sprite. If you select your texture in the Project view and change the "Mesh Type" from "Tight" to "Full Rect".
     
  5. darreney

    darreney

    Joined:
    Oct 9, 2018
    Posts:
    34
    Wow Thank You @bgolus for solving my issue once again! it's a Unity setting. That's exactly the fix! You are just so awsome!!