Skip to content

Instantly share code, notes, and snippets.

@Lokno
Last active December 5, 2023 17:00
Show Gist options
  • Save Lokno/1674486360baa9ce4cfa97d1938f760e to your computer and use it in GitHub Desktop.
Save Lokno/1674486360baa9ce4cfa97d1938f760e to your computer and use it in GitHub Desktop.
OBS shaderfilter designed to clamp the value of a color the more desaturated it is
// https://obsproject.com/forum/resources/obs-shaderfilter.1736/
uniform float vmaxThreshold<
string label = "Threshold";
string widget_type = "slider";
float minimum = 0.0;
float maximum = 1.0;
float step = 0.001;
> = 0.2; //<Range(0.0, 1.0)>
// https://www.chilliant.com/rgb2hsv.html
float3 RGBtoHCV(in float3 RGB)
{
float Epsilon = 1e-10;
// Based on work by Sam Hocevar and Emil Persson
float4 P = (RGB.g < RGB.b) ? float4(RGB.bg, -1.0, 2.0/3.0) : float4(RGB.gb, 0.0, -1.0/3.0);
float4 Q = (RGB.r < P.x) ? float4(P.xyw, RGB.r) : float4(RGB.r, P.yzx);
float C = Q.x - min(Q.w, Q.y);
float H = abs((Q.w - Q.y) / (6 * C + Epsilon) + Q.z);
return float3(H, C, Q.x);
}
float3 HUEtoRGB(in float H)
{
float R = abs(H * 6 - 3) - 1;
float G = 2 - abs(H * 6 - 2);
float B = 2 - abs(H * 6 - 4);
return saturate(float3(R,G,B));
}
// RGB to HSV conversion
float3 RGBtoHSV(in float3 RGB)
{
float Epsilon = 1e-10;
float3 HCV = RGBtoHCV(RGB);
float S = HCV.y / (HCV.z + Epsilon);
return float3(HCV.x, S, HCV.z);
}
// HSV to RGB conversion
float3 HSVtoRGB(in float3 HSV)
{
float3 RGB = HUEtoRGB(HSV.x);
return ((RGB - 1) * HSV.y + 1) * HSV.z;
}
// Cubic ease-out function
float cubicEaseOut(float x) {
x = 1.0 - x;
return 1.0 - x * x * x;
}
float4 mainImage(VertData v_in) : TARGET
{
float4 originalColor = image.Sample(textureSampler, v_in.uv);
float3 hsv = RGBtoHSV(originalColor.rgb);
// Clamp value as saturation decreases with cubic ease-out
float vmax = lerp(vmaxThreshold,1.0,cubicEaseOut(hsv.y));
// Apply the value adjustment
hsv.z = min(hsv.z, vmax);
return float4(HSVtoRGB(hsv), originalColor.a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment