BleachBypass.js 987 B

123456789101112131415161718192021222324252627282930313233
  1. import { float, Fn, vec3, vec4, min, max, mix, luminance } from 'three/tsl';
  2. /**
  3. * Applies a bleach bypass effect to the given color node.
  4. *
  5. * @tsl
  6. * @function
  7. * @param {Node<vec4>} color - The color node to apply the sepia for.
  8. * @param {Node<float>} [opacity=1] - Influences how strong the effect is blended with the original color.
  9. * @return {Node<vec4>} The updated color node.
  10. */
  11. export const bleach = /*@__PURE__*/ Fn( ( [ color, opacity = 1 ] ) => {
  12. const base = color;
  13. const lum = luminance( base.rgb );
  14. const blend = vec3( lum );
  15. const L = min( 1.0, max( 0.0, float( 10.0 ).mul( lum.sub( 0.45 ) ) ) );
  16. const result1 = blend.mul( base.rgb ).mul( 2.0 );
  17. const result2 = float( 2.0 ).mul( blend.oneMinus() ).mul( base.rgb.oneMinus() ).oneMinus();
  18. const newColor = mix( result1, result2, L );
  19. const A2 = base.a.mul( opacity );
  20. const mixRGB = A2.mul( newColor.rgb );
  21. mixRGB.addAssign( base.rgb.mul( A2.oneMinus() ) );
  22. return vec4( mixRGB, base.a );
  23. } );