glitterVertexShader.glsl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. attribute vec3 a_position;
  12. attribute vec3 a_normal;
  13. uniform mat4 u_projectionMatrix;
  14. uniform mat4 u_modelViewMatrix;
  15. uniform mat4 u_sceneTransformMatrix;
  16. uniform mat4 u_primitiveTransformMatrix;
  17. uniform mat4 u_operationsTransformMatrix;
  18. varying vec2 v_texturePosition;
  19. varying vec3 v_normal;
  20. attribute vec3 center;
  21. uniform float time;
  22. uniform ivec2 numTiles;
  23. uniform sampler2D permTexture;
  24. varying float angle;
  25. #if __VERSION__ < 140
  26. mat4 inverse(mat4 m)
  27. {
  28. float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];
  29. float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];
  30. float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];
  31. float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];
  32. float b00 = a00 * a11 - a01 * a10;
  33. float b01 = a00 * a12 - a02 * a10;
  34. float b02 = a00 * a13 - a03 * a10;
  35. float b03 = a01 * a12 - a02 * a11;
  36. float b04 = a01 * a13 - a03 * a11;
  37. float b05 = a02 * a13 - a03 * a12;
  38. float b06 = a20 * a31 - a21 * a30;
  39. float b07 = a20 * a32 - a22 * a30;
  40. float b08 = a20 * a33 - a23 * a30;
  41. float b09 = a21 * a32 - a22 * a31;
  42. float b10 = a21 * a33 - a23 * a31;
  43. float b11 = a22 * a33 - a23 * a32;
  44. float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  45. return mat4(
  46. a11 * b11 - a12 * b10 + a13 * b09,
  47. a02 * b10 - a01 * b11 - a03 * b09,
  48. a31 * b05 - a32 * b04 + a33 * b03,
  49. a22 * b04 - a21 * b05 - a23 * b03,
  50. a12 * b08 - a10 * b11 - a13 * b07,
  51. a00 * b11 - a02 * b08 + a03 * b07,
  52. a32 * b02 - a30 * b05 - a33 * b01,
  53. a20 * b05 - a22 * b02 + a23 * b01,
  54. a10 * b10 - a11 * b08 + a13 * b06,
  55. a01 * b08 - a00 * b10 - a03 * b06,
  56. a30 * b04 - a31 * b02 + a33 * b00,
  57. a21 * b02 - a20 * b04 - a23 * b00,
  58. a11 * b07 - a10 * b09 - a12 * b06,
  59. a00 * b09 - a01 * b07 + a02 * b06,
  60. a31 * b01 - a30 * b03 - a32 * b00,
  61. a20 * b03 - a21 * b01 + a22 * b00) / det;
  62. }
  63. #endif
  64. float snoise(vec2 p)
  65. {
  66. return texture2D(permTexture, p).r;
  67. }
  68. mat4 translationMatrix(vec3 axis)
  69. {
  70. return mat4(1.0, 0.0, 0.0, 0.0,
  71. 0.0, 1.0, 0.0, 0.0,
  72. 0.0, 0.0, 1.0, 0.0,
  73. axis.x, axis.y, axis.z, 1.0);
  74. }
  75. mat4 rotationMatrix(vec3 axis, float angle)
  76. {
  77. axis = normalize(axis);
  78. float s = sin(angle);
  79. float c = cos(angle);
  80. float oc = 1.0 - c;
  81. return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
  82. oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
  83. oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
  84. 0.0, 0.0, 0.0, 1.0);
  85. }
  86. void main( void )
  87. {
  88. vec2 pos = (center.xy + 1.0) / 2.0;
  89. // 0..1 pseudo-random value used to randomize the tile’s start time.
  90. float fuzz = snoise(pos);
  91. float startTime = pos.x * 0.5 + fuzz * 0.25;
  92. float endTime = startTime + 0.25;
  93. // Scale the transition time to minimize the time a tile will stay black.
  94. float transitionTime = clamp((time - startTime) / (endTime - startTime), 0.0, 1.0);
  95. if (transitionTime < 0.5)
  96. transitionTime = transitionTime / 2.0;
  97. else
  98. transitionTime = (transitionTime / 2.0) + 0.5;
  99. angle = transitionTime * M_PI * 2.0;
  100. mat4 modelViewMatrix = u_modelViewMatrix * u_operationsTransformMatrix * u_sceneTransformMatrix * u_primitiveTransformMatrix;
  101. mat4 transformMatrix = translationMatrix(center) * rotationMatrix(vec3(0.0, 1.0, 0.0), angle) * translationMatrix(-center);
  102. mat3 normalMatrix = mat3(transpose(inverse(transformMatrix)));
  103. gl_Position = u_projectionMatrix * modelViewMatrix * transformMatrix * vec4(a_position, 1.0);
  104. v_texturePosition = vec2((a_position.x + 1.0) / 2.0, (1.0 - a_position.y) / 2.0);
  105. v_normal = normalize(normalMatrix * a_normal);
  106. }
  107. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */