rect2d.vertex.fx 4.3 KB

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