diff --git a/README.md b/README.md index 214f6b8..b9a0ac6 100644 --- a/README.md +++ b/README.md @@ -80,12 +80,15 @@ Here is an example: ```glsl shader_type canvas_item; -#include "res://shaderlib/hsv.gdshaderinc" +#include "res://shaderlib/oklab.gdshaderinc" -//!load ./examples/images/swamp.jpg +//!load ./images/swamp.jpg void fragment() { - COLOR = hsv_offset(COLOR, 0.32, 0.2, 0.0); + vec4 oklab = rgb2oklab(COLOR); + vec4 oklch = oklab2oklch(oklab); + oklch.z -= 2.0; + COLOR = oklab2rgb(oklch2oklab(oklch)); } ``` diff --git a/examples/color_and_pixelate.gdshader b/examples/color_and_pixelate.gdshader index bf5f18a..dfb5e7b 100644 --- a/examples/color_and_pixelate.gdshader +++ b/examples/color_and_pixelate.gdshader @@ -1,7 +1,7 @@ shader_type canvas_item; -#include "res://shaderlib/colorspaces.gdshaderinc" -#include "res://shaderlib/effects.gdshaderinc" +#include "res://shaderlib/hsv.gdshaderinc" +#include "res://shaderlib/pixelate.gdshaderinc" //!load ./images/swamp.jpg diff --git a/examples/oklab.gdshader b/examples/oklab.gdshader index 0d8f093..8018695 100644 --- a/examples/oklab.gdshader +++ b/examples/oklab.gdshader @@ -1,6 +1,6 @@ shader_type canvas_item; -#include "res://shaderlib/colorspaces.gdshaderinc" +#include "res://shaderlib/oklab.gdshaderinc" //!load ./images/swamp.jpg diff --git a/examples/place_texture.gdshader b/examples/place_texture.gdshader index 183d080..76d01fb 100644 --- a/examples/place_texture.gdshader +++ b/examples/place_texture.gdshader @@ -1,7 +1,7 @@ shader_type canvas_item; -#include "res://shaderlib/transform.gdshaderinc" -#include "res://shaderlib/transparency.gdshaderinc" +#include "res://shaderlib/place_texture.gdshaderinc" +#include "res://shaderlib/common.gdshaderinc" //!load ./images/swamp.jpg //!load+ img2 ./images/grass.png diff --git a/shaderlib/common.gdshaderinc b/shaderlib/common.gdshaderinc index 7ee57f8..8af35dc 100644 --- a/shaderlib/common.gdshaderinc +++ b/shaderlib/common.gdshaderinc @@ -3,3 +3,13 @@ float cbrt(float x) { return pow(x, 1.0/3.0); } + +/* + Alpha Blending a over b after Bruce A. Wallace + source: https://en.wikipedia.org/wiki/Alpha_compositing +*/ +vec4 alpha_blend(vec4 b, vec4 a) { + float alpha = a.a + (b.a * (1.0 - a.a)); + vec3 col = ((a.rgb*a.a) + ((b.rgb*b.a) * (1.0 - a.a)) / alpha); + return vec4(col.r, col.g, col.b, alpha); +} diff --git a/shaderlib/hsv.gdshaderinc b/shaderlib/hsv.gdshaderinc new file mode 100644 index 0000000..2a7834d --- /dev/null +++ b/shaderlib/hsv.gdshaderinc @@ -0,0 +1,27 @@ + +/* + rgb2hsv and hsv2rgb functions adapted + from https://godotshaders.com/shader/hsv-adjustment/ + original code by https://godotshaders.com/author/al1-ce/ + + Color space conversion functions always work with vec4. + The fourth value is always alpha. +*/ + +// Convert RGB to HSV (hue, saturation, brightness) +vec4 rgb2hsv(vec4 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec4(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x, c.a); +} + +// Convert HSV back to RGB (red, green, blue) +vec4 hsv2rgb(vec4 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + vec3 rgb = c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); + return vec4(rgb.r, rgb.g, rgb.b, c.a); +} diff --git a/shaderlib/colorspaces.gdshaderinc b/shaderlib/oklab.gdshaderinc similarity index 64% rename from shaderlib/colorspaces.gdshaderinc rename to shaderlib/oklab.gdshaderinc index 6a8ae9b..96b6f34 100644 --- a/shaderlib/colorspaces.gdshaderinc +++ b/shaderlib/oklab.gdshaderinc @@ -1,42 +1,16 @@ -/* - Color space conversion functions always work with vec4. - The fourth value is always alpha. -*/ - -#include "res://shaderlib/common.gdshaderinc" - -/* - rgb2hsv and hsv2rgb functions adapted - from https://godotshaders.com/shader/hsv-adjustment/ - original code by https://godotshaders.com/author/al1-ce/ -*/ - -// Convert RGB to HSV (hue, saturation, brightness) -vec4 rgb2hsv(vec4 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec4(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x, c.a); -} - -// Convert HSV back to RGB (red, green, blue) -vec4 hsv2rgb(vec4 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - vec3 rgb = c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); - return vec4(rgb.r, rgb.g, rgb.b, c.a); -} - /* OkLab and OkLCh For more details on oklab, see - https://bottosson.github.io/posts/oklab/ - https://en.wikipedia.org/wiki/Oklab_color_space + + Color space conversion functions always work with vec4. + The fourth value is always alpha. */ +#include "res://shaderlib/common.gdshaderinc" + vec4 rgb2oklab(vec4 c) { float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b; float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b; diff --git a/shaderlib/effects.gdshaderinc b/shaderlib/pixelate.gdshaderinc similarity index 100% rename from shaderlib/effects.gdshaderinc rename to shaderlib/pixelate.gdshaderinc diff --git a/shaderlib/transform.gdshaderinc b/shaderlib/place_texture.gdshaderinc similarity index 100% rename from shaderlib/transform.gdshaderinc rename to shaderlib/place_texture.gdshaderinc diff --git a/shaderlib/transparency.gdshaderinc b/shaderlib/transparency.gdshaderinc deleted file mode 100644 index a97e4b7..0000000 --- a/shaderlib/transparency.gdshaderinc +++ /dev/null @@ -1,10 +0,0 @@ - -/* - Alpha Blending a over b after Bruce A. Wallace - source: https://en.wikipedia.org/wiki/Alpha_compositing -*/ -vec4 alpha_blend(vec4 b, vec4 a) { - float alpha = a.a + (b.a * (1.0 - a.a)); - vec3 col = ((a.rgb*a.a) + ((b.rgb*b.a) * (1.0 - a.a)) / alpha); - return vec4(col.r, col.g, col.b, alpha); -}