rippleFragmentShader.glsl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * This file is part of the LibreOffice project.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. #version 120
  10. #define M_PI 3.1415926535897932384626433832795
  11. uniform sampler2D leavingSlideTexture;
  12. uniform sampler2D enteringSlideTexture;
  13. uniform float time;
  14. uniform vec2 center;
  15. uniform float slideRatio;
  16. varying vec2 v_texturePosition;
  17. // This function returns the distance between two points, taking into account the slide ratio.
  18. float betterDistance(vec2 p1, vec2 p2)
  19. {
  20. p1.x *= slideRatio;
  21. p2.x *= slideRatio;
  22. return distance(p1, p2);
  23. }
  24. void main()
  25. {
  26. const float w = 0.5;
  27. const float v = 0.1;
  28. // Distance from this fragment to the center, in slide coordinates.
  29. float dist = betterDistance(center, v_texturePosition);
  30. // We want the ripple to span all of the slide at the end of the transition.
  31. float t = time * (sqrt(2.0) * (slideRatio < 1.0 ? 1.0 / slideRatio : slideRatio));
  32. // Interpolate the distance to the center in function of the time.
  33. float mixed = smoothstep(t*w-v, t*w+v, dist);
  34. // Get the displacement offset from the current pixel, for fragments that have been touched by the ripple already.
  35. vec2 offset = (v_texturePosition - center) * (sin(dist * 64.0 - time * 16.0) + 0.5) / 32.0;
  36. vec2 wavyTexCoord = mix(v_texturePosition + offset, v_texturePosition, time);
  37. // Get the final position we will sample from.
  38. vec2 pos = mix(wavyTexCoord, v_texturePosition, mixed);
  39. // Sample from the textures and mix that together.
  40. vec4 leaving = texture2D(leavingSlideTexture, pos);
  41. vec4 entering = texture2D(enteringSlideTexture, pos);
  42. gl_FragColor = mix(entering, leaving, mixed);
  43. }
  44. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */