radialBlur.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { interleavedGradientNoise, Fn, vec2, vec4, mix, uv, Loop, premultiplyAlpha, unpremultiplyAlpha, int, float, nodeObject, convertToTexture, screenCoordinate } from 'three/tsl';
  2. /**
  3. * This TSL function blurs an image in a circular pattern, radiating from a configurable center point in screen space.
  4. *
  5. * Radial blurs can be used for different kind of effects like producing simple faked lighting effects also known as
  6. * "light shafts". The major limitation of this specific usage is the center point can only be defined in 2D so the
  7. * effect does not honor the depth of 3D objects. Consequently, it is not intended for physically correct lit scenes.
  8. *
  9. * @tsl
  10. * @function
  11. * @param {Node<vec4>} textureNode - The texture node that should be blurred.
  12. * @param {Object} [options={}] - Additional options for the radial blur effect.
  13. * @param {Node<vec2>} [options.center=vec2(0.5, 0.5)] - The center of the light in screen uvs.
  14. * @param {Node<int>} [options.weight=float(0.9)] - Base weight factor for each sample in the range `[0,1]`.
  15. * @param {Node<int>} [options.decay=float(0.95)] - Decreases the weight factor so each iteration adds less to the sum. Must be in the range `[0,1]`.
  16. * If you increase the sample count, you have to increase this option as well to avoid a darking effect.
  17. * @param {Node<int>} [options.count=int(32)] - The number if iterations. Should be in the range `[16,64]`.
  18. * @param {Node<int>} [options.exposure=float(5)] - Exposure control of the blur.
  19. * @return {Node<vec4>} The blurred texture node.
  20. */
  21. export const radialBlur = /*#__PURE__*/ Fn( ( [ textureNode, options = {} ] ) => {
  22. textureNode = convertToTexture( textureNode );
  23. const center = nodeObject( options.center ) || vec2( 0.5, 0.5 );
  24. const weight = nodeObject( options.weight ) || float( 0.9 );
  25. const decay = nodeObject( options.decay ) || float( 0.95 );
  26. const count = nodeObject( options.count ) || int( 32 );
  27. const exposure = nodeObject( options.exposure ) || float( 5 );
  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 sampleUv = vec2( textureNode.uvNode || uv() );
  34. const base = tap( sampleUv ).toConst();
  35. const blur = vec4().toVar();
  36. const offset = center.sub( sampleUv ).div( count ).toConst();
  37. const w = float( weight ).toVar();
  38. const noise = interleavedGradientNoise( screenCoordinate );
  39. sampleUv.addAssign( offset.mul( noise ) ); // mitigate banding
  40. Loop( { start: int( 0 ), end: int( count ), type: 'int', condition: '<' }, () => {
  41. sampleUv.addAssign( offset );
  42. const sample = tap( sampleUv );
  43. blur.addAssign( sample.mul( w ) );
  44. w.mulAssign( decay );
  45. } );
  46. blur.divAssign( count );
  47. blur.mulAssign( exposure );
  48. const color = mix( blur, base.mul( 2 ), 0.5 );
  49. return premultipliedAlpha ? unpremultiplyAlpha( color ) : color;
  50. } );
粤ICP备19079148号