ConvolutionShader.js 1.2 KB

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