Alright, I’m not very familiar with Shaders but I’ll try to explain in details what I’m trying to achieve, why, and what I achieved so far.
The background
I use NGUI for my Game GUI and one great feature is that it keeps all images from your GUI on the same Atlas. This is good for saving both space and drawcalls I guess.
Okay, when using NGUI I use this Unlit Transparent Colored Shader, which let me pick a color and multiply with my texture, this is great when I have a white sprite and want to change it’s color.
Here is the Shader Code (I hope there’s no problem sharing it here as it’s the base for my other shader):
Shader "Unlit/Transparent Colored"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
}
SubShader
{
LOD 100
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Pass
{
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Offset -1, -1
ColorMask RGB
AlphaTest Greater .01
Blend SrcAlpha OneMinusSrcAlpha
ColorMaterial AmbientAndDiffuse
SetTexture [_MainTex]
{
Combine Texture * Primary
}
}
}
}
The goal
What I want to achieve is to have a monochromatic GUI that can change color based on the color I pick. In other words, I want to achieve a effect similar to the Overlay Blend Mode on Photoshop.
Quoted from: The Overlay Blend Mode In Photoshop
Using Images to explain that would be that the gray button bellow would change its color as I picked blue(0, 0, 255) or red(192,43,43).
What’s achieved so far?
By changing the shader above (and I had no Idea what I was doing) I could achieve a similar effect, that works quite well. Just change the previous shader Pass so it looks like this:
Pass
{
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Offset -1, -1
ColorMask RGB
AlphaTest Greater .01
Blend SrcAlpha OneMinusSrcAlpha
ColorMaterial AmbientAndDiffuse
SetTexture [_MainTex]
{
Combine Texture +- Primary, Texture * Primary
}
SetTexture [_MainTex]
{
Combine Texture +- previous, Texture * Primary
}
}
The problem
This shader works pretty well when I have grayscale buttons, the 50% gray looks about the color I pick and the “shadows and lights” seem to adjust well to the selected color.
But when I put a colored sprite with this shader… well… I excpected that by selecting the 50% gray as my pick color everything should remain unchanged compared to the original sprite, but that’s not what happen. Check the results bellow:
Original / 50% gray picked
Because of that weird effect I had separate all my grayscale and colored sprites into 2 atlases, which causes me a lot of trouble with rendring order.
The Request
Can anyone help me achieve what I want with the right code so I could use the same atlas for colored and grayscale sprites? I mean, because obviously my Pass is not doing the Overlay Blend Mode right, it’s just something similar.
The Thanks
Thanks in advance, specially if you had patience to read this big post. I hope everything is well explained.