Skip to content

Commit 6ecdc3c

Browse files
authored
Add HSVUtils.glsllib (#2644)
* Add HSVUtils.glsllib Adds support for converting between RGB and HSV to allow for adjusting colors in HSV color space in shaders. * Update Unshaded.frag * Update PBRLightingUtils.glsllib * Update PBRLighting.j3md * Update Unshaded.j3md * Update Lighting.frag * Update Lighting.j3md
1 parent 16ae3d7 commit 6ecdc3c

File tree

7 files changed

+70
-0
lines changed

7 files changed

+70
-0
lines changed

jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.frag

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#import "Common/ShaderLib/MaterialFog.glsllib"
1111
#endif
1212

13+
#ifdef HSV_OFFSET
14+
#import "Common/ShaderLib/HSVUtils.glsllib"
15+
uniform vec3 m_HSVOffset;
16+
#endif
17+
1318
varying vec2 texCoord;
1419
#ifdef SEPARATE_TEXCOORD
1520
varying vec2 texCoord2;
@@ -113,6 +118,10 @@ void main(){
113118
vec4 diffuseColor = vec4(1.0);
114119
#endif
115120

121+
#ifdef HSV_OFFSET
122+
diffuseColor.rgb = alterColorWithHsvOffset(diffuseColor.rgb, m_HSVOffset);
123+
#endif
124+
116125
float alpha = DiffuseSum.a * diffuseColor.a;
117126
#ifdef ALPHAMAP
118127
alpha = alpha * texture2D(m_AlphaMap, newTexCoord).r;

jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ MaterialDef Phong Lighting {
2222
// Diffuse color
2323
Color Diffuse
2424

25+
//Optional vec3 for adjusting the final Color in HSV color space
26+
Vector3 HSVOffset
27+
2528
// Specular color
2629
Color Specular
2730

@@ -173,12 +176,14 @@ MaterialDef Phong Lighting {
173176
NUM_MORPH_TARGETS: NumberOfMorphTargets
174177
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
175178
NORMAL_TYPE: NormalType
179+
HSV_OFFSET : HSVOffset
176180

177181
// fog - jayfella
178182
USE_FOG : UseFog
179183
FOG_LINEAR : LinearFog
180184
FOG_EXP : ExpFog
181185
FOG_EXPSQ : ExpSqFog
186+
182187
}
183188
}
184189

jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ MaterialDef PBR Lighting {
1919
// the emissive intensity
2020
Float EmissiveIntensity : 2.0
2121

22+
//Optional vec3 for adjusting the final BaseColor in HSV color space
23+
Vector3 HSVOffset
24+
2225
// BaseColor map
2326
Texture2D BaseColorMap
2427

@@ -215,6 +218,7 @@ MaterialDef PBR Lighting {
215218
FOG_LINEAR : LinearFog
216219
FOG_EXP : ExpFog
217220
FOG_EXPSQ : ExpSqFog
221+
HSV_OFFSET : HSVOffset
218222
}
219223
}
220224

jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.frag

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ uniform sampler2D m_LightMap;
1616
uniform float m_DesaturationValue;
1717
#endif
1818

19+
#ifdef HSV_OFFSET
20+
#import "Common/ShaderLib/HSVUtils.glsllib"
21+
uniform vec3 m_HSVOffset;
22+
#endif
23+
1924
varying vec2 texCoord1;
2025
varying vec2 texCoord2;
2126

@@ -49,6 +54,10 @@ void main(){
4954
discard;
5055
}
5156
#endif
57+
58+
#ifdef HSV_OFFSET
59+
color.rgb = alterColorWithHsvOffset(color.rgb, m_HSVOffset);
60+
#endif
5261

5362
#ifdef DESATURATION
5463
vec3 gray = vec3(dot(vec3(0.2126,0.7152,0.0722), color.rgb));

jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ MaterialDef Unshaded {
99
Float PointSize : 1.0
1010
Boolean SeparateTexCoord
1111

12+
//Optional vec3 for adjusting the final Color in HSV color space
13+
Vector3 HSVOffset
14+
1215
// Texture of the glowing parts of the material
1316
Texture2D GlowMap
1417
// The glow color of the object
@@ -89,6 +92,7 @@ MaterialDef Unshaded {
8992
NUM_MORPH_TARGETS: NumberOfMorphTargets
9093
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
9194
DESATURATION : DesaturationValue
95+
HSV_OFFSET : HSVOffset
9296
}
9397
}
9498

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Functions for converting between RGB and HSV to allow for adjusting colors in HSV color space
2+
3+
vec3 rgb2hsv(vec3 c){
4+
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
5+
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
6+
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
7+
8+
float d = q.x - min(q.w, q.y);
9+
float e = 1.0e-10;
10+
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
11+
}
12+
13+
vec3 hsv2rgb(vec3 c){
14+
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
15+
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
16+
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
17+
}
18+
19+
vec3 alterColorWithHsvOffset(vec3 color, vec3 hsvOffset){
20+
vec3 colorHSV = rgb2hsv(color);
21+
colorHSV += hsvOffset;
22+
23+
// wrap hue
24+
colorHSV.x = fract(colorHSV.x);
25+
26+
// clamp saturation and value
27+
colorHSV.yz = clamp(colorHSV.yz, 0.0, 1.0);
28+
29+
return hsv2rgb(colorHSV);
30+
}

jme3-core/src/main/resources/Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
uniform sampler2D m_BaseColorMap;
5858
#endif
5959

60+
#ifdef HSV_OFFSET
61+
#import "Common/ShaderLib/HSVUtils.glsllib"
62+
uniform vec3 m_HSVOffset;
63+
#endif
64+
6065
#ifdef USE_PACKED_MR
6166
uniform sampler2D m_MetallicRoughnessMap;
6267
#else
@@ -346,6 +351,10 @@
346351
if( baseColor.a < m_AlphaDiscardThreshold) discard;
347352
#endif
348353

354+
#ifdef HSV_OFFSET
355+
baseColor.rgb = alterColorWithHsvOffset(baseColor.rgb, m_HSVOffset);
356+
#endif
357+
349358
surface.albedo = baseColor.rgb;
350359
surface.alpha = baseColor.a;
351360

0 commit comments

Comments
 (0)