SobelOperatorNode.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import TempNode from '../core/TempNode.js';
  2. import { uv } from '../accessors/UVNode.js';
  3. import { luminance } from './ColorAdjustmentNode.js';
  4. import { addNodeElement, tslFn, nodeObject, vec2, vec3, vec4, mat3 } from '../shadernode/ShaderNode.js';
  5. import { NodeUpdateType } from '../core/constants.js';
  6. import { uniform } from '../core/UniformNode.js';
  7. import { add } from '../math/OperatorNode.js';
  8. import { Vector2 } from 'three';
  9. class SobelOperatorNode extends TempNode {
  10. constructor( textureNode ) {
  11. super();
  12. this.textureNode = textureNode;
  13. this.updateBeforeType = NodeUpdateType.RENDER;
  14. this._invSize = uniform( new Vector2() );
  15. }
  16. updateBefore() {
  17. const map = this.textureNode.value;
  18. this._invSize.value.set( 1 / map.image.width, 1 / map.image.height );
  19. }
  20. setup() {
  21. const { textureNode } = this;
  22. const uvNode = textureNode.uvNode || uv();
  23. const sampleTexture = ( uv ) => this.textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
  24. const sobel = tslFn( () => {
  25. // Sobel Edge Detection (see https://youtu.be/uihBwtPIBxM)
  26. const texel = this._invSize;
  27. // kernel definition (in glsl matrices are filled in column-major order)
  28. const Gx = mat3( - 1, - 2, - 1, 0, 0, 0, 1, 2, 1 ); // x direction kernel
  29. const Gy = mat3( - 1, 0, 1, - 2, 0, 2, - 1, 0, 1 ); // y direction kernel
  30. // fetch the 3x3 neighbourhood of a fragment
  31. // first column
  32. const tx0y0 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( - 1, - 1 ) ) ) ).xyz );
  33. const tx0y1 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( - 1, 0 ) ) ) ).xyz );
  34. const tx0y2 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( - 1, 1 ) ) ) ).xyz );
  35. // second column
  36. const tx1y0 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 0, - 1 ) ) ) ).xyz );
  37. const tx1y1 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 0, 0 ) ) ) ).xyz );
  38. const tx1y2 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 0, 1 ) ) ) ).xyz );
  39. // third column
  40. const tx2y0 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 1, - 1 ) ) ) ).xyz );
  41. const tx2y1 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 1, 0 ) ) ) ).xyz );
  42. const tx2y2 = luminance( sampleTexture( uvNode.add( texel.mul( vec2( 1, 1 ) ) ) ).xyz );
  43. // gradient value in x direction
  44. const valueGx = add(
  45. Gx[ 0 ][ 0 ].mul( tx0y0 ),
  46. Gx[ 1 ][ 0 ].mul( tx1y0 ),
  47. Gx[ 2 ][ 0 ].mul( tx2y0 ),
  48. Gx[ 0 ][ 1 ].mul( tx0y1 ),
  49. Gx[ 1 ][ 1 ].mul( tx1y1 ),
  50. Gx[ 2 ][ 1 ].mul( tx2y1 ),
  51. Gx[ 0 ][ 2 ].mul( tx0y2 ),
  52. Gx[ 1 ][ 2 ].mul( tx1y2 ),
  53. Gx[ 2 ][ 2 ].mul( tx2y2 )
  54. );
  55. // gradient value in y direction
  56. const valueGy = add(
  57. Gy[ 0 ][ 0 ].mul( tx0y0 ),
  58. Gy[ 1 ][ 0 ].mul( tx1y0 ),
  59. Gy[ 2 ][ 0 ].mul( tx2y0 ),
  60. Gy[ 0 ][ 1 ].mul( tx0y1 ),
  61. Gy[ 1 ][ 1 ].mul( tx1y1 ),
  62. Gy[ 2 ][ 1 ].mul( tx2y1 ),
  63. Gy[ 0 ][ 2 ].mul( tx0y2 ),
  64. Gy[ 1 ][ 2 ].mul( tx1y2 ),
  65. Gy[ 2 ][ 2 ].mul( tx2y2 )
  66. );
  67. // magnitute of the total gradient
  68. const G = valueGx.mul( valueGx ).add( valueGy.mul( valueGy ) ).sqrt();
  69. return vec4( vec3( G ), 1 );
  70. } );
  71. const outputNode = sobel();
  72. return outputNode;
  73. }
  74. }
  75. export const sobel = ( node ) => nodeObject( new SobelOperatorNode( nodeObject( node ) ) );
  76. addNodeElement( 'sobel', sobel );
  77. export default SobelOperatorNode;