SobelOperatorShader.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. console.warn( "THREE.SobelOperatorShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * Sobel Edge Detection (see https://youtu.be/uihBwtPIBxM)
  4. *
  5. * As mentioned in the video the Sobel operator expects a grayscale image as input.
  6. *
  7. */
  8. THREE.SobelOperatorShader = {
  9. uniforms: {
  10. "tDiffuse": { value: null },
  11. "resolution": { value: new THREE.Vector2() }
  12. },
  13. vertexShader: [
  14. "varying vec2 vUv;",
  15. "void main() {",
  16. " vUv = uv;",
  17. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  18. "}"
  19. ].join( "\n" ),
  20. fragmentShader: [
  21. "uniform sampler2D tDiffuse;",
  22. "uniform vec2 resolution;",
  23. "varying vec2 vUv;",
  24. "void main() {",
  25. " vec2 texel = vec2( 1.0 / resolution.x, 1.0 / resolution.y );",
  26. // kernel definition (in glsl matrices are filled in column-major order)
  27. " const mat3 Gx = mat3( -1, -2, -1, 0, 0, 0, 1, 2, 1 );", // x direction kernel
  28. " const mat3 Gy = mat3( -1, 0, 1, -2, 0, 2, -1, 0, 1 );", // y direction kernel
  29. // fetch the 3x3 neighbourhood of a fragment
  30. // first column
  31. " float tx0y0 = texture2D( tDiffuse, vUv + texel * vec2( -1, -1 ) ).r;",
  32. " float tx0y1 = texture2D( tDiffuse, vUv + texel * vec2( -1, 0 ) ).r;",
  33. " float tx0y2 = texture2D( tDiffuse, vUv + texel * vec2( -1, 1 ) ).r;",
  34. // second column
  35. " float tx1y0 = texture2D( tDiffuse, vUv + texel * vec2( 0, -1 ) ).r;",
  36. " float tx1y1 = texture2D( tDiffuse, vUv + texel * vec2( 0, 0 ) ).r;",
  37. " float tx1y2 = texture2D( tDiffuse, vUv + texel * vec2( 0, 1 ) ).r;",
  38. // third column
  39. " float tx2y0 = texture2D( tDiffuse, vUv + texel * vec2( 1, -1 ) ).r;",
  40. " float tx2y1 = texture2D( tDiffuse, vUv + texel * vec2( 1, 0 ) ).r;",
  41. " float tx2y2 = texture2D( tDiffuse, vUv + texel * vec2( 1, 1 ) ).r;",
  42. // gradient value in x direction
  43. " float valueGx = Gx[0][0] * tx0y0 + Gx[1][0] * tx1y0 + Gx[2][0] * tx2y0 + ",
  44. " Gx[0][1] * tx0y1 + Gx[1][1] * tx1y1 + Gx[2][1] * tx2y1 + ",
  45. " Gx[0][2] * tx0y2 + Gx[1][2] * tx1y2 + Gx[2][2] * tx2y2; ",
  46. // gradient value in y direction
  47. " float valueGy = Gy[0][0] * tx0y0 + Gy[1][0] * tx1y0 + Gy[2][0] * tx2y0 + ",
  48. " Gy[0][1] * tx0y1 + Gy[1][1] * tx1y1 + Gy[2][1] * tx2y1 + ",
  49. " Gy[0][2] * tx0y2 + Gy[1][2] * tx1y2 + Gy[2][2] * tx2y2; ",
  50. // magnitute of the total gradient
  51. " float G = sqrt( ( valueGx * valueGx ) + ( valueGy * valueGy ) );",
  52. " gl_FragColor = vec4( vec3( G ), 1 );",
  53. "}"
  54. ].join( "\n" )
  55. };