1 | [vert.glsl] |
---|
2 | |
---|
3 | #version 120 |
---|
4 | |
---|
5 | attribute vec2 in_Position; |
---|
6 | |
---|
7 | varying vec4 pass_Position; |
---|
8 | |
---|
9 | void main(void) |
---|
10 | { |
---|
11 | pass_Position = vec4(0.5 * in_Position + 0.5, 0.0, 1.0); |
---|
12 | gl_Position = vec4(in_Position, 0.5, 1.0); |
---|
13 | } |
---|
14 | |
---|
15 | [frag.glsl] |
---|
16 | |
---|
17 | #version 120 |
---|
18 | |
---|
19 | uniform sampler2D u_Texture; |
---|
20 | |
---|
21 | varying vec4 pass_Position; |
---|
22 | |
---|
23 | float segdist(vec2 p1, vec2 p2, vec2 a) |
---|
24 | { |
---|
25 | float d = max(1e-10, dot(p2 - p1, p2 - p1)); |
---|
26 | float t = clamp(dot(a - p1, p2 - p1) / d, 0.0, 1.0); |
---|
27 | return distance(a, mix(p1, p2, t)); |
---|
28 | } |
---|
29 | |
---|
30 | void main(void) |
---|
31 | { |
---|
32 | float width = 800.0; |
---|
33 | float height = 600.0; |
---|
34 | float texture_width = 256.0; |
---|
35 | float line_width = 1.2; |
---|
36 | float dot_size = 1.0; |
---|
37 | vec4 delta = vec4(1.0 / texture_width, 0.0, |
---|
38 | 2.0 / texture_width, 0.0); |
---|
39 | |
---|
40 | vec2 p = pass_Position.xy; |
---|
41 | vec2 tc = vec2(floor(p.x * texture_width) / texture_width, p.y); |
---|
42 | float t = p.x * texture_width - floor(p.x * texture_width); |
---|
43 | vec4 c; |
---|
44 | c[0] = texture2D(u_Texture, tc - delta.xy).x; |
---|
45 | c[1] = texture2D(u_Texture, tc).x; |
---|
46 | c[2] = texture2D(u_Texture, tc + delta.xy).x; |
---|
47 | c[3] = texture2D(u_Texture, tc + delta.zw).x; |
---|
48 | |
---|
49 | /* Find the 4 closest points in screen space */ |
---|
50 | vec2 p0 = vec2((tc.x - delta.x) * width, c[0] * height); |
---|
51 | vec2 p1 = vec2((tc.x ) * width, c[1] * height); |
---|
52 | vec2 p2 = vec2((tc.x + delta.x) * width, c[2] * height); |
---|
53 | vec2 p3 = vec2((tc.x + delta.z) * width, c[3] * height); |
---|
54 | vec2 a = vec2(p.x * width, p.y * height); |
---|
55 | |
---|
56 | /* Compute distance to segments */ |
---|
57 | float d = segdist(p0, p1, a); |
---|
58 | d = min(d, segdist(p1, p2, a)); |
---|
59 | d = min(d, segdist(p2, p3, a)); |
---|
60 | |
---|
61 | /* Compute distance to dots */ |
---|
62 | d = min(d, length(a - p0) - dot_size); |
---|
63 | d = min(d, length(a - p1) - dot_size); |
---|
64 | d = min(d, length(a - p2) - dot_size); |
---|
65 | d = min(d, length(a - p3) - dot_size); |
---|
66 | |
---|
67 | /* Add line width */ |
---|
68 | float lum = clamp(line_width - d, 0.0, 1.0); |
---|
69 | |
---|
70 | /* Compensate for sRGB */ |
---|
71 | lum = pow(1.0 - lum, 1.0 / 2.4); |
---|
72 | |
---|
73 | /* Choose some funny colours */ |
---|
74 | gl_FragColor = vec4(mix(p.x, 1.0, lum), lum, lum, 1.0); |
---|
75 | } |
---|
76 | |
---|
77 | [vert.hlsl] |
---|
78 | |
---|
79 | void main(float2 in_Position : POSITION, |
---|
80 | out float4 out_Position : POSITION, |
---|
81 | out float4 pass_Position : TEXCOORD0) |
---|
82 | { |
---|
83 | pass_Position = float4(0.5 * in_Position + 0.5, 0.0, 1.0); |
---|
84 | out_Position = float4(in_Position, 0.5, 1.0); |
---|
85 | } |
---|
86 | |
---|
87 | [frag.hlsl] |
---|
88 | |
---|
89 | float segdist(float2 p1, float2 p2, float2 a) |
---|
90 | { |
---|
91 | float d = max(1e-10, dot(p2 - p1, p2 - p1)); |
---|
92 | float t = clamp(dot(a - p1, p2 - p1) / d, 0.0, 1.0); |
---|
93 | return distance(a, lerp(p1, p2, t)); |
---|
94 | } |
---|
95 | |
---|
96 | void main(in float4 pass_Position : TEXCOORD0, |
---|
97 | uniform sampler2D u_Texture, |
---|
98 | out float4 out_FragColor : COLOR) |
---|
99 | { |
---|
100 | float width = 800.0; |
---|
101 | float height = 600.0; |
---|
102 | float texture_width = 256.0; |
---|
103 | float line_width = 1.2; |
---|
104 | float dot_size = 1.0; |
---|
105 | float4 delta = float4(1.0 / texture_width, 0.0, |
---|
106 | 2.0 / texture_width, 0.0); |
---|
107 | |
---|
108 | float2 p = pass_Position.xy; |
---|
109 | float2 tc = float2(floor(p.x * texture_width) / texture_width, p.y); |
---|
110 | float t = p.x * texture_width - floor(p.x * texture_width); |
---|
111 | float4 c; |
---|
112 | c[0] = tex2D(u_Texture, tc - delta.xy).x; |
---|
113 | c[1] = tex2D(u_Texture, tc).x; |
---|
114 | c[2] = tex2D(u_Texture, tc + delta.xy).x; |
---|
115 | c[3] = tex2D(u_Texture, tc + delta.zw).x; |
---|
116 | |
---|
117 | /* Find the 4 closest points in screen space */ |
---|
118 | float2 p0 = float2((tc.x - delta.x) * width, c[0] * height); |
---|
119 | float2 p1 = float2((tc.x ) * width, c[1] * height); |
---|
120 | float2 p2 = float2((tc.x + delta.x) * width, c[2] * height); |
---|
121 | float2 p3 = float2((tc.x + delta.z) * width, c[3] * height); |
---|
122 | float2 a = float2(p.x * width, p.y * height); |
---|
123 | |
---|
124 | /* Compute distance to segments */ |
---|
125 | float d = segdist(p0, p1, a); |
---|
126 | d = min(d, segdist(p1, p2, a)); |
---|
127 | d = min(d, segdist(p2, p3, a)); |
---|
128 | |
---|
129 | /* Compute distance to dots */ |
---|
130 | d = min(d, length(a - p0) - dot_size); |
---|
131 | d = min(d, length(a - p1) - dot_size); |
---|
132 | d = min(d, length(a - p2) - dot_size); |
---|
133 | d = min(d, length(a - p3) - dot_size); |
---|
134 | |
---|
135 | /* Add line width */ |
---|
136 | float lum = clamp(line_width - d, 0.0, 1.0); |
---|
137 | |
---|
138 | /* Compensate for sRGB */ |
---|
139 | lum = pow(1.0 - lum, 1.0 / 2.4); |
---|
140 | |
---|
141 | /* Choose some funny colours */ |
---|
142 | out_FragColor = float4(lerp(p.x, 1.0, lum), lum, lum, 1.0); |
---|
143 | } |
---|
144 | |
---|