Author Topic: Soft clipping shader problem with alpha texture  (Read 3318 times)

rganeyev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Soft clipping shader problem with alpha texture
« on: February 27, 2014, 06:24:47 AM »
Hi all.

I want to make rounded-corner texture in UITexture as facebook avatars in draggable panel.


To do that I decided to change soft-clipping shader, so it will take alpha texture.
Here's the code:
  1. Shader "vtornik/iphone/avatar_shader (SoftClip)"
  2. {
  3.         Properties
  4.         {
  5.                 _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
  6.                 _AlphaTexture ("Base (A)", 2D) = "" {}
  7.         }
  8.  
  9.         SubShader
  10.         {
  11.                 LOD 200
  12.  
  13.                 Tags
  14.                 {
  15.                         "Queue" = "Transparent"
  16.                         "IgnoreProjector" = "True"
  17.                         "RenderType" = "Transparent"
  18.                 }
  19.                
  20.                 Pass
  21.                 {
  22.                         Cull Off
  23.                         Lighting Off
  24.                         ZWrite Off
  25.                         Offset -1, -1
  26.                         Fog { Mode Off }
  27.                         ColorMask RGB
  28.                         AlphaTest Greater .01
  29.                         Blend SrcAlpha OneMinusSrcAlpha
  30.  
  31.                         CGPROGRAM
  32.                         #pragma vertex vert
  33.                         #pragma fragment frag
  34.  
  35.                         #include "UnityCG.cginc"
  36.  
  37.                         sampler2D _MainTex;
  38.                         sampler2D _AlphaTexture;
  39.                         float4 _MainTex_ST;
  40.                         float2 _ClipSharpness = float2(20.0, 20.0);
  41.  
  42.                         struct appdata_t
  43.                         {
  44.                                 float4 vertex : POSITION;
  45.                                 half4 color : COLOR;
  46.                                 float2 texcoord : TEXCOORD0;
  47.                         };
  48.  
  49.                         struct v2f
  50.                         {
  51.                                 float4 vertex : POSITION;
  52.                                 half4 color : COLOR;
  53.                                 float2 texcoord : TEXCOORD0;
  54.                                 float2 worldPos : TEXCOORD1;
  55.                         };
  56.  
  57.                         v2f vert (appdata_t v)
  58.                         {
  59.                                 v2f o;
  60.                                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  61.                                 o.color = v.color;
  62.                                 o.texcoord = v.texcoord;
  63.                                 o.worldPos = TRANSFORM_TEX(v.vertex.xy, _MainTex);
  64.                                 return o;
  65.                         }
  66.  
  67.                         half4 frag (v2f IN) : COLOR
  68.                         {
  69.                                 // Softness factor
  70.                                 float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipSharpness;
  71.                        
  72.                                 // Sample the texture
  73.                                 fixed4 alpha = tex2D(_AlphaTexture, IN.texcoord);
  74.  
  75.                                 half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
  76.                                 col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);
  77.                                 col.a *= alpha.a;
  78.                                 return col;
  79.                         }
  80.                         ENDCG
  81.                 }
  82.         }
  83.  
  84.         Fallback Off
  85. }
  86.  

The problem is the first time they are being showed textures become all gray. After I drag draggable panel, hide and show texture, it becomes normal.
What's the problem with shader? How to solve this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Soft clipping shader problem with alpha texture
« Reply #1 on: February 27, 2014, 05:05:35 PM »
This is more of a Unity shader question than anything else.

This line doesn't make sense to me:
  1. o.worldPos = TRANSFORM_TEX(v.vertex.xy, _MainTex);
Why are you transforming a vertex by a texture matrix?