Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

04_texture.lolfx 2.1 KiB

před 11 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. [vert.glsl]
  2. #version 130
  3. in vec2 in_position;
  4. out vec4 pass_position;
  5. void main(void)
  6. {
  7. pass_position = vec4(0.5 * in_position + 0.5, 0.0, 1.0);
  8. gl_Position = vec4(in_position, 0.5, 1.0);
  9. }
  10. [frag.glsl]
  11. #version 130
  12. #if defined GL_ES
  13. precision highp float;
  14. #endif
  15. uniform sampler2D u_texture;
  16. in vec4 pass_position;
  17. float segdist(vec2 p1, vec2 p2, vec2 a)
  18. {
  19. float d = max(1e-10, dot(p2 - p1, p2 - p1));
  20. float t = clamp(dot(a - p1, p2 - p1) / d, 0.0, 1.0);
  21. return distance(a, mix(p1, p2, t));
  22. }
  23. void main(void)
  24. {
  25. float width = 800.0;
  26. float height = 600.0;
  27. float texture_width = 256.0;
  28. float line_width = 1.2;
  29. float dot_size = 1.0;
  30. vec4 delta = vec4(1.0 / texture_width, 0.0,
  31. 2.0 / texture_width, 0.0);
  32. vec2 p = pass_position.xy;
  33. vec2 tc = vec2(floor(p.x * texture_width) / texture_width, p.y);
  34. float t = p.x * texture_width - floor(p.x * texture_width);
  35. vec4 c;
  36. c[0] = texture2D(u_texture, tc - delta.xy).x;
  37. c[1] = texture2D(u_texture, tc).x;
  38. c[2] = texture2D(u_texture, tc + delta.xy).x;
  39. c[3] = texture2D(u_texture, tc + delta.zw).x;
  40. /* Find the 4 closest points in screen space */
  41. vec2 p0 = vec2((tc.x - delta.x) * width, c[0] * height);
  42. vec2 p1 = vec2((tc.x ) * width, c[1] * height);
  43. vec2 p2 = vec2((tc.x + delta.x) * width, c[2] * height);
  44. vec2 p3 = vec2((tc.x + delta.z) * width, c[3] * height);
  45. vec2 a = vec2(p.x * width, p.y * height);
  46. /* Compute distance to segments */
  47. float d = segdist(p0, p1, a);
  48. d = min(d, segdist(p1, p2, a));
  49. d = min(d, segdist(p2, p3, a));
  50. /* Compute distance to dots */
  51. d = min(d, length(a - p0) - dot_size);
  52. d = min(d, length(a - p1) - dot_size);
  53. d = min(d, length(a - p2) - dot_size);
  54. d = min(d, length(a - p3) - dot_size);
  55. /* Add line width */
  56. float lum = clamp(line_width - d, 0.0, 1.0);
  57. /* Compensate for sRGB */
  58. lum = pow(1.0 - lum, 1.0 / 2.4);
  59. /* Choose some funny colours */
  60. vec4 col = vec4(mix(p.x, 1.0, lum), lum, lum, 1.0);
  61. gl_FragColor = col;
  62. }