Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

149 righe
4.2 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. #if defined GL_ES
  13. precision highp float;
  14. #endif
  15. uniform sampler2D u_Texture;
  16. varying 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. gl_FragColor = vec4(mix(p.x, 1.0, lum), lum, lum, 1.0);
  61. }
  62. [vert.hlsl]
  63. void main(float2 in_Position : POSITION,
  64. out float4 out_Position : POSITION,
  65. out float4 pass_Position : TEXCOORD0)
  66. {
  67. pass_Position = float4(0.5 * in_Position + 0.5, 0.0, 1.0);
  68. out_Position = float4(in_Position, 0.5, 1.0);
  69. }
  70. [frag.hlsl]
  71. float segdist(float2 p1, float2 p2, float2 a)
  72. {
  73. float d = max(1e-10, dot(p2 - p1, p2 - p1));
  74. float t = clamp(dot(a - p1, p2 - p1) / d, 0.0, 1.0);
  75. return distance(a, lerp(p1, p2, t));
  76. }
  77. void main(in float4 pass_Position : TEXCOORD0,
  78. uniform sampler2D u_Texture,
  79. out float4 out_FragColor : COLOR)
  80. {
  81. float width = 800.0;
  82. float height = 600.0;
  83. float texture_width = 256.0;
  84. float line_width = 1.2;
  85. float dot_size = 1.0;
  86. float4 delta = float4(1.0 / texture_width, 0.0,
  87. 2.0 / texture_width, 0.0);
  88. float2 p = pass_Position.xy;
  89. float2 tc = float2(floor(p.x * texture_width) / texture_width, p.y);
  90. float t = p.x * texture_width - floor(p.x * texture_width);
  91. float4 c;
  92. c[0] = tex2D(u_Texture, tc - delta.xy).x;
  93. c[1] = tex2D(u_Texture, tc).x;
  94. c[2] = tex2D(u_Texture, tc + delta.xy).x;
  95. c[3] = tex2D(u_Texture, tc + delta.zw).x;
  96. /* Find the 4 closest points in screen space */
  97. float2 p0 = float2((tc.x - delta.x) * width, c[0] * height);
  98. float2 p1 = float2((tc.x ) * width, c[1] * height);
  99. float2 p2 = float2((tc.x + delta.x) * width, c[2] * height);
  100. float2 p3 = float2((tc.x + delta.z) * width, c[3] * height);
  101. float2 a = float2(p.x * width, p.y * height);
  102. /* Compute distance to segments */
  103. float d = segdist(p0, p1, a);
  104. d = min(d, segdist(p1, p2, a));
  105. d = min(d, segdist(p2, p3, a));
  106. /* Compute distance to dots */
  107. d = min(d, length(a - p0) - dot_size);
  108. d = min(d, length(a - p1) - dot_size);
  109. d = min(d, length(a - p2) - dot_size);
  110. d = min(d, length(a - p3) - dot_size);
  111. /* Add line width */
  112. float lum = clamp(line_width - d, 0.0, 1.0);
  113. /* Compensate for sRGB */
  114. lum = pow(1.0 - lum, 1.0 / 2.4);
  115. /* Choose some funny colours */
  116. out_FragColor = float4(lerp(p.x, 1.0, lum), lum, lum, 1.0);
  117. }