Author Topic: New Shader for NGUI - Swap Colors not working properly  (Read 3534 times)

saulotti

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
New Shader for NGUI - Swap Colors not working properly
« on: September 13, 2013, 03:07:53 PM »
Hi!

I've created a new shader, and it works fine in most of the cases. Including Alpha clipping and in other conditions. But, for some reason it is not rendering correctly when I have multiple atlas, depths and panels. The sprite disappear. But if I make a copy of this object and place it outside of it's parent panel, both become visible.

The ideia behind this shader is simple: Find the Pure Red and Pure Pink and replace by the color passed as a parameter to the shader (the same parameter used on NGUI).

Did I use something that NGUI doesn't support on the panel/atlas handling? Thanks!

  1. Shader "Unlit/Transparent Colored Swap"
  2. {
  3.         Properties
  4.         {
  5.                 _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
  6.         }
  7.  
  8.         SubShader
  9.         {
  10.                 LOD 200
  11.  
  12.                 Tags
  13.                 {
  14.                         "Queue" = "Transparent"
  15.                         "IgnoreProjector" = "True"
  16.                         "RenderType" = "Transparent"
  17.                 }
  18.  
  19.                 Pass
  20.                 {
  21.                         Cull Off
  22.                         Lighting Off
  23.                         ZWrite Off
  24.                         Offset -1, -1
  25.                         Fog { Mode Off }
  26.                         ColorMask RGB
  27.                         Blend SrcAlpha OneMinusSrcAlpha
  28.                
  29.                         CGPROGRAM
  30.                         #pragma vertex vert
  31.                         #pragma fragment frag
  32.                         #include "UnityCG.cginc"
  33.  
  34.                         sampler2D _MainTex;
  35.                         float4 _MainTex_ST;
  36.  
  37.                         struct appdata_t
  38.                         {
  39.                                 float4 vertex : POSITION;
  40.                                 half4 color : COLOR;
  41.                                 float2 texcoord : TEXCOORD0;
  42.                         };
  43.  
  44.                         struct v2f
  45.                         {
  46.                                 float4 vertex : POSITION;
  47.                                 half4 color : COLOR;
  48.                                 float2 texcoord : TEXCOORD0;
  49.                                 float2 worldPos : TEXCOORD1;
  50.                         };
  51.  
  52.                         v2f vert (appdata_t v)
  53.                         {
  54.                                 v2f o;
  55.                                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  56.                                 o.color = v.color;
  57.                                 o.texcoord = v.texcoord;
  58.                                 o.worldPos = TRANSFORM_TEX(v.vertex.xy, _MainTex);
  59.                                 return o;
  60.                         }
  61.  
  62.                         half4 frag (v2f IN) : COLOR
  63.                         {
  64.                                 // Sample the texture
  65.                                 half4 col = tex2D(_MainTex, IN.texcoord);
  66.  
  67.                                 float2 factor = abs(IN.worldPos);
  68.                                 float val = 1.0 - max(factor.x, factor.y);
  69.  
  70.                                 // Option 1: 'if' statement
  71.                                 if (val < 0.0) col.a = 0.0;
  72.  
  73.                                 // Option 2: no 'if' statement -- may be faster on some devices
  74.                                 //col.a *= ceil(clamp(val, 0.0, 1.0));
  75.  
  76.                                 if (col.r == 1 && col.g == 0 && col.b == 1 && col.a > 0)
  77.                 {
  78.                         return IN.color;
  79.                 }
  80.                 else if (col.r == 1 && col.g == 0 && col.b == 0 && col.a > 0) {
  81.                         fixed4 c = IN.color*0.65;
  82.                         c.a = IN.color.a;
  83.                         return c;
  84.                 }
  85.                 else {
  86.                         return (1,1,1,IN.color.a) * col;
  87.                 }
  88.                         }
  89.                         ENDCG
  90.                 }
  91.         }
  92.        
  93.         SubShader
  94.         {
  95.                 LOD 100
  96.  
  97.                 Tags
  98.                 {
  99.                         "Queue" = "Transparent"
  100.                         "IgnoreProjector" = "True"
  101.                         "RenderType" = "Transparent"
  102.                 }
  103.                
  104.                 Pass
  105.                 {
  106.                         Cull Off
  107.                         Lighting Off
  108.                         ZWrite Off
  109.                         Fog { Mode Off }
  110.                         ColorMask RGB
  111.                         AlphaTest Greater .01
  112.                         Blend SrcAlpha OneMinusSrcAlpha
  113.                         ColorMaterial AmbientAndDiffuse
  114.                        
  115.                         SetTexture [_MainTex]
  116.                         {
  117.                                 Combine Texture * Primary
  118.                         }
  119.                 }
  120.         }
  121.        
  122. }
  123.  

saulotti

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: New Shader for NGUI - Swap Colors not working properly
« Reply #1 on: September 13, 2013, 03:42:12 PM »
I've just realised that the same effect happens with the shader: Transparent Colored (Alpha Clip).
And this was the shader I've copied from to start my own shader.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: New Shader for NGUI - Swap Colors not working properly
« Reply #2 on: September 13, 2013, 10:29:28 PM »
"The sprite disappear" -- the old depth vs Z issue.

Solution: upgrade to NGUI 2.7.0 and check the Depth Sorting option on your panels. Or if you can -- to NGUI 3.0.0, as it addresses this particular issue fully.