From d9f38dab3e4d617872724d1b43552ec1f1103f61 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Fri, 3 Mar 2023 22:37:39 +0100 Subject: [PATCH] Added support for changing the saturation for font textures. --- es-core/src/components/TextComponent.cpp | 7 +++++++ es-core/src/components/TextComponent.h | 1 + es-core/src/resources/Font.cpp | 8 ++++++++ es-core/src/resources/Font.h | 1 + resources/shaders/glsl/core.glsl | 11 +++++++++-- 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/es-core/src/components/TextComponent.cpp b/es-core/src/components/TextComponent.cpp index affcd4840..92e6f86d9 100644 --- a/es-core/src/components/TextComponent.cpp +++ b/es-core/src/components/TextComponent.cpp @@ -115,6 +115,13 @@ void TextComponent::setOpacity(float opacity) mTextCache->setOpacity(mThemeOpacity); } +void TextComponent::setSaturation(float saturation) +{ + mSaturation = saturation; + if (mTextCache) + mTextCache->setSaturation(saturation); +} + void TextComponent::setDimming(float dimming) { mDimming = dimming; diff --git a/es-core/src/components/TextComponent.h b/es-core/src/components/TextComponent.h index 17e5118fa..96d8834c8 100644 --- a/es-core/src/components/TextComponent.h +++ b/es-core/src/components/TextComponent.h @@ -62,6 +62,7 @@ public: float const getColorOpacity() const override { return mColorOpacity; } void setOpacity(float opacity) override; + void setSaturation(float saturation) override; void setDimming(float dimming) override; void setSelectable(bool status) { mSelectable = status; } diff --git a/es-core/src/resources/Font.cpp b/es-core/src/resources/Font.cpp index cf23b0c6e..f1ecf5652 100644 --- a/es-core/src/resources/Font.cpp +++ b/es-core/src/resources/Font.cpp @@ -826,6 +826,14 @@ void TextCache::setOpacity(float opacity) } } +void TextCache::setSaturation(float saturation) +{ + for (auto it = vertexLists.begin(); it != vertexLists.end(); ++it) { + for (auto it2 = it->verts.begin(); it2 != it->verts.end(); ++it2) + it2->saturation = saturation; + } +} + void TextCache::setDimming(float dimming) { for (auto it = vertexLists.begin(); it != vertexLists.end(); ++it) { diff --git a/es-core/src/resources/Font.h b/es-core/src/resources/Font.h index ece1cd7c9..86990aa53 100644 --- a/es-core/src/resources/Font.h +++ b/es-core/src/resources/Font.h @@ -238,6 +238,7 @@ public: void setColor(unsigned int color); void setOpacity(float opacity); + void setSaturation(float saturation); void setDimming(float dimming); friend Font; diff --git a/resources/shaders/glsl/core.glsl b/resources/shaders/glsl/core.glsl index cc1f04dcd..da5609e47 100644 --- a/resources/shaders/glsl/core.glsl +++ b/resources/shaders/glsl/core.glsl @@ -79,8 +79,8 @@ void main() sampledColor.rgb *= sampledColor.a; } - // Saturation. - if (saturation != 1.0) { + // Saturation, except for font textures. + if (saturation != 1.0 && 0x0u == (shaderFlags & 0x2u)) { vec3 grayscale; // Premultiplied textures are all in BGRA format. if (0x0u != (shaderFlags & 0x01u)) @@ -105,6 +105,13 @@ void main() sampledColor *= color; } + // Saturation for font textures. + if (saturation != 1.0 && 0x0u != (shaderFlags & 0x2u)) { + vec3 grayscale = vec3(dot(sampledColor.rgb, vec3(0.299, 0.587, 0.114))); + vec3 blendedColor = mix(grayscale, sampledColor.rgb, saturation); + sampledColor = vec4(blendedColor, sampledColor.a); + } + // When post-processing we drop the alpha channel to avoid strange issues with some // graphics drivers. if (0x0u != (shaderFlags & 0x4u))