Here's the code of the shader again, stripped of comments:
float4 sampleColor = sampleLinear(oImage, outCoord());
float fGrey = 0.3 * sampleColor.r + 0.59 * sampleColor.g + 0.11 * sampleColor.b;
float4 vGrey = float4(fGrey, fGrey, fGrey, 1.0);
outputColor = (1.0 - fAmplitude) * sampleColor + fAmplitude * vGrey;
mat = new Array(0.2, 0.6, 0.2, 0, 0,
0.2, 0.6, 0.2, 0, 0,
0.2, 0.6, 0.2, 0, 0,
0, 0, 0, 1, 0);
cmFilter = new ColorMatrixFilter(mat);
dat.applyFilter(dat, dat.rect, new Point(), cmFilter);
There are a couple of notable differences though. First I use two different sets of numbers to map a colour to greyscale. The ColorMatrixFilter uses
(0.2, 0.6, 0.2)
which I came up with by trial and error while working on Bug Tunnel Defense, as I wrote some code to desaturate sprites once loaded. The shader uses
(0.3, 0.59, 0.11)
The other difference is the ColorMatrixFilter only does one thing: there's no option to only partially desaturate. This is easy to add though: and for consistency I'll do it using the numbers from the shader
var fR:Number = 0.30 * fAmplitude;
var fG:Number = 0.59 * fAmplitude;
var fB:Number = 0.11 * fAmplitude;
mat = new Array(fR + (1 - fAmplitude), fG, fB, 0, 0,
var fG:Number = 0.59 * fAmplitude;
var fB:Number = 0.11 * fAmplitude;
mat = new Array(fR + (1 - fAmplitude), fG, fB, 0, 0,
fR, fG + (1 - fAmplitude), fB, 0, 0,
fR, fG, fB + (1 - fAmplitude), 0, 0,
0, 0, 0, 1, 0);
cmFilter = new ColorMatrixFilter(mat);
dat.applyFilter(dat, dat.rect, new Point(), cmFilter);
This creates a matrix which has the same effect as the shader calculation. The shader code is simpler as it acts directly on the colour of each pixel.
As for which is faster I don't know as I've not tried comparing them. In my so far limited experience Flash shaders are very efficient, but I've not tried using a ColorMatrixFilter in the same way.
Perhaps the main advantage of shaders is it's easy to add other functionality. The matrix used above is about as much as can be crammed into a ColorMatrixFilter, but the shader barely scratches the surface of what's possible.
No comments:
Post a Comment