简体   繁体   English

Alpha与OpenGL ES 2.0融合了吗?

[英]Alpha blending with OpenGL ES 2.0?

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. 但这是行不通的,因为在着色器运行之前,gl_FragColor似乎不包含任何有意义的内容。

What's the correct approach? 正确的方法是什么?

Alpha blending is done for you. Alpha混合已为您完成。 On shader exit, gl_FragColor should hold the alpha value in w component and you have to set the blending mode with the normal API just like there is no shader at all. 在着色器退出时, gl_FragColor应该在w组件中保留alpha值,并且您必须使用普通API设置混合模式,就像根本没有着色器一样。 For example gl_FragColor = vec4(0,1,0,0.5) will result in a green, 50% transparent fragment. 例如gl_FragColor = vec4(0,1,0,0.5)将产生绿色的50%透明片段。

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

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