简体   繁体   English

WebGL:更改片段着色器中的颜色饱和度或亮度

[英]WebGL: Change color saturation or luminosity in fragment shader

i found this great page hls picker , and i'm wondering if there is possibility to achieve similar effect in WebGL.我发现了这个很棒的页面hls 选择器,我想知道是否有可能在 WebGL 中实现类似的效果。 I'm passing to my fragment shader some color, for example #FF7400, what is the easiest way to convert it to hsl and change its luminosity, or to have smooth transition to black color (luminosity equel 0).我正在向我的片段着色器传递一些颜色,例如#FF7400,将其转换为 hsl 并更改其亮度或平滑过渡到黑色(亮度 equel 0)的最简单方法是什么。 I want to make clouds in my page that have different color(luminosity) depends how far they are from sun.我想在我的页面中制作具有不同颜色(亮度)的云,这取决于它们离太阳的距离。 Thanks in advance for any help.提前感谢您的帮助。

thanks for geat links but i think that i found much simplier way to made easy color transition, all i need is to use webGL method T mix(T x, T y, float a) - linear blend of x and y. 感谢geat链接,但我认为我发现了更简单的方法来轻松进行颜色过渡,我需要做的就是使用webGL方法T mix(T x, T y, float a) -x和y的线性混合。

This code i use in shadertoy editor : 我在shadertoy编辑器中使用的这段代码:

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
     vec2 uv = gl_FragCoord.xy / iResolution.xy;
     vec4 orange = vec4(0.533, 0.25, 0.145, 1.0);
     vec4 blue = vec4(0.18, 0.23, 0.27, 1.0);
     vec4 black = vec4(0.0, 0.0, 0.0, 1.0);
     vec4 white = vec4(1.0, 1.0, 1.0, 1.0);

     float ratio = iResolution.x / iResolution.y;
     float PI = 3.14159265359;

     vec4 mixC = mix(orange, blue, sin(ratio * uv.y));
     mixC = mix(mixC, black, cos(2.0 * PI * uv.x) / ratio);
     mixC = mix(mixC, black, cos(2.0 * PI * uv.y) / ratio);
     mixC = mix(mixC, white, 0.1);
     fragColor = mixC;   
}

As you can see there, i've made transition between of four colors with just couple lines of code and the results looks like this: 如您所见,我仅用几行代码就完成了四种颜色的转换,结果如下所示: 简单的颜色过渡

I think about fragment shader as a little photoshop. 我认为片段着色器就像一个小小的Photoshop。 Every photoshop operation should be possible with WebGL. WebGL应该可以进行每个photoshop操作。

If we are talking about 2D image where sun position is relative and you want to just use some basic image processing, you can use functions from this answer rgb2hsv and hsv2rgb . 如果我们正在谈论太阳位置是相对的2D图像,而您只想使用一些基本的图像处理,则可以使用此答案中的函数rgb2hsvhsv2rgb I think it should work with GLSL 1. Then you can multiply Sat, Lum or Hue and then transfer it back to RGB. 我认为它应该与GLSL 1兼容。然后,您可以将Sat,Lum或Hue相乘,然后将其传输回RGB。

If it doesnt, you might have to reimplement it from common formula, use wiki or this link: http://www.rapidtables.com/convert/color/rgb-to-hsl.htm 如果没有,您可能必须从通用公式重新实现,请使用Wiki或以下链接: http : //www.rapidtables.com/convert/color/rgb-to-hsl.htm

If you want to do some more image processing, when neighbour pixels are needed, I suggest this great tutorial where you can easy do some edge sharpening, blur etc.: http://webglfundamentals.org/webgl/lessons/webgl-image-processing.html 如果您想进行更多图像处理,则在需要相邻像素的情况下,建议您使用此出色的教程,在该教程中可以轻松进行一些边缘锐化,模糊处理等操作: http : //webglfundamentals.org/webgl/lessons/webgl-image- processing.html

original image原始图像

Hi guys, i think i found a unorthodox but easy way to desaturate an RGB image, we just need to find the average color for the pixel, average _color = (R+G+B)/3 keeping the Alpha... vec4(average_color,average_color,average_color, Alpha);大家好,我想我找到了一种非正统但简单的方法来降低 RGB 图像的饱和度,我们只需要找到像素的平均颜色,平均 _color = (R+G+B)/3 保持 Alpha...vec4(平均颜色,平均颜色,平均颜色,阿尔法);

    void main()
{
    //write a color of the current fragment to a variable
    lowp vec4 color_of_pixel = texture2D(texture_sampler, var_texcoord0.xy);

    //Find the average among red, green and blue and it keeps the "force" of the color...
    float average_color = ((color_of_pixel.r + color_of_pixel.g + color_of_pixel.b)/3);

    lowp vec4 color_of_pixel_final = vec4(average_color,average_color,average_color,color_of_pixel.a);
    gl_FragColor = color_of_pixel_final;  // write the color_of_pixel to the output gl_FragColor
}

Desaturated image去饱和图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM