void makeKernel(inout vec4 n[9], sampler2D tex, vec2 coord) {
	vec2 size = vec2(textureSize(tex, 0));
	float w = 1.0 / size.x;
	float h = 1.0 / size.y;

	n[0] = texture(tex, coord + vec2( -w, -h));
	n[1] = texture(tex, coord + vec2(0.0, -h));
	n[2] = texture(tex, coord + vec2(  w, -h));
	n[3] = texture(tex, coord + vec2( -w, 0.0));
	n[4] = texture(tex, coord);
	n[5] = texture(tex, coord + vec2(  w, 0.0));
	n[6] = texture(tex, coord + vec2( -w, h));
	n[7] = texture(tex, coord + vec2(0.0, h));
	n[8] = texture(tex, coord + vec2(  w, h));
}


float intensity(in vec4 color){
	return sqrt((color.x*color.x)+(color.y*color.y)+(color.z*color.z));
}

in vec2 fragTexCoord;
in vec3 fragNormal;
in vec4 fragColor;

uniform sampler2D texture0;

out vec4 finalColor;

void main()
{
	vec4 n[9];
	makeKernel(n, texture0, fragTexCoord);

	vec4 sobel_edge_h = n[2] + (2.0*n[5]) + n[8] - (n[0] + (2.0*n[3]) + n[6]);
	vec4 sobel_edge_v = n[0] + (2.0*n[1]) + n[2] - (n[6] + (2.0*n[7]) + n[8]);
	vec4 sobel = sqrt((sobel_edge_h * sobel_edge_h) + (sobel_edge_v * sobel_edge_v));

	float value = intensity(sobel);
	value = smoothstep(0.95, 1.0, value);
	finalColor = vec4(value, value, value, 1.0);

#if (__VERSION__ == 100)
    gl_FragColor = finalColor;
#endif
}
