Simple (In theory) shader question

Hi there,
I think this is a simple question with a simple answer, but I may be wrong.
I need to write a shader which has 2 basic purposes,

  1. it takes a texture and 2 colors as input properties
  2. it replaces color a with color b
  3. it only Paints the parts of the texture which were color a (now color b)
    it uses transparency as well

however I have no idea where to start on this
any pointers would be good
Thanks in advance.

Are you sure you don’t want to colorize a grayscale area of the texture instead? Rationale would be helpful.

I have a source texture which say has 2 colors one for skin tone the other for outline, the skin could be green in the texture the outline blue.
Id want to be able to specify replacement colors for each one.

Use two masking channels, then. If you’re using DXT5, use alpha, and any channel of a greyscale RGB for best compression. Trying to actually chroma key something will look worse and be slower. Unless you’re talking about a toon “outline”, in which case that’s not even a texture to be colorized, so no masking is necessary.

Hi jessy,
you couldn’t ‘whip up’ a quick example (been working through your tutorials on the Shader side of things but it’s taking me a while to figure it out.)

I’d need to be able to specify the 2 color properties and the source texture,
then presumably in the combine section of the subshader I need it to somehow only color the material if the pixel in the texture matches color 1, and then actually replace/paint it with color 2.

No, not without a mockup of the effect, with a diagram, or at least a really good description. Words aren’t enough to convey your needs, in my opinion.

heres a basic ‘mockup’ showing 1 pair of colors (color to be replace the replacement color)
I’d hopefully be able to figure out how to add other pairs of colors for additional replacements
I’d really appreciate your help thanks

No, I meant a mockup of what you’re actually going to do with it. I need to form a mental model of “what” and “why”, before I can move to “how”.

well this is pretty much representative, I have a graphic of an avatar, I need to be able to tint parts of the graphic (probably at the most 2 parts, outline skin tone) to different player defined colors. PM me your cell no. if you feel comfortable with it and I can discuss (probably a lot easier) I’ll pm you as well.

Do you have an important reason for using only a single material? This task would be much easier if you just use separate materials for the textured parts and the customizable parts. If that is not an option, you’ll have to write a custom Cg shader, which is not easy if you aren’t experienced with shader programming.

The reason I’m trying to do this using the 1 source texture is because for example we have eye assets 102 of them and we need to color 2 parts of them. I’d rather not have to double the number of images and overlay the different versions if a shader could do the job.

Got your pm jessy I’ll post an example asset here and try to explain it in more detail shortly.

So heres a small example of the eyes, in this situation we may want to color the outline, the pupil and the white of the eye different colors, in the source graphic they have different rgb values so in theory they can be addressed/identified and altered.
We are also needing to do a similar thing with T-shirts for the avatars although I don’t have any examples of them yet.

692942--24923--$Boys_Eyes__BEF001.png

If you don’t actually need to tint a texture, but rather, are just using flat colors, then it would be better to change the colors of different parts of your texture to be masks in a channel, instead of colors that are a mix of different channels (like brown). This does that; it should either work for you, or help show you that you haven’t provided all the necessary information.

Please forgive the lack of formatting; navigating this box on iOS is infuriating. Sorry about the GLSL if you’re on Windows, but I haven’t had time or a reason to learn Cg.

Shader “4-Colorize” {

Properties {
_MainTex (“Color Masks”, 2D) = “” {}
_RedColor (“Color for Red Texture Channel”, Color) = (1,0,0,1)
_GreenColor (“Color for Green Texture Channel”, Color) = (0,1,0,1)
_BlueColor (“Color for Blue Texture Channel”, Color) = (0,0,1,1)
_AlphaColor (“Color for Alpha Texture Channel”, Color) = (1,1,1,1)
}

SubShader {
Tags {“Queue”=“Transparent”}
ZWrite Off
Blend One OneMinusSrcAlpha
Pass {
GLSLPROGRAM
varying mediump vec2 uv;

#ifdef VERTEX
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
uv = gl_MultiTexCoord0.xy;
}
#endif

#ifdef FRAGMENT
uniform lowp sampler2D _MainTex;
uniform lowp vec4 _RedColor, _GreenColor, _BlueColor, _AlphaColor;
void main() {
lowp mat4 colors = mat4(_RedColor, _GreenColor, BlueColor, AlphaColor);
for (int i = 0; i < 4; ++i) colors
.rgb = colors.a;*

gl_FragColor = colors * texture2D(_MainTex, uv);
* }*
* #endif*
* ENDGLSL*
* }*
}
}