diff --git a/examples/blur.gdshader b/examples/blur.gdshader new file mode 100644 index 0000000..8d08069 --- /dev/null +++ b/examples/blur.gdshader @@ -0,0 +1,8 @@ +shader_type canvas_item; + +//!load ./images/swamp.jpg +#include "res://shaderlib/blur.gdshaderinc" + +void fragment() { + COLOR = gaussian_blur(TEXTURE, UV, 48, 24.0); +} diff --git a/shaderlib/blur.gdshaderinc b/shaderlib/blur.gdshaderinc new file mode 100644 index 0000000..2ee4380 --- /dev/null +++ b/shaderlib/blur.gdshaderinc @@ -0,0 +1,30 @@ + +// gaussian_blur adapted from https://godotshaders.com/shader/customizable-gausian-blur/ +// original code by https://godotshaders.com/author/djbob-gaming-yt/ +// maximum radius is 64 +vec4 gaussian_blur(sampler2D texture, vec2 uv, int radius, float sigma) { + vec2 resolution = 1.0 / vec2(textureSize(texture, 0)); + // calculate kernel + float kernel[64]; + float sum = 0.0; + for (int i = 0; i <= radius; i++) { + kernel[i] = exp(-0.5 * float(i * i) / (sigma * sigma)); + sum += i == 0 ? kernel[i] : 2.0 * kernel[i]; + } + for (int i = 0; i <= radius; i++) { + kernel[i] /= sum; + } + // + vec4 final_color = vec4(0.0); + float total_weight = 0.0; + for (int x = -radius; x <= radius; x++) { + for (int y = -radius; y <= radius; y++) { + float weight = kernel[abs(x)] * kernel[abs(y)]; + vec2 offset = vec2(float(x), float(y)) * resolution; + final_color += texture(texture, uv + offset) * weight; + total_weight += weight; + } + } + final_color /= total_weight; + return final_color; +} diff --git a/src/Editor.gd b/src/Editor.gd index 6352620..39a2441 100644 --- a/src/Editor.gd +++ b/src/Editor.gd @@ -133,14 +133,16 @@ var shaderlib_regex = { "transform": RegEx.create_from_string(r'\s*\#include\s+\"res\:\/\/shaderlib\/transform\.gdshaderinc\"'), "transparency": RegEx.create_from_string(r'\s*\#include\s+\"res\:\/\/shaderlib\/transparency\.gdshaderinc\"'), "effects": RegEx.create_from_string(r'\s*\#include\s+\"res\:\/\/shaderlib\/effects\.gdshaderinc\"'), - "denoise": RegEx.create_from_string(r'\s*\#include\s+\"res\:\/\/shaderlib\/denoise\.gdshaderinc\"') + "denoise": RegEx.create_from_string(r'\s*\#include\s+\"res\:\/\/shaderlib\/denoise\.gdshaderinc\"'), + "blur": RegEx.create_from_string(r'\s*\#include\s+\"res\:\/\/shaderlib\/blur\.gdshaderinc\"'), } const shaderlib_functions = { "colorspaces": ["rgb2hsv", "hsv2rgb"], "transform": ["place_texture"], "transparency": ["alpha_blend"], "effects": ["pixelate"], - "denoise": ["smart_denoise"] + "denoise": ["smart_denoise"], + "blur": ["gaussian_blur"], } # # configure Highlighter