FreiChenShader.js 3.3 KB

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