Edge Detection
What Edge Detection Does:
An edge detection shader finds the edges of a particular texture based on the difference in contrast of its pixels.
Creation Process:
I first of all went about creatig my “comparison slider”. Basically, it is a series of variables that represent a three-by-three square of pixels. Basically it consists of a center point surrounded by eight points. One for each cardinal direction, as a metaphor. North, South, East, West, Northeast, Southeast, Northwest, and Southwest. Three sets of variables were declared. One for each greyscale set of color values: Red, Green, Blue. Once all of my variables were made, I was able to use if, else statements to compare the center points to those surrounding them. Using a threshold value which was prepared to be exported to Slim as a slider, I compared it to the difference between the color values of my center and the surrounding pixels. If it was greater than or equal to my threshold, it was colored the edge color. If not, it was colored the background color.
Shader Code:
surface edge_detection( float Kd = 1, size = 0.1, difference = 0.01; string texname = ""; color edgecolor = color(1,1,1), back = color(0,0,0);) { color surfcolor = back, diffusecolor; if(texname!= "") { //Red Slider float R = float texture(texname[0], s, t); float R_NW = float texture(texname[0], s-size, t-size); float R_N = float texture(texname[0], s, t-size); float R_NE = float texture(texname[0], s+size, t-size); float R_W = float texture(texname[0], s-size, t); float R_E = float texture(texname[0], s, t+size); float R_SW = float texture(texname[0], s-size, t+size); float R_S = float texture(texname[0], s, t+size); float R_SE = float texture(texname[0], s+size, t+size); //Red Compare if(abs(R-R_NW)>=difference) surfcolor = edgecolor; if(abs(R-R_N)>=difference) surfcolor = edgecolor; if(abs(R-R_NE)>=difference) surfcolor = edgecolor; if(abs(R-R_W)>=difference) surfcolor = edgecolor; if(abs(R-R_E)>=difference) surfcolor = edgecolor; if(abs(R-R_SW)>=difference) surfcolor = edgecolor; if(abs(R-R_S)>=difference) surfcolor = edgecolor; if(abs(R-R_SE)>=difference) surfcolor = edgecolor; else surfcolor = back; } else Oi = Os; Ci = Oi*Cs*diffusecolor*surfcolor; }
Possible Expansions:
My use of the code only implements edge detection over the red channel. For better results, the function should be used for red, green, blue, and alpha channels.