CrosshatchFragmentShader function

ShaderObject CrosshatchFragmentShader(
  1. int mode
)

Implementation

ShaderObject CrosshatchFragmentShader(int mode) {
  return ShaderObject("crosshatchPixelateF")
    ..AddUniformVars([uTexture])
    ..SetBody([
      "#define MODE ${mode}",
      """
float level1 = 1.0;
float level2 = 0.7;
float level3 = 0.5;
float level4 = 0.3;

float dist = 10.0;
float thickness = 0.5;

#if MODE == 0
// diagonal crosshach
vec2 slope = vec2(1.0, 1.0);
#elif MODE == 1
// regular crosshatch
vec2 slope = vec2(1.0, 0.0);
#else
ERROR
#endif

float PixelColor(vec2 pixel, float lum) {
    if (lum < level1) {
        // main anti diagonal
        if (mod(pixel.x * slope.x + pixel.y * slope.y, dist) <= thickness) return 0.0;
    }

    if (lum < level2) {
        // main diagonal
        if (mod(pixel.x * slope.y - pixel.y * slope.x, dist) <= thickness) return 0.0;
    }

    if (lum < level3) {
        // inbetween anti diagonals
        if (mod(pixel.x * slope.x + pixel.y * slope.y - dist * 0.5, dist) <= thickness) return 0.0;
    }

    if (lum < level4) {
        // inbetween main diagonals
        if (mod(pixel.x * slope.y - pixel.y * slope.x - dist * 0.5, dist) <= thickness) return 0.0;
    }

    return 1.0;
}

void main()  {
    vec2 pixel = gl_FragCoord.xy;  // this is really pixel + 0.5

    float lum = dot(vec3(0.2126, 0.7152, 0.0723),
                    texelFetch(${uTexture}, ivec2(pixel), 0).rgb);

    float color = PixelColor(pixel, lum);
    ${oFragColor} = vec4(vec3(color), 1.0);
}

"""
    ]);
}