sprite2d.vertex.fx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 topLeftUV;
  10. att vec2 sizeUV;
  11. att vec2 scaleFactor;
  12. att vec2 textureSize;
  13. // x: frame, y: invertY, z: alignToPixel
  14. att vec3 properties;
  15. att vec2 zBias;
  16. att vec4 transformX;
  17. att vec4 transformY;
  18. att float opacity;
  19. // Uniforms
  20. // Output
  21. varying vec2 vUV;
  22. varying float vOpacity;
  23. void main(void) {
  24. vec2 pos2;
  25. //vec2 off = vec2(1.0 / textureSize.x, 1.0 / textureSize.y);
  26. vec2 off = vec2(0.0, 0.0);
  27. vec2 sfSizeUV = sizeUV * scaleFactor;
  28. float frame = properties.x;
  29. float invertY = properties.y;
  30. float alignToPixel = properties.z;
  31. // Left/Top
  32. if (index == 0.0) {
  33. pos2 = vec2(0.0, 0.0);
  34. vUV = vec2(topLeftUV.x + (frame*sfSizeUV.x) + off.x, topLeftUV.y - off.y);
  35. }
  36. // Left/Bottom
  37. else if (index == 1.0) {
  38. pos2 = vec2(0.0, 1.0);
  39. vUV = vec2(topLeftUV.x + (frame*sfSizeUV.x) + off.x, (topLeftUV.y + sfSizeUV.y));
  40. }
  41. // Right/Bottom
  42. else if (index == 2.0) {
  43. pos2 = vec2( 1.0, 1.0);
  44. vUV = vec2(topLeftUV.x + sfSizeUV.x + (frame*sfSizeUV.x), (topLeftUV.y + sfSizeUV.y));
  45. }
  46. // Right/Top
  47. else if (index == 3.0) {
  48. pos2 = vec2( 1.0, 0.0);
  49. vUV = vec2(topLeftUV.x + sfSizeUV.x + (frame*sfSizeUV.x), topLeftUV.y - off.y);
  50. }
  51. if (invertY == 1.0) {
  52. vUV.y = 1.0 - vUV.y;
  53. }
  54. vec4 pos;
  55. if (alignToPixel == 1.0)
  56. {
  57. pos.xy = floor(pos2.xy * sizeUV * textureSize);
  58. } else {
  59. pos.xy = pos2.xy * sizeUV * textureSize;
  60. }
  61. vOpacity = opacity;
  62. pos.z = 1.0;
  63. pos.w = 1.0;
  64. gl_Position = vec4(dot(pos, transformX), dot(pos, transformY), zBias.x, 1);
  65. }