我正在按照Erik Buck 的本指南为我的视频处理获得绿屏效果。这绝对很棒,但并不完全符合我的要求。我的任务是在我的 iOS 项目中使用 OpenGL使用相同的方法“切掉”白色。
下面是来自 Erik Buck 项目的 OpenGL 代码,用于查找每个纹理的绿色纹素并将其不透明度分配为零:
varying highp vec2 vCoordinate;
uniform sampler2D uVideoframe;
void main()
{
// Look up the color of the texel corresponding to the fragment being
// generated while rendering a triangle
lowp vec4 tempColor = texture2D(uVideoframe, vCoordinate);
// Calculate the average intensity of the texel's red and blue components
lowp float rbAverage = tempColor.r * 0.5 + tempColor.b * 0.5;
// Calculate the difference between the green element intensity and the
// average of red and blue intensities
lowp float gDelta = tempColor.g - rbAverage;
// If the green intensity is greater than the average of red and blue
// intensities, calculate a transparency value in the range 0.0 to 1.0
// based on how much more intense the green element is
tempColor.a = 1.0 - smoothstep(0.0, 0.25, gDelta);
// Use the cube of the transparency value. That way, a fragment that
// is partially translucent becomes even more translucent. This sharpens
// the final result by avoiding almost but not quite opaque fragments that
// tend to form halos at color boundaries.
tempColor.a = tempColor.a * tempColor.a * tempColor.a;
gl_FragColor = tempColor;
}
通过反复试验,我能够操纵此代码以使不同的颜色透明,但白色已被证明是一个挑战。
如何使背景中的白色透明?