|
| 1 | + |
| 2 | +// Original shader by Ken Slade |
| 3 | +// https://www.shadertoy.com/view/ldsSWr |
| 4 | + |
| 5 | +// Ported to Processing by Raphaël de Courville <twitter: @sableRaph> |
| 6 | + |
| 7 | +#ifdef GL_ES |
| 8 | +precision highp float; |
| 9 | +#endif |
| 10 | + |
| 11 | +// This line is optional from Processing 2.1 and up |
| 12 | +#define PROCESSING_TEXTURE_SHADER |
| 13 | + |
| 14 | +uniform sampler2D texture; // iChannel0 in Shadertoy |
| 15 | +uniform vec2 sketchSize; // iResolution in Shadertoy |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +// * Code from Shadertoy * |
| 20 | + |
| 21 | +// Note: Uniforms have been renamed (see above) |
| 22 | + |
| 23 | +// Note 2: void mainImage( out vec4 fragColor, in vec2 fragCoord ) |
| 24 | +// was changed into |
| 25 | +// void main( void ) |
| 26 | + |
| 27 | +// Note 3: fragCoord and fragColor had to be renamed into |
| 28 | +// gl_FragCoord and gl_FragColor |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +// Basic edge detection via convolution |
| 34 | + |
| 35 | +// at https://www.shadertoy.com/view/ldsSWr |
| 36 | + |
| 37 | +// Based on original Sobel shader by: |
| 38 | +// Jeroen Baert - [email protected] (www.forceflow.be) |
| 39 | +// at https://www.shadertoy.com/view/Xdf3Rf |
| 40 | + |
| 41 | +//options are edge, colorEdge, or trueColorEdge |
| 42 | +#define EDGE_FUNC edge |
| 43 | + |
| 44 | +//options are KAYYALI_NESW, KAYYALI_SENW, PREWITT, ROBERTSCROSS, SCHARR, or SOBEL |
| 45 | +#define SOBEL |
| 46 | + |
| 47 | +// Use these parameters to fiddle with settings |
| 48 | +#ifdef SCHARR |
| 49 | +#define STEP 0.15 |
| 50 | +#else |
| 51 | +#define STEP 1.0 |
| 52 | +#endif |
| 53 | + |
| 54 | + |
| 55 | +#ifdef KAYYALI_NESW |
| 56 | +const mat3 kayyali_NESW = mat3(-6.0, 0.0, 6.0, |
| 57 | + 0.0, 0.0, 0.0, |
| 58 | + 6.0, 0.0, -6.0); |
| 59 | +#endif |
| 60 | +#ifdef KAYYALI_SENW |
| 61 | +const mat3 kayyali_SENW = mat3(6.0, 0.0, -6.0, |
| 62 | + 0.0, 0.0, 0.0, |
| 63 | + -6.0, 0.0, 6.0); |
| 64 | +#endif |
| 65 | +#ifdef PREWITT |
| 66 | +// Prewitt masks (see http://en.wikipedia.org/wiki/Prewitt_operator) |
| 67 | +const mat3 prewittKernelX = mat3(-1.0, 0.0, 1.0, |
| 68 | + -1.0, 0.0, 1.0, |
| 69 | + -1.0, 0.0, 1.0); |
| 70 | + |
| 71 | +const mat3 prewittKernelY = mat3(1.0, 1.0, 1.0, |
| 72 | + 0.0, 0.0, 0.0, |
| 73 | + -1.0, -1.0, -1.0); |
| 74 | +#endif |
| 75 | +#ifdef ROBERTSCROSS |
| 76 | +// Roberts Cross masks (see http://en.wikipedia.org/wiki/Roberts_cross) |
| 77 | +const mat3 robertsCrossKernelX = mat3(1.0, 0.0, 0.0, |
| 78 | + 0.0, -1.0, 0.0, |
| 79 | + 0.0, 0.0, 0.0); |
| 80 | +const mat3 robertsCrossKernelY = mat3(0.0, 1.0, 0.0, |
| 81 | + -1.0, 0.0, 0.0, |
| 82 | + 0.0, 0.0, 0.0); |
| 83 | +#endif |
| 84 | +#ifdef SCHARR |
| 85 | +// Scharr masks (see http://en.wikipedia.org/wiki/Sobel_operator#Alternative_operators) |
| 86 | +const mat3 scharrKernelX = mat3(3.0, 10.0, 3.0, |
| 87 | + 0.0, 0.0, 0.0, |
| 88 | + -3.0, -10.0, -3.0); |
| 89 | + |
| 90 | +const mat3 scharrKernelY = mat3(3.0, 0.0, -3.0, |
| 91 | + 10.0, 0.0, -10.0, |
| 92 | + 3.0, 0.0, -3.0); |
| 93 | +#endif |
| 94 | +#ifdef SOBEL |
| 95 | +// Sobel masks (see http://en.wikipedia.org/wiki/Sobel_operator) |
| 96 | +const mat3 sobelKernelX = mat3(1.0, 0.0, -1.0, |
| 97 | + 2.0, 0.0, -2.0, |
| 98 | + 1.0, 0.0, -1.0); |
| 99 | + |
| 100 | +const mat3 sobelKernelY = mat3(-1.0, -2.0, -1.0, |
| 101 | + 0.0, 0.0, 0.0, |
| 102 | + 1.0, 2.0, 1.0); |
| 103 | +#endif |
| 104 | + |
| 105 | +//performs a convolution on an image with the given kernel |
| 106 | +float convolve(mat3 kernel, mat3 image) { |
| 107 | + float result = 0.0; |
| 108 | + for (int i = 0; i < 3; i++) { |
| 109 | + for (int j = 0; j < 3; j++) { |
| 110 | + result += kernel[i][j]*image[i][j]; |
| 111 | + } |
| 112 | + } |
| 113 | + return result; |
| 114 | +} |
| 115 | + |
| 116 | +//helper function for colorEdge() |
| 117 | +float convolveComponent(mat3 kernelX, mat3 kernelY, mat3 image) { |
| 118 | + vec2 result; |
| 119 | + result.x = convolve(kernelX, image); |
| 120 | + result.y = convolve(kernelY, image); |
| 121 | + return clamp(length(result), 0.0, 255.0); |
| 122 | +} |
| 123 | + |
| 124 | +//returns color edges using the separated color components for the measure of intensity |
| 125 | +//for each color component instead of using the same intensity for all three. This results |
| 126 | +//in false color edges when transitioning from one color to another, but true colors when |
| 127 | +//the transition is from black to color (or color to black). |
| 128 | +vec4 colorEdge(float stepx, float stepy, vec2 center, mat3 kernelX, mat3 kernelY) { |
| 129 | + //get samples around pixel |
| 130 | + vec4 colors[9]; |
| 131 | + colors[0] = texture2D(texture,center + vec2(-stepx,stepy)); |
| 132 | + colors[1] = texture2D(texture,center + vec2(0,stepy)); |
| 133 | + colors[2] = texture2D(texture,center + vec2(stepx,stepy)); |
| 134 | + colors[3] = texture2D(texture,center + vec2(-stepx,0)); |
| 135 | + colors[4] = texture2D(texture,center); |
| 136 | + colors[5] = texture2D(texture,center + vec2(stepx,0)); |
| 137 | + colors[6] = texture2D(texture,center + vec2(-stepx,-stepy)); |
| 138 | + colors[7] = texture2D(texture,center + vec2(0,-stepy)); |
| 139 | + colors[8] = texture2D(texture,center + vec2(stepx,-stepy)); |
| 140 | + |
| 141 | + mat3 imageR, imageG, imageB, imageA; |
| 142 | + for (int i = 0; i < 3; i++) { |
| 143 | + for (int j = 0; j < 3; j++) { |
| 144 | + imageR[i][j] = colors[i*3+j].r; |
| 145 | + imageG[i][j] = colors[i*3+j].g; |
| 146 | + imageB[i][j] = colors[i*3+j].b; |
| 147 | + imageA[i][j] = colors[i*3+j].a; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + vec4 color; |
| 152 | + color.r = convolveComponent(kernelX, kernelY, imageR); |
| 153 | + color.g = convolveComponent(kernelX, kernelY, imageG); |
| 154 | + color.b = convolveComponent(kernelX, kernelY, imageB); |
| 155 | + color.a = convolveComponent(kernelX, kernelY, imageA); |
| 156 | + |
| 157 | + return color; |
| 158 | +} |
| 159 | + |
| 160 | +//finds edges where fragment intensity changes from a higher value to a lower one (or |
| 161 | +//vice versa). |
| 162 | +vec4 edge(float stepx, float stepy, vec2 center, mat3 kernelX, mat3 kernelY){ |
| 163 | + // get samples around pixel |
| 164 | + mat3 image = mat3(length(texture2D(texture,center + vec2(-stepx,stepy)).rgb), |
| 165 | + length(texture2D(texture,center + vec2(0,stepy)).rgb), |
| 166 | + length(texture2D(texture,center + vec2(stepx,stepy)).rgb), |
| 167 | + length(texture2D(texture,center + vec2(-stepx,0)).rgb), |
| 168 | + length(texture2D(texture,center).rgb), |
| 169 | + length(texture2D(texture,center + vec2(stepx,0)).rgb), |
| 170 | + length(texture2D(texture,center + vec2(-stepx,-stepy)).rgb), |
| 171 | + length(texture2D(texture,center + vec2(0,-stepy)).rgb), |
| 172 | + length(texture2D(texture,center + vec2(stepx,-stepy)).rgb)); |
| 173 | + vec2 result; |
| 174 | + result.x = convolve(kernelX, image); |
| 175 | + result.y = convolve(kernelY, image); |
| 176 | + |
| 177 | + float color = clamp(length(result), 0.0, 255.0); |
| 178 | + return vec4(color); |
| 179 | +} |
| 180 | + |
| 181 | +//Colors edges using the actual color for the fragment at this location |
| 182 | +vec4 trueColorEdge(float stepx, float stepy, vec2 center, mat3 kernelX, mat3 kernelY) { |
| 183 | + vec4 edgeVal = edge(stepx, stepy, center, kernelX, kernelY); |
| 184 | + return edgeVal * texture2D(texture,center); |
| 185 | +} |
| 186 | + |
| 187 | +void main( void ){ |
| 188 | + vec2 uv = gl_FragCoord.xy / sketchSize.xy; |
| 189 | + vec4 color = texture2D(texture, uv.xy); |
| 190 | +#ifdef KAYYALI_NESW |
| 191 | + gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1], |
| 192 | + uv, |
| 193 | + kayyali_NESW, kayyali_NESW); |
| 194 | +#endif |
| 195 | +#ifdef KAYYALI_SENW |
| 196 | + gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1], |
| 197 | + uv, |
| 198 | + kayyali_SENW, kayyali_SENW); |
| 199 | +#endif |
| 200 | +#ifdef PREWITT |
| 201 | + gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1], |
| 202 | + uv, |
| 203 | + prewittKernelX, prewittKernelY); |
| 204 | +#endif |
| 205 | +#ifdef ROBERTSCROSS |
| 206 | + gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1], |
| 207 | + uv, |
| 208 | + robertsCrossKernelX, robertsCrossKernelY); |
| 209 | +#endif |
| 210 | +#ifdef SOBEL |
| 211 | + gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1], |
| 212 | + uv, |
| 213 | + sobelKernelX, sobelKernelY); |
| 214 | +#endif |
| 215 | +#ifdef SCHARR |
| 216 | + gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1], |
| 217 | + uv, |
| 218 | + scharrKernelX, scharrKernelY); |
| 219 | +#endif |
| 220 | +} |
0 commit comments