diff --git a/README.md b/README.md index 5c2e0a9..4c3496d 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ shader_type canvas_item; #include "res://shaderlib/hsv.gdshaderinc" -//!load ./swamp.jpg +//!load ./examples/images/swamp.jpg void fragment() { COLOR = hsv_offset(COLOR, 0.32, 0.2, 0.0); diff --git a/examples/channel_offset.gdshader b/examples/channel_offset.gdshader index 73915ea..a1ad8b5 100644 --- a/examples/channel_offset.gdshader +++ b/examples/channel_offset.gdshader @@ -1,6 +1,6 @@ shader_type canvas_item; -//!load ./swamp.jpg +//!load ./images/swamp.jpg const vec2 offset_r = vec2(-0.002, -0.002); const vec2 offset_g = vec2(0., 0.); diff --git a/examples/color_and_pixelate.gdshader b/examples/color_and_pixelate.gdshader index f1ea38c..7c2c0ef 100644 --- a/examples/color_and_pixelate.gdshader +++ b/examples/color_and_pixelate.gdshader @@ -3,7 +3,7 @@ shader_type canvas_item; #include "res://shaderlib/hsv.gdshaderinc" #include "res://shaderlib/effects.gdshaderinc" -//!load ./swamp.jpg +//!load ./images/swamp.jpg void fragment() { COLOR = pixelate(TEXTURE, UV, 200.0); diff --git a/examples/denoise.gdshader b/examples/denoise.gdshader new file mode 100644 index 0000000..09dabb6 --- /dev/null +++ b/examples/denoise.gdshader @@ -0,0 +1,9 @@ +shader_type canvas_item; + +//!load ./images/noisy.png + +#include "res://shaderlib/denoise.gdshaderinc" + +void fragment() { + COLOR = smart_denoise(TEXTURE, UV, 12.0, 1.0, .12); +} diff --git a/examples/greyscale.gdshader b/examples/greyscale.gdshader index 921c591..00aac8b 100644 --- a/examples/greyscale.gdshader +++ b/examples/greyscale.gdshader @@ -1,6 +1,6 @@ shader_type canvas_item; -//!load ./swamp.jpg +//!load ./images/swamp.jpg void fragment() { float b = (COLOR.r + COLOR.g + COLOR.b) / 3.0; diff --git a/examples/CREDITS.md b/examples/images/CREDITS.md similarity index 100% rename from examples/CREDITS.md rename to examples/images/CREDITS.md diff --git a/examples/grass.png b/examples/images/grass.png similarity index 100% rename from examples/grass.png rename to examples/images/grass.png diff --git a/examples/grass.png.import b/examples/images/grass.png.import similarity index 72% rename from examples/grass.png.import rename to examples/images/grass.png.import index f9a52a6..327f716 100644 --- a/examples/grass.png.import +++ b/examples/images/grass.png.import @@ -3,15 +3,15 @@ importer="texture" type="CompressedTexture2D" uid="uid://c1mh1d2f3u4ju" -path="res://.godot/imported/grass.png-30af7213ed2d70b810b4ae788314a9e9.ctex" +path="res://.godot/imported/grass.png-61a458998da568ce60ccb8a0c7caaf6d.ctex" metadata={ "vram_texture": false } [deps] -source_file="res://examples/grass.png" -dest_files=["res://.godot/imported/grass.png-30af7213ed2d70b810b4ae788314a9e9.ctex"] +source_file="res://examples/images/grass.png" +dest_files=["res://.godot/imported/grass.png-61a458998da568ce60ccb8a0c7caaf6d.ctex"] [params] diff --git a/examples/images/noisy.png b/examples/images/noisy.png new file mode 100644 index 0000000..08e0664 Binary files /dev/null and b/examples/images/noisy.png differ diff --git a/examples/images/noisy.png.import b/examples/images/noisy.png.import new file mode 100644 index 0000000..e8bab73 --- /dev/null +++ b/examples/images/noisy.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://cfe2d0qes5x87" +path="res://.godot/imported/noisy.png-1b2e79340785c6c0f50d5bad5ce97356.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://examples/images/noisy.png" +dest_files=["res://.godot/imported/noisy.png-1b2e79340785c6c0f50d5bad5ce97356.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/examples/swamp.jpg b/examples/images/swamp.jpg similarity index 100% rename from examples/swamp.jpg rename to examples/images/swamp.jpg diff --git a/examples/swamp.jpg.import b/examples/images/swamp.jpg.import similarity index 72% rename from examples/swamp.jpg.import rename to examples/images/swamp.jpg.import index 3cc2a0b..e0efe23 100644 --- a/examples/swamp.jpg.import +++ b/examples/images/swamp.jpg.import @@ -3,15 +3,15 @@ importer="texture" type="CompressedTexture2D" uid="uid://ckjb0agn5btv7" -path="res://.godot/imported/swamp.jpg-8e3eac7e7aacce65638e712310cdb35c.ctex" +path="res://.godot/imported/swamp.jpg-1dfdcd52a5ef03d42a82a7f06acefa98.ctex" metadata={ "vram_texture": false } [deps] -source_file="res://examples/swamp.jpg" -dest_files=["res://.godot/imported/swamp.jpg-8e3eac7e7aacce65638e712310cdb35c.ctex"] +source_file="res://examples/images/swamp.jpg" +dest_files=["res://.godot/imported/swamp.jpg-1dfdcd52a5ef03d42a82a7f06acefa98.ctex"] [params] diff --git a/examples/lowpass.gdshader b/examples/lowpass.gdshader index 2e235c8..a84c2a2 100644 --- a/examples/lowpass.gdshader +++ b/examples/lowpass.gdshader @@ -1,6 +1,6 @@ shader_type canvas_item; -//!load ./swamp.jpg +//!load ./images/swamp.jpg // Settings const float threshold = 0.6; diff --git a/examples/multistep_distort.gdshader b/examples/multistep_distort.gdshader index d4cc0ba..cea638f 100644 --- a/examples/multistep_distort.gdshader +++ b/examples/multistep_distort.gdshader @@ -1,7 +1,7 @@ shader_type canvas_item; //!steps 9 -//!load ./swamp.jpg +//!load ./images/swamp.jpg const float strength = 0.01; diff --git a/examples/place_texture.gdshader b/examples/place_texture.gdshader index 282ee69..183d080 100644 --- a/examples/place_texture.gdshader +++ b/examples/place_texture.gdshader @@ -3,8 +3,8 @@ shader_type canvas_item; #include "res://shaderlib/transform.gdshaderinc" #include "res://shaderlib/transparency.gdshaderinc" -//!load ./swamp.jpg -//!load+ img2 ./grass.png +//!load ./images/swamp.jpg +//!load+ img2 ./images/grass.png uniform sampler2D img2: repeat_disable, filter_nearest; diff --git a/shaderlib/denoise.gdshaderinc b/shaderlib/denoise.gdshaderinc new file mode 100644 index 0000000..322f6ec --- /dev/null +++ b/shaderlib/denoise.gdshaderinc @@ -0,0 +1,66 @@ + +/* glslSmartDenoise by Michele Morrone, adapted +original code: https://github.com/BrutPitt/glslSmartDeNoise +license of the original code: +BSD 2-Clause License + +Copyright (c) 2019-2020 Michele Morrone +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#define INV_SQRT_OF_2PI 0.39894228040143267793994605993439 // 1.0/SQRT_OF_2PI +#define INV_PI 0.31830988618379067153776752674503 + +vec4 smart_denoise(sampler2D tex, vec2 uv, float sigma, float kSigma, float threshold) { + float radius = round(kSigma*sigma); + float radQ = radius * radius; + + float invSigmaQx2 = .5 / (sigma * sigma); // 1.0 / (sigma^2 * 2.0) + float invSigmaQx2PI = INV_PI * invSigmaQx2; // 1/(2 * PI * sigma^2) + + float invThresholdSqx2 = .5 / (threshold * threshold); // 1.0 / (sigma^2 * 2.0) + float invThresholdSqrt2PI = INV_SQRT_OF_2PI / threshold; // 1.0 / (sqrt(2*PI) * sigma^2) + + vec4 centrPx = texture(tex,uv); + + float zBuff = 0.0; + vec4 aBuff = vec4(0.0); + vec2 size = vec2(textureSize(tex, 0)); + + for (float dx = -radius; dx <= radius; dx++) { + float pt = sqrt(radQ - dx * dx); // pt = yRadius: have circular trend + for (float dy = -pt; dy <= pt; dy++) { + vec2 d = vec2(dx, dy); + float blurFactor = exp( -dot(d, d) * invSigmaQx2 ) * invSigmaQx2PI; + + vec4 walkPx = texture(tex,uv+d/size); + vec4 dC = walkPx-centrPx; + float deltaFactor = exp(-dot(dC, dC) * invThresholdSqx2) * invThresholdSqrt2PI * blurFactor; + + zBuff += deltaFactor; + aBuff += deltaFactor*walkPx; + } + } + return aBuff/zBuff; +} diff --git a/src/Editor.gd b/src/Editor.gd index 2d1aa6e..edc4633 100644 --- a/src/Editor.gd +++ b/src/Editor.gd @@ -132,13 +132,15 @@ var shaderlib_regex = { "hsv": RegEx.create_from_string(r'\s*\#include\s+\"res\:\/\/shaderlib\/hsv\.gdshaderinc\"'), "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\"') + "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\"') } const shaderlib_functions = { "hsv": ["rgb2hsv", "hsv2rgb", "hsv_offset", "hsv_multiply"], "transform": ["place_texture"], "transparency": ["alpha_blend"], - "effects": ["pixelate"] + "effects": ["pixelate"], + "denoise": ["smart_denoise"] } # # configure Highlighter