SobelOperatorNode.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { Vector2, TempNode, NodeUpdateType } from 'three/webgpu';
  2. import { nodeObject, Fn, uv, uniform, convertToTexture, vec2, vec3, vec4, mat3, luminance, add } from 'three/tsl';
  3. /**
  4. * Post processing node for detecting edges with a sobel filter.
  5. * A sobel filter should be applied after tone mapping and output color
  6. * space conversion.
  7. *
  8. * @augments TempNode
  9. * @three_import import { sobel } from 'three/addons/tsl/display/SobelOperatorNode.js';
  10. */
  11. class SobelOperatorNode extends TempNode {
  12. static get type() {
  13. return 'SobelOperatorNode';
  14. }
  15. /**
  16. * Constructs a new sobel operator node.
  17. *
  18. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  19. */
  20. constructor( textureNode ) {
  21. super( 'vec4' );
  22. /**
  23. * The texture node that represents the input of the effect.
  24. *
  25. * @type {TextureNode}
  26. */
  27. this.textureNode = textureNode;
  28. /**
  29. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node updates
  30. * its internal uniforms once per frame in `updateBefore()`.
  31. *
  32. * @type {string}
  33. * @default 'frame'
  34. */
  35. this.updateBeforeType = NodeUpdateType.FRAME;
  36. /**
  37. * A uniform node holding the inverse resolution value.
  38. *
  39. * @private
  40. * @type {UniformNode<vec2>}
  41. */
  42. this._invSize = uniform( new Vector2() );
  43. }
  44. /**
  45. * This method is used to update the effect's uniforms once per frame.
  46. *
  47. * @param {NodeFrame} frame - The current node frame.
  48. */
  49. updateBefore( /* frame */ ) {
  50. const map = this.textureNode.value;
  51. this._invSize.value.set( 1 / map.image.width, 1 / map.image.height );
  52. }
  53. /**
  54. * This method is used to setup the effect's TSL code.
  55. *
  56. * @param {NodeBuilder} builder - The current node builder.
  57. * @return {ShaderCallNodeInternal}
  58. */
  59. setup( /* builder */ ) {
  60. const { textureNode } = this;
  61. const uvNode = textureNode.uvNode || uv();
  62. const sampleTexture = ( uv ) => textureNode.sample( uv );
  63. const sobel = Fn( () => {
  64. // Sobel Edge Detection (see https://youtu.be/uihBwtPIBxM)
  65. const texel = this._invSize;
  66. // kernel definition (in glsl matrices are filled in column-major order)
  67. const Gx = mat3( - 1, - 2, - 1, 0, 0, 0, 1, 2, 1 ); // x direction kernel
  68. const Gy = mat3( - 1, 0, 1, - 2, 0, 2, - 1, 0, 1 ); // y direction kernel
  69. // fetch the 3x3 neighbourhood of a fragment
  70. // first column
  71. const tx0y0 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( - 1, - 1 ) ) ) ).xyz );
  72. const tx0y1 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( - 1, 0 ) ) ) ).xyz );
  73. const tx0y2 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( - 1, 1 ) ) ) ).xyz );
  74. // second column
  75. const tx1y0 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 0, - 1 ) ) ) ).xyz );
  76. const tx1y1 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 0, 0 ) ) ) ).xyz );
  77. const tx1y2 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 0, 1 ) ) ) ).xyz );
  78. // third column
  79. const tx2y0 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 1, - 1 ) ) ) ).xyz );
  80. const tx2y1 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 1, 0 ) ) ) ).xyz );
  81. const tx2y2 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 1, 1 ) ) ) ).xyz );
  82. // gradient value in x direction
  83. const valueGx = add(
  84. Gx[ 0 ][ 0 ].mul( tx0y0 ),
  85. Gx[ 1 ][ 0 ].mul( tx1y0 ),
  86. Gx[ 2 ][ 0 ].mul( tx2y0 ),
  87. Gx[ 0 ][ 1 ].mul( tx0y1 ),
  88. Gx[ 1 ][ 1 ].mul( tx1y1 ),
  89. Gx[ 2 ][ 1 ].mul( tx2y1 ),
  90. Gx[ 0 ][ 2 ].mul( tx0y2 ),
  91. Gx[ 1 ][ 2 ].mul( tx1y2 ),
  92. Gx[ 2 ][ 2 ].mul( tx2y2 )
  93. );
  94. // gradient value in y direction
  95. const valueGy = add(
  96. Gy[ 0 ][ 0 ].mul( tx0y0 ),
  97. Gy[ 1 ][ 0 ].mul( tx1y0 ),
  98. Gy[ 2 ][ 0 ].mul( tx2y0 ),
  99. Gy[ 0 ][ 1 ].mul( tx0y1 ),
  100. Gy[ 1 ][ 1 ].mul( tx1y1 ),
  101. Gy[ 2 ][ 1 ].mul( tx2y1 ),
  102. Gy[ 0 ][ 2 ].mul( tx0y2 ),
  103. Gy[ 1 ][ 2 ].mul( tx1y2 ),
  104. Gy[ 2 ][ 2 ].mul( tx2y2 )
  105. );
  106. // magnitude of the total gradient
  107. const G = valueGx.mul( valueGx ).add( valueGy.mul( valueGy ) ).sqrt();
  108. return vec4( vec3( G ), 1 );
  109. } );
  110. const outputNode = sobel();
  111. return outputNode;
  112. }
  113. }
  114. export default SobelOperatorNode;
  115. /**
  116. * TSL function for creating a sobel operator node which performs edge detection with a sobel filter.
  117. *
  118. * @tsl
  119. * @function
  120. * @param {Node<vec4>} node - The node that represents the input of the effect.
  121. * @returns {SobelOperatorNode}
  122. */
  123. export const sobel = ( node ) => nodeObject( new SobelOperatorNode( convertToTexture( node ) ) );