motionBlur.fragment.fx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Samplers
  2. varying vec2 vUV;
  3. uniform sampler2D textureSampler;
  4. uniform sampler2D velocitySampler;
  5. uniform float motionStrength;
  6. uniform float motionScale;
  7. uniform vec2 screenSize;
  8. void main(void)
  9. {
  10. #ifdef GEOMETRY_SUPPORTED
  11. vec2 texelSize = 1.0 / screenSize;
  12. vec2 velocityColor = texture2D(velocitySampler, vUV).rg * 2.0 - 1.0;
  13. vec2 velocity = vec2(pow(velocityColor.r, 3.0), pow(velocityColor.g, 3.0));
  14. velocity *= motionScale * motionStrength;
  15. float speed = length(velocity / texelSize);
  16. int samplesCount = int(clamp(speed, 1.0, SAMPLES));
  17. velocity = normalize(velocity) * texelSize;
  18. float hlim = float(-samplesCount) * 0.5 + 0.5;
  19. vec4 result = texture2D(textureSampler, vUV);
  20. for (int i = 1; i < int(SAMPLES); ++i)
  21. {
  22. if (i >= samplesCount)
  23. break;
  24. vec2 offset = vUV + velocity * (hlim + float(i));
  25. result += texture2D(textureSampler, offset);
  26. }
  27. gl_FragColor = result / float(samplesCount);
  28. gl_FragColor.a = 1.0;
  29. #else
  30. gl_FragColor = texture2D(textureSampler, vUV);
  31. #endif
  32. }