Created
September 4, 2011 09:43
-
-
Save jcayzac/1192591 to your computer and use it in GitHub Desktop.
Edge detection fragment shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
precision mediump float; | |
uniform vec2 resolution; | |
uniform sampler2D tex0; | |
const vec2 n = vec2(1.0/1024.0, 1.0/512.0); | |
const vec4 luminance_vector = vec4(0.3, 0.59, 0.11, 0.0); | |
void main(void) { | |
vec2 uv = vec2(1.0) - (gl_FragCoord.xy / resolution.xy); | |
vec2 n = 1.0/resolution.xy; | |
vec4 LD = texture2D(tex0, uv - n ); | |
vec4 LC = texture2D(tex0, uv + vec2(-n.x, 0.0)); | |
vec4 LU = texture2D(tex0, uv + vec2(-n.x, n.y)); | |
vec4 CD = texture2D(tex0, uv + vec2( 0.0, -n.y)); | |
vec4 CC = texture2D(tex0, uv ); | |
vec4 CU = texture2D(tex0, uv + vec2( 0.0, n.y)); | |
vec4 RD = texture2D(tex0, uv + vec2( n.x, -n.y)); | |
vec4 RC = texture2D(tex0, uv + vec2( n.x, 0.0)); | |
vec4 RU = texture2D(tex0, uv + n ); | |
vec2 g = vec2( | |
dot(luminance_vector, RD - LD) + | |
2.0*dot(luminance_vector, RC - LC) + | |
dot(luminance_vector, RU - LU), | |
dot(luminance_vector, LD - LU) + | |
2.0*dot(luminance_vector, CD - CU) + | |
dot(luminance_vector, RD - RU) | |
); | |
float baseLum = dot(luminance_vector, CC); | |
float len = clamp(length(g)/baseLum, 0.1, 10.0); | |
gl_FragColor = vec4(CC.rgb*len, 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment