LuminosityHighPassShader.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {
  2. Color
  3. } from 'three';
  4. /**
  5. * @module LuminosityHighPassShader
  6. * @three_import import { LuminosityHighPassShader } from 'three/addons/shaders/LuminosityHighPassShader.js';
  7. */
  8. /**
  9. * Luminosity high pass shader.
  10. *
  11. * @constant
  12. * @type {ShaderMaterial~Shader}
  13. */
  14. const LuminosityHighPassShader = {
  15. name: 'LuminosityHighPassShader',
  16. uniforms: {
  17. 'tDiffuse': { value: null },
  18. 'luminosityThreshold': { value: 1.0 },
  19. 'smoothWidth': { value: 1.0 },
  20. 'defaultColor': { value: new Color( 0x000000 ) },
  21. 'defaultOpacity': { value: 0.0 }
  22. },
  23. vertexShader: /* glsl */`
  24. varying vec2 vUv;
  25. void main() {
  26. vUv = uv;
  27. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  28. }`,
  29. fragmentShader: /* glsl */`
  30. uniform sampler2D tDiffuse;
  31. uniform vec3 defaultColor;
  32. uniform float defaultOpacity;
  33. uniform float luminosityThreshold;
  34. uniform float smoothWidth;
  35. varying vec2 vUv;
  36. void main() {
  37. vec4 texel = texture2D( tDiffuse, vUv );
  38. float v = luminance( texel.xyz );
  39. vec4 outputColor = vec4( defaultColor.rgb, defaultOpacity );
  40. float alpha = smoothstep( luminosityThreshold, luminosityThreshold + smoothWidth, v );
  41. gl_FragColor = mix( outputColor, texel, alpha );
  42. }`
  43. };
  44. export { LuminosityHighPassShader };