Need help with this desaturation shader by Jessy

Hi,

I have been trying to understand this desaturation shader by Jessy, but I don’t understand the fallback pass (The one using fixed pipeline).

This is the shader:
http://forum.unity3d.com/threads/82105-Desaturation-grayscale-shader-for-iOS

This is the pass I do not understand:

SubShader {Pass {

    Color (.389, .1465, .4645., .5)

    SetTexture[_MainTex] {Combine one - texture * primary alpha}

    SetTexture[_] {Combine previous Dot3 primary}

}}

First of all, I assume this is based on one of the well known luminance formulas. It seems the primary color set here is the invert of the luminance values ussually used. But why does he use the invert color to compute later the dot3 over the primary? Have been looking for information on google with no luck. Could anybody help?

Thanks in advance.

Yes indeed. The one from GPU Gems –float grayscale = dot(float3(0.222, 0.707, 0.071), inColor)– is generally what I use as a starting point. You can make the color an assignable property if you want; I just hardcoded it for simplicity.

Well, with the Dot3 combiner, your operands will be multiplied by two,and then have one subtracted, before being dotted. So you’ve got two choices: have your values in the .5 to 1 range, or the .5 to 0 range (not 0 to .5; .5 becomes the origin for your vectors, and 0 becomes -1). If we could use the ternary combiners, you could use the .5 to 1 range, like so:

Color (.611, .8535, .5355., .5)   // (.222, .707, .071) * .5 + .5
SetTexture[_MainTex] {Combine texture * primary alpha + primary alpha}
SetTexture[_] {Combine previous Dot3 primary}

However, because those combiners don’t work on some hardware, particularly the PowerVR MBX Lite, which was still somewhat relevant when that other thread came about, we have to take the other approach, in order to avoid either having to use more texture stages, or overcomplicate the shader elsewhere. You still compress the range of the values; you just have the vectors point into the negative direction from the origin:

Color (.389, .1465, .4645., .5)  // (1 - (.222, .707, .071)) * .5
SetTexture[_MainTex] {Combine one - texture * primary alpha}
SetTexture[_] {Combine previous Dot3 primary}

I made it up; I have no idea if anyone ever used the fixed function pipeline for this effect.