honeycombFragmentShader.glsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 150
  10. in vec2 texturePosition;
  11. in float fuzz;
  12. in vec2 v_center;
  13. in vec3 normal;
  14. in vec4 shadowCoordinate;
  15. uniform sampler2D slideTexture;
  16. uniform sampler2D colorShadowTexture;
  17. uniform sampler2D depthShadowTexture;
  18. uniform float selectedTexture;
  19. uniform float time;
  20. uniform float hexagonSize;
  21. bool isBorder(vec2 point)
  22. {
  23. return point.x < 0.02 || point.x > 0.98 || point.y < 0.02 || point.y > 0.98;
  24. }
  25. void main()
  26. {
  27. const vec2 samplingPoints[9] = vec2[](
  28. vec2(0, 0),
  29. vec2(-1, -1),
  30. vec2(-1, 0),
  31. vec2(-1, 1),
  32. vec2(0, 1),
  33. vec2(1, 1),
  34. vec2(1, 0),
  35. vec2(1, -1),
  36. vec2(0, -1)
  37. );
  38. vec4 fragment = vec4(texture(slideTexture, texturePosition).rgb, 1.0);
  39. vec3 lightVector = vec3(0.0, 0.0, 1.0);
  40. float light = max(dot(lightVector, normal), 0.0);
  41. if (hexagonSize > 1.0) {
  42. // The space in-between hexagons.
  43. if (selectedTexture > 0.5)
  44. fragment.a = 1.0 - time * 8 + gl_FragCoord.x / 1024.;
  45. else
  46. fragment.a = time * 8 - 7.3 + gl_FragCoord.x / 1024.;
  47. } else {
  48. // The hexagons themselves.
  49. float startTime;
  50. float actualTime;
  51. if (selectedTexture > 0.5) {
  52. // Leaving slide.
  53. if (isBorder(v_center))
  54. // If the center is “outside” of the canvas, clear it first.
  55. startTime = 0.15;
  56. else
  57. startTime = 0.15 + fuzz * 0.4;
  58. float endTime = startTime + 0.05;
  59. actualTime = 1.0 - clamp((time - startTime) / (endTime - startTime), 0, 1);
  60. } else {
  61. // Entering slide.
  62. if (isBorder(v_center))
  63. // If the center is “outside” of the canvas, clear it first.
  64. startTime = 0.85;
  65. else
  66. startTime = 0.3 + fuzz * 0.4;
  67. float endTime = startTime + 0.05;
  68. actualTime = clamp((time - startTime) / (endTime - startTime), 0, 1);
  69. if (time < 0.8)
  70. actualTime *= time / 0.8;
  71. }
  72. if (selectedTexture > 0.5) {
  73. // Leaving texture needs to be transparent to see-through.
  74. fragment.a = actualTime;
  75. } else {
  76. // Entering one though, would look weird with transparency.
  77. fragment.rgb *= actualTime;
  78. }
  79. }
  80. // Compute the shadow.
  81. float visibility = 1.0;
  82. const float epsilon = 0.0001;
  83. if (selectedTexture < 0.5) {
  84. float depthShadow = texture(depthShadowTexture, shadowCoordinate.xy).z;
  85. float shadowRadius = (1.0 / (shadowCoordinate.z - depthShadow)) * 1000.0;
  86. // Only the entering slide.
  87. for (int i = 0; i < 9; ++i) {
  88. vec2 coordinate = shadowCoordinate.xy + samplingPoints[i] / shadowRadius;
  89. if (depthShadow < shadowCoordinate.z - epsilon) {
  90. visibility -= 0.05 * texture(colorShadowTexture, coordinate).a;
  91. }
  92. }
  93. }
  94. vec4 black = vec4(0.0, 0.0, 0.0, fragment.a);
  95. gl_FragColor = mix(black, fragment, visibility * light);
  96. }
  97. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */