rect2d.vertex.fx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Attributes
  2. attribute float index;
  3. attribute vec2 zBias;
  4. attribute vec4 transformX;
  5. attribute vec4 transformY;
  6. attribute vec2 origin;
  7. #ifdef Border
  8. attribute float borderThickness;
  9. #endif
  10. #ifdef FillSolid
  11. attribute vec4 fillSolidColor;
  12. #endif
  13. #ifdef BorderSolid
  14. attribute vec4 borderSolidColor;
  15. #endif
  16. #ifdef FillGradient
  17. attribute vec4 fillGradientColor1;
  18. attribute vec4 fillGradientColor2;
  19. attribute vec4 fillGradientTY;
  20. #endif
  21. #ifdef BorderGradient
  22. attribute vec4 borderGradientColor1;
  23. attribute vec4 borderGradientColor2;
  24. attribute vec4 borderGradientTY;
  25. #endif
  26. // xyzw are: width, height, roundRadius (0.0 for simple rectangle with four vertices)
  27. attribute vec3 properties;
  28. // First index is the center, then there's four sections of 16 subdivisions
  29. #define rsub0 17.0
  30. #define rsub1 33.0
  31. #define rsub2 49.0
  32. #define rsub3 65.0
  33. #define rsub 64.0
  34. #define TWOPI 6.28318530
  35. // Output
  36. varying vec2 vUV;
  37. varying vec4 vColor;
  38. void main(void) {
  39. vec2 pos2;
  40. // notRound case, only five vertices, 0 is center, then the 4 other for perimeter
  41. if (properties.z == 0.0) {
  42. if (index == 0.0) {
  43. pos2 = vec2(0.5, 0.5);
  44. }
  45. else if (index == 1.0) {
  46. pos2 = vec2(1.0, 1.0);
  47. }
  48. else if (index == 2.0) {
  49. pos2 = vec2(1.0, 0.0);
  50. }
  51. else if (index == 3.0) {
  52. pos2 = vec2(0.0, 0.0);
  53. }
  54. else {
  55. pos2 = vec2(0.0, 1.0);
  56. }
  57. }
  58. else
  59. {
  60. #ifdef Border
  61. float w = properties.x;
  62. float h = properties.y;
  63. float r = properties.z;
  64. float nru = r / w;
  65. float nrv = r / h;
  66. vec2 borderOffset = vec2(1.0, 1.0);
  67. float segi = index;
  68. if (index < rsub) {
  69. borderOffset = vec2(1.0-(borderThickness*2.0 / w), 1.0-(borderThickness*2.0 / h));
  70. }
  71. else {
  72. segi -= rsub;
  73. }
  74. // right/bottom
  75. if (segi < rsub0) {
  76. pos2 = vec2(1.0 - nru, nrv);
  77. }
  78. // left/bottom
  79. else if (segi < rsub1) {
  80. pos2 = vec2(nru, nrv);
  81. }
  82. // left/top
  83. else if (segi < rsub2) {
  84. pos2 = vec2(nru, 1.0 - nrv);
  85. }
  86. // right/top
  87. else {
  88. pos2 = vec2(1.0 - nru, 1.0 - nrv);
  89. }
  90. float angle = TWOPI - ((index - 1.0) * TWOPI / (rsub - 0.5));
  91. pos2.x += cos(angle) * nru;
  92. pos2.y += sin(angle) * nrv;
  93. pos2.x = ((pos2.x - 0.5) * borderOffset.x) + 0.5;
  94. pos2.y = ((pos2.y - 0.5) * borderOffset.y) + 0.5;
  95. #else
  96. if (index == 0.0) {
  97. pos2 = vec2(0.5, 0.5);
  98. }
  99. else {
  100. float w = properties.x;
  101. float h = properties.y;
  102. float r = properties.z;
  103. float nru = r / w;
  104. float nrv = r / h;
  105. // right/bottom
  106. if (index < rsub0) {
  107. pos2 = vec2(1.0 - nru, nrv);
  108. }
  109. // left/bottom
  110. else if (index < rsub1) {
  111. pos2 = vec2(nru, nrv);
  112. }
  113. // left/top
  114. else if (index < rsub2) {
  115. pos2 = vec2(nru, 1.0 - nrv);
  116. }
  117. // right/top
  118. else {
  119. pos2 = vec2(1.0 - nru, 1.0 - nrv);
  120. }
  121. float angle = TWOPI - ((index - 1.0) * TWOPI / (rsub - 0.5));
  122. pos2.x += cos(angle) * nru;
  123. pos2.y += sin(angle) * nrv;
  124. }
  125. #endif
  126. }
  127. #ifdef FillSolid
  128. vColor = fillSolidColor;
  129. #endif
  130. #ifdef BorderSolid
  131. vColor = borderSolidColor;
  132. #endif
  133. #ifdef FillGradient
  134. float v = dot(vec4(pos2.xy, 1, 1), fillGradientTY);
  135. vColor = mix(fillGradientColor2, fillGradientColor1, v); // As Y is inverted, Color2 first, then Color1
  136. #endif
  137. #ifdef BorderGradient
  138. float v = dot(vec4(pos2.xy, 1, 1), borderGradientTY);
  139. vColor = mix(borderGradientColor2, borderGradientColor1, v); // As Y is inverted, Color2 first, then Color1
  140. #endif
  141. vec4 pos;
  142. pos.xy = (pos2.xy - origin) * properties.xy;
  143. pos.z = 1.0;
  144. pos.w = 1.0;
  145. gl_Position = vec4(dot(pos, transformX), dot(pos, transformY), zBias.x, zBias.y);
  146. }