axmol/tests/cpp-tests/Source/shaders/example_EdgeDetection.fsh

47 lines
1.2 KiB
GLSL

#version 310 es
precision highp float;
precision highp int;
layout(location = 0) in vec4 v_fragmentColor;
layout(location = 1) in vec2 v_texCoord;
layout(binding = 0) uniform sampler2D u_tex0;
layout(std140, binding = 0) uniform fs_ub {
vec2 resolution;
};
float lookup(vec2 p, float dx, float dy)
{
vec2 uv = p.xy + vec2(dx , dy ) / resolution.xy;
vec4 c = texture(u_tex0, uv.xy);
return 0.2126*c.r + 0.7152*c.g + 0.0722*c.b;
}
layout(location = 0) out vec4 FragColor;
void main(void)
{
vec2 p = v_texCoord.xy;
// simple sobel edge detection
float gx = 0.0;
gx += -1.0 * lookup(p, -1.0, -1.0);
gx += -2.0 * lookup(p, -1.0, 0.0);
gx += -1.0 * lookup(p, -1.0, 1.0);
gx += 1.0 * lookup(p, 1.0, -1.0);
gx += 2.0 * lookup(p, 1.0, 0.0);
gx += 1.0 * lookup(p, 1.0, 1.0);
float gy = 0.0;
gy += -1.0 * lookup(p, -1.0, -1.0);
gy += -2.0 * lookup(p, 0.0, -1.0);
gy += -1.0 * lookup(p, 1.0, -1.0);
gy += 1.0 * lookup(p, -1.0, 1.0);
gy += 2.0 * lookup(p, 0.0, 1.0);
gy += 1.0 * lookup(p, 1.0, 1.0);
float g = gx*gx + gy*gy;
FragColor.xyz = vec3(1.-g);
FragColor.w = 1.;
}