Shader creation learning resources?

Okay, I think I want a shader that lets me recolor things on the fly, kind of like if I were using a color fill layer in Photoshop with the Overlay or Hard Light blend modes set with variable opacity. I know about changing the base color of a model, but that doesn’t quite have the effect I’m looking for. The problem with changing the base color is that the texture it’s multiplying uses it as the brightest color value when you’d ideally keep some white in the highlights and whatnot.

The textures are supposed to be designed so that the midtone is a middle gray (RGB 127 127 127), the highlights are brighter, and the shadows are darker. The selected color needs to be blended with the texture in such a manner that the middle gray regions adopt the color at its full value, the darker regions darken the selected color, and the highlights brighten the selected color (up to a pure white or a much lighter shade of the selected color).

To complicate things a bit more, the texture needs an alpha channel of sorts that only defines the areas that the selected color is allowed to change. (Picture a car as an example–the glass, headlights, grill, tires, and whatever shouldn’t be touched, so the texture alpha would mask out those parts and only allow the paint to be changed.)

The reason I’m thinking about that is because the application I’m working on is looking like it would need to use a lot of textures, and if I can remove the need for including duplicates in all possible color schemes, then I can cut down on the number of textures needed by simply recoloring a much smaller set of base gray textures as needed.

I read the ShaderLab documentation and came to the conclusion that because I didn’t really understand much of it, then obviously I’m not ready to get into writing shaders. :lol:

In the spirit of learning to walk before I run, can anyone recommend some good books or online resources that I can use to better understand how shaders work and how they ought to be written? Bonus points if those books/online resources explain things to me like I’m a slobbering idiot.

Thanks!

I don’t know what the algorithms are for those blending modes, but if what I’m about to say isn’t good enough for you, and you can find me the algorithms, I’ll try to do better.

Check this out and tell me if it’s the right idea for you:

If you take the Masked Tint - Vertex Colors shader, and change the line

combine primary * texture

to be

combine primary * texture DOUBLE

then you will get the texture as you painted it if the vertex color is 50% grey, and you can darken or lighten from there.

Essentially my entire (very limited!) knowledge of writing shaders come from the ShaderLab reference, but at the moment, that encompasses everything you can do with Unity iPhone, so I’m cool with that for the time being. 8)

All that stuff I put in the iPhone section is exceedingly simple, so you might want to pick them apart with the ShaderLab reference at hand.

Nice! I’ll take a look at that and give it a try.

Here’s the most concise and straightforward description of the Porter-Duff compositing operations I could find on Google:

http://www.vbaccelerator.com/home/VB/CODE/VBMEDIA/Image_Processing/Compositing/article.asp

Overlay, Hard Light, and the other Photoshop modes are down the page, under Extended Compositing Operations.

Just looking at the algorithms for these two modes in particular, I’m a bit unsure that some of them can even be implemented in a shader due to the math conditionals in some of them, as my weak understanding of the ShaderLab documentation leaves me with the impression that you can only do the “simple” inline math operations presented in the documentation.

Thanks for posting a link to your shader–I’ll give it and your suggested tweaks a whirl. I still need to find some books and stuff on shaders just so I don’t feel like a caveman trying to figure out a coffeepot, though. :lol: