SobelOperatorShader.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @author Mugen87 / https://github.com/Mugen87
  3. *
  4. * Sobel Edge Detection (see https://youtu.be/uihBwtPIBxM)
  5. *
  6. * As mentioned in the video the Sobel operator expects a grayscale image as input.
  7. *
  8. */
  9. THREE.SobelOperatorShader = {
  10. uniforms: {
  11. "tDiffuse": { value: null },
  12. "resolution": { value: new THREE.Vector2() }
  13. },
  14. vertexShader: [
  15. "void main() {",
  16. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  17. "}"
  18. ].join( "\n" ),
  19. fragmentShader: [
  20. "uniform sampler2D tDiffuse;",
  21. "uniform vec2 resolution;",
  22. "void main() {",
  23. // kernel definition (in glsl matrices are filled in column-major order)
  24. "const mat3 Gx = mat3( -1, -2, -1, 0, 0, 0, 1, 2, 1 );", // x direction kernel
  25. "const mat3 Gy = mat3( -1, 0, 1, -2, 0, 2, -1, 0, 1 );", // y direction kernel
  26. // fetch the 3x3 neighbourhood of a fragment
  27. "vec2 p = vec2( gl_FragCoord.x, gl_FragCoord.y );",
  28. // first column
  29. "vec2 x0y0 = ( p + vec2( -1, -1 ) ) / resolution;",
  30. "vec2 x0y1 = ( p + vec2( -1, 0 ) ) / resolution;",
  31. "vec2 x0y2 = ( p + vec2( -1, 1 ) ) / resolution;",
  32. // second column
  33. "vec2 x1y0 = ( p + vec2( 0, -1 ) ) / resolution;",
  34. "vec2 x1y1 = ( p + vec2( 0, 0 ) ) / resolution;",
  35. "vec2 x1y2 = ( p + vec2( 0, 1 ) ) / resolution;",
  36. // third column
  37. "vec2 x2y0 = ( p + vec2( 1, -1 ) ) / resolution;",
  38. "vec2 x2y1 = ( p + vec2( 1, 0 ) ) / resolution;",
  39. "vec2 x2y2 = ( p + vec2( 1, 1 ) ) / resolution;",
  40. // sample values (we assume grayscale colors so only read a single channel)
  41. "float tx0y0 = texture2D( tDiffuse, x0y0 ).r;",
  42. "float tx0y1 = texture2D( tDiffuse, x0y1 ).r;",
  43. "float tx0y2 = texture2D( tDiffuse, x0y2 ).r;",
  44. "float tx1y0 = texture2D( tDiffuse, x1y0 ).r;",
  45. "float tx1y1 = texture2D( tDiffuse, x1y1 ).r;",
  46. "float tx1y2 = texture2D( tDiffuse, x1y2 ).r;",
  47. "float tx2y0 = texture2D( tDiffuse, x2y0 ).r;",
  48. "float tx2y1 = texture2D( tDiffuse, x2y1 ).r;",
  49. "float tx2y2 = texture2D( tDiffuse, x2y2 ).r;",
  50. // gradient value in x direction
  51. "float valueGx = Gx[0][0] * tx0y0 + Gx[1][0] * tx1y0 + Gx[2][0] * tx2y0 + ",
  52. " Gx[0][1] * tx0y1 + Gx[1][1] * tx1y1 + Gx[2][1] * tx2y1 + ",
  53. " Gx[0][2] * tx0y2 + Gx[1][2] * tx1y2 + Gx[2][2] * tx2y2; ",
  54. // gradient value in y direction
  55. "float valueGy = Gy[0][0] * tx0y0 + Gy[1][0] * tx1y0 + Gy[2][0] * tx2y0 + ",
  56. " Gy[0][1] * tx0y1 + Gy[1][1] * tx1y1 + Gy[2][1] * tx2y1 + ",
  57. " Gy[0][2] * tx0y2 + Gy[1][2] * tx1y2 + Gy[2][2] * tx2y2; ",
  58. // magnitute of the total gradient
  59. "float G = sqrt( ( valueGx * valueGx ) + ( valueGy * valueGy ) );",
  60. "gl_FragColor = vec4( vec3( G ), 1 );",
  61. "}"
  62. ].join( "\n" )
  63. };