text2d.vertex.fx 1.4 KB

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