AfterimageShader.js 949 B

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