boxBlur.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Fn, vec2, uv, Loop, vec4, premultiplyAlpha, unpremultiplyAlpha, max, int, textureSize, nodeObject, convertToTexture } from 'three/tsl';
  2. /**
  3. * Applies a box blur effect to the given texture node.
  4. *
  5. * Compared to Gaussian blur, box blur produces a more blocky result but with better performance when correctly
  6. * configured. It is intended for mobile devices or performance restricted use cases where Gaussian is too heavy.
  7. *
  8. * The (kernel) `size` parameter should be small (1, 2 or 3) since it determines the number of samples based on (size * 2 + 1)^2.
  9. * This implementation uses a single pass approach so the kernel is not applied as a separable filter. That means larger
  10. * kernels won't perform well. Use Gaussian instead if you need a more high-quality blur.
  11. *
  12. * To produce wider blurs, increase the `separation` parameter instead which has no influence on the performance.
  13. *
  14. * Reference: {@link https://github.com/lettier/3d-game-shaders-for-beginners/blob/master/demonstration/shaders/fragment/box-blur.frag}.
  15. *
  16. * @function
  17. * @param {Node<vec4>} textureNode - The texture node that should be blurred.
  18. * @param {Object} [options={}] - Additional options for the hash blur effect.
  19. * @param {Node<int>} [options.size=int(1)] - Controls the blur's kernel. For performant results, the range should within [1, 3].
  20. * @param {Node<int>} [options.separation=int(1)] - Spreads out the blur without having to sample additional fragments. Ranges from [1, Infinity].
  21. * @param {boolean} [options.premultipliedAlpha=false] - Whether to use premultiplied alpha for the blur effect.
  22. * @return {Node<vec4>} The blurred texture node.
  23. */
  24. export const boxBlur = /*#__PURE__*/ Fn( ( [ textureNode, options = {} ] ) => {
  25. textureNode = convertToTexture( textureNode );
  26. const size = nodeObject( options.size ) || int( 1 );
  27. const separation = nodeObject( options.separation ) || int( 1 );
  28. const premultipliedAlpha = options.premultipliedAlpha || false;
  29. const tap = ( uv ) => {
  30. const sample = textureNode.sample( uv );
  31. return premultipliedAlpha ? premultiplyAlpha( sample ) : sample;
  32. };
  33. const targetUV = textureNode.uvNode || uv();
  34. const result = vec4( 0 );
  35. const sep = max( separation, 1 );
  36. const count = int( 0 );
  37. const pixelStep = vec2( 1 ).div( textureSize( textureNode ) );
  38. Loop( { start: size.negate(), end: size, name: 'i', condition: '<=' }, ( { i } ) => {
  39. Loop( { start: size.negate(), end: size, name: 'j', condition: '<=' }, ( { j } ) => {
  40. const uvs = targetUV.add( vec2( i, j ).mul( pixelStep ).mul( sep ) );
  41. result.addAssign( tap( uvs ) );
  42. count.addAssign( 1 );
  43. } );
  44. } );
  45. result.divAssign( count );
  46. return premultipliedAlpha ? unpremultiplyAlpha( result ) : result;
  47. } );
粤ICP备19079148号