SobelOperatorNode.js 4.4 KB

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