Author Topic: Problem with Greyscale Shader on UI2DSprites  (Read 2177 times)

tehr0b

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Problem with Greyscale Shader on UI2DSprites
« on: July 22, 2014, 04:32:42 PM »
I recently needed a greyscale shader, so I took the NGUI Premultiplied Colored shader, and changed the fragment shader to be this

  1. half4 frag (v2f IN) : COLOR  
  2. {  
  3.     half4 col;
  4.     half4 colt;
  5.     col = tex2D(_MainTex, IN.texcoord);
  6.     colt = col * IN.color;
  7.     float grey = dot(col.rgb, float3(0.299, 0.587, 0.114));
  8.     col.rgb = (IN.color.r==0) * float3(grey, grey, grey) + (IN.color.r!=0) * colt.rgb;
  9.     col.a = colt.a;
  10.     return col;
  11. }


Everything works great on UISprites, however it's been causing weird alpha issues with UI2DSprites. I did a quick side-by-side comparison, with the top images being UISprites in an atlas and the bottom sprites being UI2DSprites.

Even though I know it's not efficient for draw-calls' sake, my game uses a good amount of UI2DSprites due to loading a lot of character assets from assetbundles, and the individual bundles are small enough that it doesn't make much sense to atlas them.

Is there something I'm doing wrong with the shader? Or is there something else I'm doing wrong?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem with Greyscale Shader on UI2DSprites
« Reply #1 on: July 22, 2014, 11:56:22 PM »
PMA shaders are quite different from Transparent shaders. You can't go from one to the other without modifying the source textures. When NGUI creates a PMA atlas, it actually modifies textures when adding them to the atlas. The main differences are in PMA shader using this blending:
  1. Blend One OneMinusSrcAlpha
...and Alpha Test being off. And of course the source texture cannot be imported with "Alpha is Transparency" turned on. PMA textures can't have transparency. Only alpha.