其他分享
首页 > 其他分享> > Android Opengl 2.0 Alpha混合问题 – 半透明纹理

Android Opengl 2.0 Alpha混合问题 – 半透明纹理

作者:互联网

我在游戏中遇到了Apha Blending的这个问题,当我用alpha纹理绘制一个表面时,看不见的是看不见的,但是看到的部分是半透明的.它取决于光量 – 光源越接近光源,但在阴影中这些物体几乎消失.

我启用Alpha Blending:

GLES20.glEnable(GLES20.GL_BLEND);

然后我设置了功能:

GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

要么

GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);

效果还是一样的.我使用带alpha通道的48位png文件.

我的片段着色器看起来像这样:

final String fragmentShader =   
"precision mediump float; \n"           
    +"uniform vec3 u_LightPos; \n"      
    +"uniform sampler2D u_Texture; \n"  
    +"varying vec3 v_Position;  \n" 
    +"varying vec4 v_Color;  \n"        
    +"varying vec3 v_Normal; \n"        
    +"varying vec2 v_TexCoordinate; \n" 
    +"void main() \n"                           
    +"{          \n"                              
    +"float distance = length(u_LightPos - v_Position); \n"                  
    +"vec3 lightVector = normalize(u_LightPos - v_Position);    \n"                 
    +"float diffuse = max(dot(v_Normal, lightVector), 0.0); \n"                                                                               
    +"diffuse = diffuse * (" + Float.toString((float)Brightness) +" / (1.0 + (0.08 * distance)));   \n"
    +"diffuse = diffuse;    \n"  
    //+3
    +"gl_FragColor = (v_Color * diffuse * texture2D(u_Texture, v_TexCoordinate));   \n"
    +"} \n";   

和顶点着色器:

uniform mat4 u_MVPMatrix;                  
uniform mat4 u_MVMatrix;        
attribute vec4 a_Position;      
attribute vec3 a_Normal;        
attribute vec2 a_TexCoordinate; 
varying vec3 v_Position;    
varying vec4 v_Color;   
varying vec3 v_Normal;  
varying vec2 v_TexCoordinate;             
void main()                                                     
{                                                         
    v_Position = vec3(u_MVMatrix * a_Position);            
    v_Color = a_Color;
    v_TexCoordinate = a_TexCoordinate;                                      
    v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
    gl_Position = u_MVPMatrix * a_Position;                               
}      

 Thx for any suggestions:)          

解决方法:

片段着色器将纹理颜色的所有4(RGBA)分量与漫反射因子相乘.当漫反射光消失时,这会使你的alpha分量趋向于零,使你的精灵几乎看不见.

要修复代码,请将其更改为以下内容:

gl_FragColor = v_Color * texture2D(u_Texture, v_TexCoordinate);
gl_FragColor.rgb *= diffuse;

标签:android,transparent,opengl-es,alpha-transparency
来源: https://codeday.me/bug/20190629/1328375.html