ConvolutionShader.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {
  2. Vector2
  3. } from 'three';
  4. /**
  5. * @module ConvolutionShader
  6. * @three_import import { ConvolutionShader } from 'three/addons/shaders/ConvolutionShader.js';
  7. */
  8. /**
  9. * Convolution shader ported from o3d sample to WebGL / GLSL.
  10. *
  11. * @constant
  12. * @type {ShaderMaterial~Shader}
  13. */
  14. const ConvolutionShader = {
  15. name: 'ConvolutionShader',
  16. defines: {
  17. 'KERNEL_SIZE_FLOAT': '25.0',
  18. 'KERNEL_SIZE_INT': '25'
  19. },
  20. uniforms: {
  21. 'tDiffuse': { value: null },
  22. 'uImageIncrement': { value: new Vector2( 0.001953125, 0.0 ) },
  23. 'cKernel': { value: [] }
  24. },
  25. vertexShader: /* glsl */`
  26. uniform vec2 uImageIncrement;
  27. varying vec2 vUv;
  28. void main() {
  29. vUv = uv - ( ( KERNEL_SIZE_FLOAT - 1.0 ) / 2.0 ) * uImageIncrement;
  30. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  31. }`,
  32. fragmentShader: /* glsl */`
  33. uniform float cKernel[ KERNEL_SIZE_INT ];
  34. uniform sampler2D tDiffuse;
  35. uniform vec2 uImageIncrement;
  36. varying vec2 vUv;
  37. void main() {
  38. vec2 imageCoord = vUv;
  39. vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );
  40. for( int i = 0; i < KERNEL_SIZE_INT; i ++ ) {
  41. sum += texture2D( tDiffuse, imageCoord ) * cKernel[ i ];
  42. imageCoord += uImageIncrement;
  43. }
  44. gl_FragColor = sum;
  45. }`
  46. };
  47. export { ConvolutionShader };