FreiChenShader.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {
  2. Vector2
  3. } from '../../../build/three.module.js';
  4. /**
  5. * Edge Detection Shader using Frei-Chen filter
  6. * Based on http://rastergrid.com/blog/2011/01/frei-chen-edge-detector
  7. *
  8. * aspect: vec2 of (1/width, 1/height)
  9. */
  10. var FreiChenShader = {
  11. uniforms: {
  12. 'tDiffuse': { value: null },
  13. 'aspect': { value: new Vector2( 512, 512 ) }
  14. },
  15. vertexShader: [
  16. 'varying vec2 vUv;',
  17. 'void main() {',
  18. ' vUv = uv;',
  19. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  20. '}'
  21. ].join( '\n' ),
  22. fragmentShader: [
  23. 'uniform sampler2D tDiffuse;',
  24. 'varying vec2 vUv;',
  25. 'uniform vec2 aspect;',
  26. 'vec2 texel = vec2(1.0 / aspect.x, 1.0 / aspect.y);',
  27. 'mat3 G[9];',
  28. // hard coded matrix values!!!! as suggested in https://github.com/neilmendoza/ofxPostProcessing/blob/master/src/EdgePass.cpp#L45
  29. 'const mat3 g0 = mat3( 0.3535533845424652, 0, -0.3535533845424652, 0.5, 0, -0.5, 0.3535533845424652, 0, -0.3535533845424652 );',
  30. 'const mat3 g1 = mat3( 0.3535533845424652, 0.5, 0.3535533845424652, 0, 0, 0, -0.3535533845424652, -0.5, -0.3535533845424652 );',
  31. 'const mat3 g2 = mat3( 0, 0.3535533845424652, -0.5, -0.3535533845424652, 0, 0.3535533845424652, 0.5, -0.3535533845424652, 0 );',
  32. 'const mat3 g3 = mat3( 0.5, -0.3535533845424652, 0, -0.3535533845424652, 0, 0.3535533845424652, 0, 0.3535533845424652, -0.5 );',
  33. 'const mat3 g4 = mat3( 0, -0.5, 0, 0.5, 0, 0.5, 0, -0.5, 0 );',
  34. 'const mat3 g5 = mat3( -0.5, 0, 0.5, 0, 0, 0, 0.5, 0, -0.5 );',
  35. 'const mat3 g6 = mat3( 0.1666666716337204, -0.3333333432674408, 0.1666666716337204, -0.3333333432674408, 0.6666666865348816, -0.3333333432674408, 0.1666666716337204, -0.3333333432674408, 0.1666666716337204 );',
  36. 'const mat3 g7 = mat3( -0.3333333432674408, 0.1666666716337204, -0.3333333432674408, 0.1666666716337204, 0.6666666865348816, 0.1666666716337204, -0.3333333432674408, 0.1666666716337204, -0.3333333432674408 );',
  37. 'const mat3 g8 = mat3( 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408 );',
  38. 'void main(void)',
  39. '{',
  40. ' G[0] = g0,',
  41. ' G[1] = g1,',
  42. ' G[2] = g2,',
  43. ' G[3] = g3,',
  44. ' G[4] = g4,',
  45. ' G[5] = g5,',
  46. ' G[6] = g6,',
  47. ' G[7] = g7,',
  48. ' G[8] = g8;',
  49. ' mat3 I;',
  50. ' float cnv[9];',
  51. ' vec3 sample;',
  52. /* fetch the 3x3 neighbourhood and use the RGB vector's length as intensity value */
  53. ' for (float i=0.0; i<3.0; i++) {',
  54. ' for (float j=0.0; j<3.0; j++) {',
  55. ' sample = texture2D(tDiffuse, vUv + texel * vec2(i-1.0,j-1.0) ).rgb;',
  56. ' I[int(i)][int(j)] = length(sample);',
  57. ' }',
  58. ' }',
  59. /* calculate the convolution values for all the masks */
  60. ' for (int i=0; i<9; i++) {',
  61. ' float dp3 = dot(G[i][0], I[0]) + dot(G[i][1], I[1]) + dot(G[i][2], I[2]);',
  62. ' cnv[i] = dp3 * dp3;',
  63. ' }',
  64. ' float M = (cnv[0] + cnv[1]) + (cnv[2] + cnv[3]);',
  65. ' float S = (cnv[4] + cnv[5]) + (cnv[6] + cnv[7]) + (cnv[8] + M);',
  66. ' gl_FragColor = vec4(vec3(sqrt(M/S)), 1.0);',
  67. '}'
  68. ].join( '\n' )
  69. };
  70. export { FreiChenShader };