godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Add built-in rgb_to_hsv() and hsv_to_rgb() functions to shaders #8635

Closed GuyUnger closed 9 months ago

GuyUnger commented 9 months ago

Describe the project you are working on

Games

Describe the problem or limitation you are having in your project

The Color class has the very useful h, s and v aliases. Since I'm frequently using this, I become reliant on it in my thinking about colors in godot. Shaders doesn't come with this functionality so I end up copy and pasting functions for it very often.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add built-in rgb_to_hsv() and hsv_to_rgb() functions to shaders.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

These are the ones I found and use, I don't know how closely they match up with the gdscript ones:

vec3 rgb_to_hsv(vec3 rgb) {
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));
    vec4 q = mix(vec4(p.xyw, rgb.r), vec4(rgb.r, p.yzx), step(p.x, rgb.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 hsv_to_rgb(vec3 hsv) {
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(hsv.xxx + K.xyz) * 6.0 - K.www);
    return hsv.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), hsv.y);
}

If this enhancement will not be used often, can it be worked around with a few lines of script?

Yes, but they're very useful to have more easily accessible and fit with working with colors in godot.

Is there a reason why this should be core and not an add-on in the asset library?

^

paddy-exe commented 9 months ago

Related/duplicate to https://github.com/godotengine/godot-proposals/issues/6454

Calinou commented 9 months ago

Thanks for the proposal! Consolidating in https://github.com/godotengine/godot-proposals/issues/6454.