From a8630e4ae1e24a0626f0b1b250301fc16fca62fe Mon Sep 17 00:00:00 2001 From: Ryan McDonough Date: Mon, 16 Mar 2026 23:06:00 -0400 Subject: [PATCH] Add HSVUtils.glsllib Adds support for converting between RGB and HSV to allow for adjusting colors in HSV color space in shaders. --- .../Common/ShaderLib/HSVUtils.glsllib | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 jme3-core/src/main/resources/Common/ShaderLib/HSVUtils.glsllib diff --git a/jme3-core/src/main/resources/Common/ShaderLib/HSVUtils.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/HSVUtils.glsllib new file mode 100644 index 0000000000..ade9f9b554 --- /dev/null +++ b/jme3-core/src/main/resources/Common/ShaderLib/HSVUtils.glsllib @@ -0,0 +1,30 @@ +// Functions for converting between RGB and HSV to allow for adjusting colors in HSV color space + +vec3 rgb2hsv(vec3 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 vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 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); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} + +vec3 alterColorWithHsvOffset(vec3 color, vec3 hsvOffset){ + vec3 colorHSV = rgb2hsv(color); + colorHSV += hsvOffset; + + // wrap hue + colorHSV.x = fract(colorHSV.x); + + // clamp saturation and value + colorHSV.yz = clamp(colorHSV.yz, 0.0, 1.0); + + return hsv2rgb(colorHSV); +}