-
Notifications
You must be signed in to change notification settings - Fork 2
/
blur_test.gdshader
48 lines (41 loc) · 1.5 KB
/
blur_test.gdshader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
shader_type canvas_item;
uniform int num_pass: hint_range(1, 2) = 2;
uniform float blur_x: hint_range(0.0, 255.0) = 5.0;
//uniform float blur_y: hint_range(1.0, 255.0) = 5.0;
void fragment() {
vec4 res = vec4(0.0);
for (int j = 0; j < num_pass; j++) {
ivec2 size = textureSize(TEXTURE, 0);
for (int i = 0; i < 2; i++){
bool horizontal = i % 2 == 0;
//float strength = horizontal ? blur_x : blur_y;
float strength = blur_x;
float full_size = strength < 255.0 ? strength : 255.0;
if (full_size < 1.0){
continue;
}
float radius = (full_size - 1.0) / 2.0;
float m = ceil(radius) - 1.0;
float alpha = floor(((radius - m) * 255.0)) / 255.0;
float last_offset = 1.0 / ((1.0 / alpha) + 1.0);
float last_weight = alpha + 1.0;
//vec2 direction = horizontal ? vec2(1.0 / float(size.x), 0.0) : vec2(0.0, 1.0 / float(size.y));
vec2 direction = vec2(1.0 / float(size.x), 0.0);
vec2 uv = UV-direction * m;
// 先采样第一个像素
vec4 total = texture(TEXTURE, uv - direction) * alpha;
vec4 center = vec4(0.0);
for (float k = 0.5; k < m * 2.0; k += 2.0) {
center += texture(TEXTURE, uv + direction * k);
}
total += center * 2.0;
// 最后一个像素对的采样,使用偏移
vec2 last_location = uv + direction * (m * 2.0 + last_offset);
total += texture(TEXTURE, last_location) * last_weight;
// 归一化模糊结果
vec4 result = total / full_size;
// 将结果输出,模拟固定点精度
COLOR = floor(result * 255.0) / 255.0;
}
}
}