FreiChenShader.js 3.1 KB

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