Alpha 与 OpenGL ES 2.0 混合?
最好的办法是什么?
我尝试使用如下所示的片段着色器天真地执行此操作:
varying lowp vec4 color;
void main()
{
lowp vec4 alpha = colorVarying.wwww;
const lowp vec4 one = vec4(1.0, 1.0, 1.0, 1.0);
lowp vec4 oneMinusAlpha = one-alpha;
gl_FragColor = gl_FragColor*oneMinusAlpha + colorVarying*alpha;
gl_FragColor.w = 1.0;
}
但这不起作用,因为在着色器运行之前 gl_FragColor 似乎不包含任何有意义的内容。
正确的做法是什么?
What's the best way?
I tried to do this naïvely with a fragment shader that looks like this:
varying lowp vec4 color;
void main()
{
lowp vec4 alpha = colorVarying.wwww;
const lowp vec4 one = vec4(1.0, 1.0, 1.0, 1.0);
lowp vec4 oneMinusAlpha = one-alpha;
gl_FragColor = gl_FragColor*oneMinusAlpha + colorVarying*alpha;
gl_FragColor.w = 1.0;
}
But this doesn't work, because it seems gl_FragColor does not contain anything meaningful before the shader runs.
What's the correct approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Alpha 混合已为您完成。在着色器退出时,
gl_FragColor
应将 alpha 值保存在w
组件中,并且您必须使用普通 API 设置混合模式,就像根本没有着色器一样。例如,gl_FragColor = vec4(0,1,0,0.5)
将生成绿色、50% 透明的片段。Alpha blending is done for you. On shader exit,
gl_FragColor
should hold the alpha value inw
component and you have to set the blending mode with the normal API just like there is no shader at all. For examplegl_FragColor = vec4(0,1,0,0.5)
will result in a green, 50% transparent fragment.