3 ms·
To visualize data on a color scale, I just sample the hue with this, I guess you can also use this to calculate other things. vec3 coolhue(float h){
by jokoon 7y ago
To visualize data on a color scale, I just sample the hue with this, I guess you can also use this to calculate other things.
vec3 coolhue(float h){
if (h == 1.f) return vec3(1.f,0,0);
h *= 6.0f;
if(h<1.f) { return vec3(0, 1.f, h); } // +blue
if(h<2.f) { h -= 1.f; return vec3(0, 1.f - h, 1.f); } // -green
if(h<3.f) { h -= 2.f; return vec3(h, 0, 1.f); } // +red
if(h<4.f) { h -= 3.f; return vec3(1.f, 0, 1.f - h); } // -blue
if(h<5.f) { h -= 4.f; return vec3(1.f, h, 0); } // +green
if(h<6.f) { h -= 5.f; return vec3(1.f - h, 1.f, 0); } // -red
return vec3(1,1,1);
}
This is for GLSL shaders, since shaders don't allow switch()
- contravariant 7y agoFor shaders you're probably faster off if you use the expression #define saturate(x) clamp(x,0,1) rgb = saturate(vec3(abs(hue-180)/60 - 1, 2 - abs(hue-120)/60, 2 - abs(hue-240)/60)) Since it avoids the if/then. Although I'm not too sure if it will work well in GLSL as HLSL has better support for the saturate function (it can usually be merged with some other operator).
- kaoD 7y agoWorks fine in GLSL (https://www.shadertoy.com/view/tdV3DW https://www.shadertoy.com/view/tdV3DW) Starting on green and mapped to [0, 1] like op's: vec3 coolhue2(float h){ float h6 = 6.*h; return saturate( vec3( 2. - abs(h6 - 4.), abs(h6 - 3.) - 1., 2. - abs(h6 - 2.) )); }