您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

81 行
1.8 KiB

  1. [vert.glsl]
  2. #version 120
  3. attribute vec2 in_Position;
  4. varying 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 120
  12. uniform sampler2D u_Texture;
  13. varying vec4 pass_Position;
  14. float rand(in vec2 p, in float v)
  15. {
  16. return fract(v * sin(dot(p, vec2(1298.9837, 7823.33145))));
  17. }
  18. float point2segment(vec2 p1, vec2 p2, vec2 a)
  19. {
  20. float l2 = dot(p2 - p1, p2 - p1);
  21. if (l2 == 0.0)
  22. return distance(a, p1);
  23. float t = dot(a - p1, p2 - p1) / l2;
  24. if (t < 0.0)
  25. return distance(a, p1);
  26. else if (t > 1.0)
  27. return distance(a, p2);
  28. vec2 proj = p1 + t * (p2 - p1);
  29. return distance(a, proj);
  30. }
  31. void main(void)
  32. {
  33. float width = 800.0;
  34. float height = 600.0;
  35. float line_width = 2.0;
  36. vec2 t = pass_Position.xy;
  37. vec2 tc1 = floor(t * 128.0) / 128.0;
  38. vec2 tc2 = tc1 + vec2(1.0, 1.0) / 128.0;
  39. vec2 tc0 = tc1 - vec2(1.0, 1.0) / 128.0;
  40. vec2 tc3 = tc2 + vec2(1.0, 1.0) / 128.0;
  41. float c0 = texture2D(u_Texture, tc0).x;
  42. float c1 = texture2D(u_Texture, tc1).x;
  43. float c2 = texture2D(u_Texture, tc2).x;
  44. float c3 = texture2D(u_Texture, tc3).x;
  45. /* Artificially compress in Y */
  46. c0 *= 0.3;
  47. c1 *= 0.3;
  48. c2 *= 0.3;
  49. c3 *= 0.3;
  50. vec2 p0 = vec2(tc0.x * width, c0 * height);
  51. vec2 p1 = vec2(tc1.x * width, c1 * height);
  52. vec2 p2 = vec2(tc2.x * width, c2 * height);
  53. vec2 p3 = vec2(tc3.x * width, c3 * height);
  54. vec2 a = vec2(t.x * width, t.y * height);
  55. float d0 = point2segment(p0, p1, a);
  56. float d1 = point2segment(p1, p2, a);
  57. float d2 = point2segment(p2, p3, a);
  58. float d = clamp(line_width - min(min(d0, d1), d2), 0.0, 1.0);
  59. gl_FragColor = vec4(t.y, d, d * 0.3, 1.0);
  60. }