rectangularMultiColorGradientFragmentShader.glsl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. uniform int i_nColors;
  11. uniform sampler1D t_colorArray4d;
  12. uniform sampler1D t_stopArray1d;
  13. uniform mat3x2 m_transform;
  14. varying vec2 v_textureCoords2d;
  15. int max(int x, int y)
  16. {
  17. if(x > y)
  18. return x;
  19. return y;
  20. }
  21. int findBucket(float t)
  22. {
  23. int nMinBucket=0;
  24. while( nMinBucket < i_nColors &&
  25. texture1D(t_stopArray1d, nMinBucket).s < t )
  26. ++nMinBucket;
  27. return max(nMinBucket-1,0);
  28. }
  29. void main(void)
  30. {
  31. vec2 v = abs( vec2(m_transform * vec3(v_textureCoords2d,1)) );
  32. float fAlpha = 1 - max(v.x, v.y);
  33. int nMinBucket=findBucket( fAlpha );
  34. float fLerp =
  35. (fAlpha-texture1D(t_stopArray1d, nMinBucket).s) /
  36. (texture1D(t_stopArray1d, nMinBucket+1).s -
  37. texture1D(t_stopArray1d, nMinBucket).s);
  38. gl_FragColor = mix(texture1D(t_colorArray4d, nMinBucket),
  39. texture1D(t_colorArray4d, nMinBucket+1),
  40. fLerp);
  41. }
  42. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */