Leadwerks Game Engine

Leadwerks Game Engine

Cartoon Shader
Josh 18 Jun, 2017 @ 2:47pm
My Mod
I was playing around with this and I just adjusted the normal / edge code. Looks nice:
#version 400 uniform sampler2D texture1; //diffuse uniform sampler2DMS texture0; //depth uniform sampler2DMS texture2; //normal uniform vec2 camerarange; uniform bool isbackbuffer; uniform vec2 buffersize; out vec4 fragData0; #define width 2 #define cartoon_color 1.0 float DepthToZPosition(in float depth) { return camerarange.x / (camerarange.y - depth * (camerarange.y - camerarange.x)) * camerarange.y; } vec4 highlight(vec4 input_color) { vec4 output_color=vec4(0); float gray=input_color.r*.2+input_color.g*.7+input_color.b*.1; output_color.r=cartoon_color*(input_color.r-gray)+input_color.r; output_color.g=cartoon_color*(input_color.g-gray)+input_color.g; output_color.b=cartoon_color*(input_color.b-gray)+input_color.b; output_color.a=input_color.a; return output_color; } void main(void) { vec2 texcoord = gl_FragCoord.xy/buffersize + 0.5/(buffersize*0.5); if (isbackbuffer) texcoord.y = 1.0 - texcoord.y; vec4 c = texture(texture1, texcoord); //Line Detection bool edge = false; float depth_o = texelFetch(texture0,ivec2(texcoord*buffersize),0).x; float depth = DepthToZPosition(depth_o); float depth_temp = 0; float depth_o_temp = 0; vec3 normal = normalize(texelFetch(texture2,ivec2(texcoord*buffersize),0).xyz*2.0-1.0); vec3 normal_temp = vec3(0); vec3 color_temp = vec3(0); float depth_max = 0; float depth_min = 0; float depth_diff = 0; vec4 acc_c = vec4(0); float count_c = 0; float normal_diff=abs(dot(normal_temp,normal)); // Check adjacent pixels for (int x=-1; x<2; x+=2) for (int y=-1; y<2; y+=2) { depth_o_temp = texelFetch(texture0,ivec2(texcoord*buffersize)+ivec2(x*width,y*width),0).x; depth_temp = DepthToZPosition(depth_o_temp); normal_temp = normalize(texelFetch(texture2,ivec2(texcoord*buffersize)+ivec2(x*width,y*width),0).xyz*2.0-1.0); //normal_temp = normal_temp/length(normal_temp); depth_min = min(depth, depth_temp); depth_max = max(depth, depth_temp); depth_diff = abs(depth_temp-depth); normal_diff=abs(dot(normal_temp,normal)); if (normal_diff<0.75 || (normal_diff < 1 || abs(depth-depth_temp)/depth_min>.01) && (.1<(depth_diff)/(depth_min)) && (depth_min<50)) { edge = true; } else { acc_c += texture(texture1, texcoord+vec2(x*width,y*width)/buffersize); count_c += 1; } } c=highlight(c); fragData0 = c; if (edge) { fragData0 = vec4(0,0,0,c.a); } }