radialMultiColorGradientFragmentShader.glsl 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. const vec2 v_center2d = vec2(0,0);
  16. int max(int x, int y)
  17. {
  18. if(x > y)
  19. return x;
  20. return y;
  21. }
  22. int findBucket(float t)
  23. {
  24. int nMinBucket=0;
  25. while( nMinBucket < i_nColors &&
  26. texture1D(t_stopArray1d, nMinBucket).s < t )
  27. ++nMinBucket;
  28. return max(nMinBucket-1,0);
  29. }
  30. void main(void)
  31. {
  32. float fAlpha =
  33. clamp( 1.0 - distance(
  34. vec2( m_transform * vec3(v_textureCoords2d,1)),
  35. v_center2d),
  36. 0.0, 1.0 );
  37. int nMinBucket=findBucket( fAlpha );
  38. float fLerp =
  39. (fAlpha-texture1D(t_stopArray1d, nMinBucket).s) /
  40. (texture1D(t_stopArray1d, nMinBucket+1).s -
  41. texture1D(t_stopArray1d, nMinBucket).s);
  42. gl_FragColor = mix(texture1D(t_colorArray4d, nMinBucket),
  43. texture1D(t_colorArray4d, nMinBucket+1),
  44. fLerp);
  45. }
  46. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */