简体   繁体   English

使用GLSL Android Opengl ES 2.0的透明度

[英]Transparency using GLSL Android Opengl es 2.0

I am working on opengl es 2.0 for android application. 我正在为Android应用程序开发opengl es 2.0。 I have a model for which i apply texture. 我有一个要应用纹理的模型。 I do this by using the below shaders. 我通过使用以下着色器来做到这一点。 Its a simple shader. 它是一个简单的着色器。

In the below shader when the texture file is transparent at some places I get a black color. 在下面的着色器中,当纹理文件在某些​​位置透明时,我得到黑色。 Instead I want to find the places where it is transparent and give a particular color like RGB(0.6,0.7,0.3). 相反,我想找到透明的地方,并给出RGB(0.6,0.7,0.3)之类的特定颜色。 Please let me know how to modify the below shader to get this in my model. 请让我知道如何修改下面的着色器以在我的模型中得到它。

protected static final String mVShader = 

    "uniform mat4 uMVPMatrix;\n" +
    "attribute vec4 aPosition;\n" +
    "attribute vec2 aTextureCoord;\n" +
    "varying vec2 vTextureCoord;\n" +

    "void main() {\n" +
    "   gl_Position = uMVPMatrix * aPosition;\n" +
    "   vTextureCoord = aTextureCoord;\n" +
    "}\n";

protected static final String mFShader = 

    "precision mediump float;\n" +

    "varying vec2 vTextureCoord;\n" +
    "uniform sampler2D uTexture0;\n" +

    "void main() {\n" +
    "   gl_FragColor = texture2D(uTexture0, vTextureCoord);\n" +
    "}\n";

Now the easiest thing would just be to check the texture color's alpha and output a different color depending on this: 现在,最简单的方法就是检查纹理颜色的Alpha值并根据此输出不同的颜色:

void main()
{
    vec4 color = texture2D(uTexture0, vTextureCoord);
    gl_FragColor = (color.a > 0.1) ? color : vec4(0.6, 0.7, 0.3, 1.0);
}

Of course you can adapt this threshold, but using just 0.0 might not work due to precision issues. 当然,您可以调整此阈值,但是由于精度问题,仅使用0.0可能不起作用。 Dynamic branching in the fragment shader might not be the best idea performance-wise, but if you say you cannot just change the texture because the color may change, this will be the easiest way. 片段着色器中的动态分支可能并不是最佳的性能选择,但是如果您说不能更改纹理,因为颜色可能会更改,这将是最简单的方法。

If the color change is persistent over a large number of frames, you maybe also could create a temporary texture, which contains the changed color in the transparent regions. 如果颜色变化在大量帧中持续存在,则可能还可以创建一个临时纹理,该纹理在透明区域中包含变化的颜色。 This can be achieved easily on the GPU using the above shader and just rendering into this temporary texture (using FBOs ) instead of the display framebuffer. 使用上述着色器在GPU上轻松渲染即可,只需渲染到此临时纹理(使用FBO )而不是显示帧缓冲区即可。

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

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