- Code: Select all
`//checkerboard effect`

precision lowp float;

varying vec2 vTex;

uniform sampler2D samplerFront;

uniform float pixelWidth;

uniform float pixelHeight;

//params

uniform float squareSize; //size of checkerboard squares in pixels

uniform float strength; //amount of lightening and darkening to apply

void main() {

vec2 screenSize = vec2(1.0/pixelWidth, 1.0/pixelHeight);

vec2 grid = floor((vTex * screenSize) / squareSize); //checkerboard grid coordinate, 0,0 is top left square

float checkerboard = mod(grid.x + grid.y, 2.0)-0.5; //-0.5 for dark +0.5 for light

vec4 front = texture2D(samplerFront, vTex);

front.rgb += checkerboard * strength;

gl_FragColor = front * front.a;

}

Hi all, I made this checkerboard effect for a selection highlight for an in-game level editor. It started as a modification of @chrisbrobs colorcheck effect so thanks to him effect-colorcheck_t163486

The differences are that it's no longer proportional to screen size (parameter is 'squaresize' rather than 'squarecount'), it now adds and subtracts the source pixels with a strength parameter rather than overriding them completely, and I cut out the colorization because I did not need it.

This was my first effect and I have some questions:

Is there an easier way of getting absolute screen-space pixel coordinates?

Having to calculate the screens size from 1/pixelWidth and 1/pixelHeight, and then multiplying that to the vTex coordinates seems wrong.

If void main is called for every pixel, is it unnecessary to put certain calculations in there?

For example, the screen size calculation above should only need to be done once. Other shaders seem to do things like this as well, is this because the performance hit is negligible?

Why isn't alpha reserved when I only adjust the rgb?

On the last line I multiply front by front.a to ensure the transparency of the sprite is reserved, but I am not sure why this is necessary:

front is a vec4, sampling the RGBA of the pixel

front.rgb is then adjusted to apply the checkerboarding

but front.rgb is only the first 3 values in the vec4, shouldn't the front.a be left intact?

thanks