有人可以解释这个Fragment Shader?它是一个色度键滤镜(绿屏效果) [英] Can someone please explain this Fragment Shader? It is a Chroma Key Filter (Green screen effect)
问题描述
我想了解这个色度键筛选器的运作方式。色度键,如果你不知道,是一个绿色屏幕效果。有人能够解释这些函数中的一些是如何工作的,以及他们在做什么吗?
I'm trying to understand how this chroma key filter works. Chroma Key, if you don't know, is a green screen effect. Would someone be able to explain how some of these functions work and what they are doing exactly?
float maskY = 0.2989 * colorToReplace.r + 0.5866 * colorToReplace.g + 0.1145 * colorToReplace.b;
float maskCr = 0.7132 * (colorToReplace.r - maskY);
float maskCb = 0.5647 * (colorToReplace.b - maskY);
float Y = 0.2989 * textureColor.r + 0.5866 * textureColor.g + 0.1145 * textureColor.b;
float Cr = 0.7132 * (textureColor.r - Y);
float Cb = 0.5647 * (textureColor.b - Y);
float blendValue = smoothstep(thresholdSensitivity, thresholdSensitivity + smoothing, distance(vec2(Cr, Cb), vec2(maskCr, maskCb)));
gl_FragColor = vec4(textureColor.rgb * blendValue, 1.0 * blendValue);
我理解前6行(转换要替换的颜色,这可能是绿色的,纹理颜色到YCrCb颜色系统)。
I understand the first 6 lines (converting the color to replace, which is probably green, and the texture color to the YCrCb color system).
此片段着色器有两个输入浮点值:thresholdSensitivity和Smoothing。
This fragment shader has two input float values: thresholdSensitivity and Smoothing.
- 阈值灵敏度控制类似像素需要着色的替换方式。
- 平滑控制逐渐相似的颜色在图像中被替换。
我不明白这些值如何在blendValue行中使用。 blendValue计算是什么?
I don't understand how those values are used in the blendValue line. What does blendValue compute? How does the blendValue line and the gl_FragColor line actually create the green screen effect?
推荐答案
smoothstep
函数在一个间隔(由前两个参数指定)中评估一个平滑的三次曲线。与GLSL的 mix
函数相比,它将其参数线性混合为:
The smoothstep
function in GLSL evaluates a smooth cubic curve over an interval (specified by the first two parameters). As compared to GLSL's mix
function, which linearly blends its parameters as:
smoothstep
使用Hermite立方多项式来确定值
smoothstep
uses a Hermite cubic polynomial to determine the value
在您的着色器中,blendValue
In your shader, blendValue is a smooth interpolation of your smoothing value based on the distance between the red and blue chrominance values.
最后, gl_FragColor $> c $ c>指定最终片段颜色(在混合之前,其发生在片段着色器完成之后)。在您的情况下,它是从输入图像读取的调制值,以及半透明的调制Alpha值。
Finally, gl_FragColor
specifies the final fragment color (before blending, which occurs after completion of the fragment shader). In your case, it's the modulated value read from the input image, and a modulated alpha value for translucency.
这篇关于有人可以解释这个Fragment Shader?它是一个色度键滤镜(绿屏效果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!