// #include //ly.img.cesdk/shader/common/ubq_color_conversions uniform shader image; uniform float offset; uniform float darkness; // Obtaining the nearest value to the predefined HSL levels float nearestLevel(float col) { const int levCount = 6; // TODO: Include option for the user to choose the amount of levels const float interval = 1.0 / float(levCount); for (float i = 0.0; i <= 1.0; i += interval) { if (col >= i && col <= (i + interval)) { return (abs(col - i) < abs(col - (i + interval))) ? i : (i + interval); } } return 0; } half4 main(float2 coord) { float4 rgba = unpremul(image.eval(coord)); float3 origCol = rgba.rgb; float alpha = rgba.a; // Applying the edge detection filter to create an outline // Obtaining the local 3x3 matrix float4 A[9]; A[0] = unpremul(image.eval(coord + float2(-1, -1))); A[1] = unpremul(image.eval(coord + float2(0, -1))); A[2] = unpremul(image.eval(coord + float2(1, -1))); A[3] = unpremul(image.eval(coord + float2(-1, 0))); A[4] = rgba; A[5] = unpremul(image.eval(coord + float2(1, 0))); A[6] = unpremul(image.eval(coord + float2(-1, 1))); A[7] = unpremul(image.eval(coord + float2(0, 1))); A[8] = unpremul(image.eval(coord + float2(1, 1))); // Computing the x-direction gradient float4 sum_x = -A[0] + (A[2]) - (A[3] * 2.0) + (A[5] * 2.0) - (A[6]) + (A[8]); // Computing the y-direction gradient float4 sum_y = A[0] + (A[1] * 2.0) + A[2] - A[6] - (A[7] * 2.0) - A[8]; // Computing the sum float4 sum = sqrt((sum_x * sum_x) + (sum_y * sum_y)); // Combining the colors to obtain a black color for the outline float sumLength = length(sum); float outlineShade = 1.0 - sumLength; float4 outlineCol = float4(outlineShade, outlineShade, outlineShade, alpha); // Thresholding if (outlineShade > 0.5) outlineShade = 1.0; // Applying the cel-shading effect float3 hslColor = rgb2hsl(origCol); hslColor.x = nearestLevel(hslColor.x); hslColor.y = nearestLevel(hslColor.y); hslColor.z = nearestLevel(hslColor.z); float3 outColor = hsl2rgb(hslColor); float4 tc = float4(outColor.r, outColor.g, outColor.b, alpha); // If pixel is an edge, set pixel to the outline color if (outlineShade != 1.0) { tc = outlineCol; } tc.rgb *= tc.a; return tc; }