AfterimageShader.js 875 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Afterimage shader
  3. * I created this effect inspired by a demo on codepen:
  4. * https://codepen.io/brunoimbrizi/pen/MoRJaN?page=1&
  5. */
  6. const AfterimageShader = {
  7. uniforms: {
  8. 'damp': { value: 0.96 },
  9. 'tOld': { value: null },
  10. 'tNew': { value: null }
  11. },
  12. vertexShader: /* glsl */`
  13. varying vec2 vUv;
  14. void main() {
  15. vUv = uv;
  16. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  17. }`,
  18. fragmentShader: /* glsl */`
  19. uniform float damp;
  20. uniform sampler2D tOld;
  21. uniform sampler2D tNew;
  22. varying vec2 vUv;
  23. vec4 when_gt( vec4 x, float y ) {
  24. return max( sign( x - y ), 0.0 );
  25. }
  26. void main() {
  27. vec4 texelOld = texture2D( tOld, vUv );
  28. vec4 texelNew = texture2D( tNew, vUv );
  29. texelOld *= damp * when_gt( texelOld, 0.1 );
  30. gl_FragColor = max(texelNew, texelOld);
  31. }`
  32. };
  33. export { AfterimageShader };