Skip to content

Commit

Permalink
fix: correct blur shader not working anymore in Godot 4.X
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanLovato committed Sep 6, 2024
1 parent 423a6ba commit e1fe8d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
48 changes: 31 additions & 17 deletions DemoPage/Assets/BlurShader.gdshader
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions DemoPage/DemoPage.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e1fe8d3

Please sign in to comment.