From d4743183d40c6044ce6fa39e2a52919a32912cda Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 19 May 2020 14:23:28 +0200 Subject: Draft first working version of `iced_glow` :tada: --- glow/src/shader/quad.frag | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 glow/src/shader/quad.frag (limited to 'glow/src/shader/quad.frag') diff --git a/glow/src/shader/quad.frag b/glow/src/shader/quad.frag new file mode 100644 index 00000000..d9e74664 --- /dev/null +++ b/glow/src/shader/quad.frag @@ -0,0 +1,67 @@ +#version 450 + +layout(origin_upper_left) in vec4 gl_FragCoord; +layout(location = 0) in vec4 v_Color; +layout(location = 1) in vec4 v_BorderColor; +layout(location = 2) in vec2 v_Pos; +layout(location = 3) in vec2 v_Scale; +layout(location = 4) in float v_BorderRadius; +layout(location = 5) in float v_BorderWidth; + +layout(location = 0) out vec4 o_Color; + +float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius) +{ + // TODO: Try SDF approach: https://www.shadertoy.com/view/wd3XRN + vec2 inner_size = size - vec2(radius, radius) * 2.0; + vec2 top_left = position + vec2(radius, radius); + vec2 bottom_right = top_left + inner_size; + + vec2 top_left_distance = top_left - frag_coord; + vec2 bottom_right_distance = frag_coord - bottom_right; + + vec2 distance = vec2( + max(max(top_left_distance.x, bottom_right_distance.x), 0), + max(max(top_left_distance.y, bottom_right_distance.y), 0) + ); + + return sqrt(distance.x * distance.x + distance.y * distance.y); +} + +void main() { + vec4 mixed_color; + + // TODO: Remove branching (?) + if(v_BorderWidth > 0) { + float internal_border = max(v_BorderRadius - v_BorderWidth, 0); + + float internal_distance = distance( + gl_FragCoord.xy, + v_Pos + vec2(v_BorderWidth), + v_Scale - vec2(v_BorderWidth * 2.0), + internal_border + ); + + float border_mix = smoothstep( + max(internal_border - 0.5, 0.0), + internal_border + 0.5, + internal_distance + ); + + mixed_color = mix(v_Color, v_BorderColor, border_mix); + } else { + mixed_color = v_Color; + } + + float d = distance( + gl_FragCoord.xy, + v_Pos, + v_Scale, + v_BorderRadius + ); + + float radius_alpha = + 1.0 - smoothstep(max(v_BorderRadius - 0.5, 0), v_BorderRadius + 0.5, d); + + o_Color = vec4(mixed_color.xyz, mixed_color.w * radius_alpha); +} -- cgit From d77492c0c37dec1207049b340a318e263cb96b82 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 May 2020 01:01:47 +0200 Subject: Avoid relying `origin_upper_left` It seems to cause considerable glitches when resizing. --- glow/src/shader/quad.frag | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'glow/src/shader/quad.frag') diff --git a/glow/src/shader/quad.frag b/glow/src/shader/quad.frag index d9e74664..17e7216f 100644 --- a/glow/src/shader/quad.frag +++ b/glow/src/shader/quad.frag @@ -1,6 +1,7 @@ #version 450 -layout(origin_upper_left) in vec4 gl_FragCoord; +layout(location = 2) uniform float u_Screen_Height; + layout(location = 0) in vec4 v_Color; layout(location = 1) in vec4 v_BorderColor; layout(location = 2) in vec2 v_Pos; @@ -31,12 +32,14 @@ float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius) void main() { vec4 mixed_color; + vec2 fragCoord = vec2(gl_FragCoord.x, u_Screen_Height - gl_FragCoord.y); + // TODO: Remove branching (?) if(v_BorderWidth > 0) { float internal_border = max(v_BorderRadius - v_BorderWidth, 0); float internal_distance = distance( - gl_FragCoord.xy, + fragCoord, v_Pos + vec2(v_BorderWidth), v_Scale - vec2(v_BorderWidth * 2.0), internal_border @@ -54,7 +57,7 @@ void main() { } float d = distance( - gl_FragCoord.xy, + fragCoord, v_Pos, v_Scale, v_BorderRadius -- cgit From 6f71a8e3d5e47ab05653315b0d44b35af6a20338 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 May 2020 05:52:11 +0200 Subject: Use `get_uniform_location` for wider compatibility --- glow/src/shader/quad.frag | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'glow/src/shader/quad.frag') diff --git a/glow/src/shader/quad.frag b/glow/src/shader/quad.frag index 17e7216f..cea36bdc 100644 --- a/glow/src/shader/quad.frag +++ b/glow/src/shader/quad.frag @@ -1,15 +1,15 @@ -#version 450 +#version 330 -layout(location = 2) uniform float u_Screen_Height; +uniform float u_ScreenHeight; -layout(location = 0) in vec4 v_Color; -layout(location = 1) in vec4 v_BorderColor; -layout(location = 2) in vec2 v_Pos; -layout(location = 3) in vec2 v_Scale; -layout(location = 4) in float v_BorderRadius; -layout(location = 5) in float v_BorderWidth; +in vec4 v_Color; +in vec4 v_BorderColor; +in vec2 v_Pos; +in vec2 v_Scale; +in float v_BorderRadius; +in float v_BorderWidth; -layout(location = 0) out vec4 o_Color; +out vec4 o_Color; float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius) { @@ -22,8 +22,8 @@ float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius) vec2 bottom_right_distance = frag_coord - bottom_right; vec2 distance = vec2( - max(max(top_left_distance.x, bottom_right_distance.x), 0), - max(max(top_left_distance.y, bottom_right_distance.y), 0) + max(max(top_left_distance.x, bottom_right_distance.x), 0.0), + max(max(top_left_distance.y, bottom_right_distance.y), 0.0) ); return sqrt(distance.x * distance.x + distance.y * distance.y); @@ -32,11 +32,11 @@ float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius) void main() { vec4 mixed_color; - vec2 fragCoord = vec2(gl_FragCoord.x, u_Screen_Height - gl_FragCoord.y); + vec2 fragCoord = vec2(gl_FragCoord.x, u_ScreenHeight - gl_FragCoord.y); // TODO: Remove branching (?) if(v_BorderWidth > 0) { - float internal_border = max(v_BorderRadius - v_BorderWidth, 0); + float internal_border = max(v_BorderRadius - v_BorderWidth, 0.0); float internal_distance = distance( fragCoord, @@ -64,7 +64,7 @@ void main() { ); float radius_alpha = - 1.0 - smoothstep(max(v_BorderRadius - 0.5, 0), v_BorderRadius + 0.5, d); + 1.0 - smoothstep(max(v_BorderRadius - 0.5, 0.0), v_BorderRadius + 0.5, d); o_Color = vec4(mixed_color.xyz, mixed_color.w * radius_alpha); } -- cgit