hashBlur.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { float, Fn, vec2, uv, sin, rand, degrees, cos, Loop, vec4 } from 'three/tsl';
  2. /**
  3. * Applies a hash blur effect to the given texture node.
  4. *
  5. * Reference: {@link https://www.shadertoy.com/view/4lXXWn}.
  6. *
  7. * @tsl
  8. * @function
  9. * @param {Node<vec4>} textureNode - The texture node that should be blurred.
  10. * @param {Node<float>} [bluramount=float(0.1)] - This node determines the amount of blur.
  11. * @param {Node<float>} [repeats=float(45)] - This node determines the quality of the blur. A higher value produces a less grainy result but is also more expensive.
  12. * @return {Node<vec4>} The blurred texture node.
  13. */
  14. export const hashBlur = /*#__PURE__*/ Fn( ( [ textureNode, bluramount = float( 0.1 ), repeats = float( 45 ) ] ) => {
  15. const draw = ( uv ) => textureNode.sample( uv );
  16. const targetUV = textureNode.uvNode || uv();
  17. const blurred_image = vec4( 0. ).toVar();
  18. Loop( { start: 0., end: repeats, type: 'float' }, ( { i } ) => {
  19. const q = vec2( vec2( cos( degrees( i.div( repeats ).mul( 360. ) ) ), sin( degrees( i.div( repeats ).mul( 360. ) ) ) ).mul( rand( vec2( i, targetUV.x.add( targetUV.y ) ) ).add( bluramount ) ) );
  20. const uv2 = vec2( targetUV.add( q.mul( bluramount ) ) );
  21. blurred_image.addAssign( draw( uv2 ) );
  22. } );
  23. blurred_image.divAssign( repeats );
  24. return blurred_image;
  25. } );
粤ICP备19079148号