有人可以解释一下这个片段着色器吗?这是一个色度键过滤器(绿屏效果) [英] Can someone please explain this Fragment Shader? It is a Chroma Key Filter (Green screen effect)
问题描述
我正在尝试了解此色度键过滤器的工作原理.Chroma Key,如果你不知道,是一种绿屏效果.有人能解释一下这些函数中的一些是如何工作的以及它们究竟在做什么吗?
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行(将颜色转换为replace,可能是绿色,纹理颜色转换为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 计算什么?blendValue 线和 gl_FragColor 线实际上是如何创建绿屏效果的?
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?
推荐答案
GLSL 中的 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
指定最终的片段颜色(在混合之前,在片段着色器完成之后发生).在您的情况下,它是从输入图像读取的调制值,以及半透明的调制 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.
这篇关于有人可以解释一下这个片段着色器吗?这是一个色度键过滤器(绿屏效果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!