diff --git a/DemoPage/Assets/BlurShader.gdshader b/DemoPage/Assets/BlurShader.gdshader index f97e759..f8eca8e 100644 --- a/DemoPage/Assets/BlurShader.gdshader +++ b/DemoPage/Assets/BlurShader.gdshader @@ -1,26 +1,40 @@ shader_type canvas_item; -uniform vec2 blur_scale = vec2(1, 0); -uniform vec4 color_modulate: source_color; -uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap; - -const int SAMPLES = 8; -const float WEIGHTS[70] = {0.000008, 0.000014, 0.000022, 0.000035, 0.000054, 0.000084, 0.000127, 0.000189, 0.000279, 0.000405, 0.00058, 0.00082, 0.001141, 0.001567, 0.002121, 0.002831, 0.003726, 0.004835, 0.006186, 0.007804, 0.009708, 0.011908, 0.014402, 0.017174, 0.020194, 0.023412, 0.026764, 0.030168, 0.033529, 0.036743, 0.039703, 0.042301, 0.044438, 0.046031, 0.047013, 0.047346, 0.047013, 0.046031, 0.044438, 0.042301, 0.039703, 0.036743, 0.033529, 0.030168, 0.026764, 0.023412, 0.020194, 0.017174, 0.014402, 0.011908, 0.009708, 0.007804, 0.006186, 0.004835, 0.003726, 0.002831, 0.002121, 0.001567, 0.001141, 0.00082, 0.00058, 0.000405, 0.000279, 0.000189, 0.000127, 0.000084, 0.000054, 0.000035, 0.000022, 0.000014}; +uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest; +uniform float blur_size = 1.0; +uniform vec3 color_modulate: source_color = vec3(1.0); void fragment() { - vec2 scale = SCREEN_PIXEL_SIZE * blur_scale; + const int SAMPLES = 9; + const int SAMPLES_HALF = SAMPLES / 2; + const float TOTAL_WEIGHT = 1.627038; + const float WEIGHTS[8] = { + 0.05467, + 0.080657, + 0.106483, + 0.125794, + 0.132981, + 0.125794, + 0.106483, + 0.080657 + }; + + vec2 scale = SCREEN_PIXEL_SIZE * blur_size; + + COLOR = vec4(0.0); - float total_weight = 0.0; - vec4 color = vec4(0.0); + vec2 blur_direction_1 = TEXTURE_PIXEL_SIZE * vec2(blur_size); + vec2 blur_direction_2 = TEXTURE_PIXEL_SIZE * vec2(blur_size, -blur_size); - for (int i = -SAMPLES / 2; i < SAMPLES / 2; ++i) { - for (int j = -SAMPLES / 2; j < SAMPLES / 2; ++j) { - int weight_index = ((i + SAMPLES/2) * SAMPLES) + j; - float weight = WEIGHTS[weight_index]; - color.rgb += texture(SCREEN_TEXTURE, UV + scale * vec2(float(i), float(j))).rgb * weight; - total_weight += weight; - } + for(int i = -SAMPLES_HALF; i < SAMPLES_HALF; ++i) { + int w = i + SAMPLES_HALF; + COLOR += texture(screen_texture, SCREEN_UV + blur_direction_1 * float(i) * scale) * WEIGHTS[w]; + } + for(int i = -SAMPLES_HALF; i < SAMPLES_HALF; ++i) { + int w = i + SAMPLES_HALF; + COLOR += texture(screen_texture, SCREEN_UV + blur_direction_2 * float(i) * scale) * WEIGHTS[w]; } - COLOR.rgb = (color.rgb / total_weight) * color_modulate.rgb; + COLOR /= TOTAL_WEIGHT; + COLOR.rgb *= color_modulate; } \ No newline at end of file diff --git a/DemoPage/DemoPage.tscn b/DemoPage/DemoPage.tscn index 5dac19c..d1e1b3b 100644 --- a/DemoPage/DemoPage.tscn +++ b/DemoPage/DemoPage.tscn @@ -30,8 +30,8 @@ [sub_resource type="ShaderMaterial" id="ShaderMaterial_oq2x4"] shader = ExtResource("2_07raf") -shader_parameter/blur_scale = Vector2(4, 4) -shader_parameter/color_modulate = Color(0.780392, 0.717647, 0.968627, 1) +shader_parameter/blur_size = 2.0 +shader_parameter/color_modulate = Color(0.522997, 0.612903, 0.846875, 1) [sub_resource type="LabelSettings" id="LabelSettings_swl1q"] font_size = 20