text2d.vertex.fx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // Attributes
  8. attribute float index;
  9. att vec2 zBias;
  10. att vec4 transformX;
  11. att vec4 transformY;
  12. att vec3 renderingInfo;
  13. att float opacity;
  14. att vec2 topLeftUV;
  15. att vec2 sizeUV;
  16. att vec2 textureSize;
  17. att vec4 color;
  18. att float superSampleFactor;
  19. // Output
  20. varying vec2 vUV;
  21. varying vec4 vColor;
  22. void main(void) {
  23. vec2 pos2;
  24. // Bottom/Left
  25. if (index == 0.0) {
  26. pos2 = vec2(0.0, 0.0);
  27. vUV = vec2(topLeftUV.x, topLeftUV.y + sizeUV.y);
  28. }
  29. // Top/Left
  30. else if (index == 1.0) {
  31. pos2 = vec2(0.0, 1.0);
  32. vUV = vec2(topLeftUV.x, topLeftUV.y);
  33. }
  34. // Top/Right
  35. else if (index == 2.0) {
  36. pos2 = vec2(1.0, 1.0);
  37. vUV = vec2(topLeftUV.x + sizeUV.x, topLeftUV.y);
  38. }
  39. // Bottom/Right
  40. else if (index == 3.0) {
  41. pos2 = vec2(1.0, 0.0);
  42. vUV = vec2(topLeftUV.x + sizeUV.x, topLeftUV.y + sizeUV.y);
  43. }
  44. // Align texture coordinate to texel to enhance rendering quality
  45. vUV = (floor(vUV*textureSize) + vec2(0.0, 0.0)) / textureSize;
  46. vColor = color;
  47. vColor.a *= opacity;
  48. vec4 pos;
  49. pos.xy = floor(pos2.xy * superSampleFactor * sizeUV * textureSize); // Align on target pixel to avoid bad interpolation
  50. pos.z = 1.0;
  51. pos.w = 1.0;
  52. float x = dot(pos, transformX);
  53. float y = dot(pos, transformY);
  54. if (renderingInfo.z == 1.0) {
  55. float rw = renderingInfo.x;
  56. float rh = renderingInfo.y;
  57. float irw = 2.0 / rw;
  58. float irh = 2.0 / rh;
  59. x = floor((x / irw) + 0.5) * irw;
  60. y = floor((y / irh) + 0.5) * irh;
  61. }
  62. gl_Position = vec4(x, y, zBias.x, 1);
  63. }