My fragment shader for a mobile game in cocos2dx works on iOS, but does not work as expected on Android and I'm out of ideas.
The idea: u_texture is the input texture of a sprite whose appearance I want to modify. u_texture1 is a "mask" texture that has some transparency and depending on its alpha channel, I want to render the sprite's appearance with some changes or without.
The very minimal example of the problem I'm experiencing:
- This is working "as expected", namely, colours the sprite red where the mask is transparent and yellow where the mask is opaque.
uniform sampler2D u_texture;
uniform sampler2D u_texture1;
varying vec2 v_texCoord;
varying vec2 v_resolution;
void main()
{
vec4 maskCol=texture2D(u_texture1, v_texCoord);
vec4 currentCol = texture2D(u_texture,v_texCoord);
vec4 newCol = currentCol;
if(maskCol.a == 0.0){
newCol.r = 1.0;
newCol.g = 0.0;
newCol.b = 0.0;
newCol.a = 1.0;
} else {
newCol.r = 1.0;
newCol.g = 1.0;
newCol.b = 0.0;
newCol.a = 1.0;
}
gl_FragColor = newCol;
}
- This code I would expect to colour the sprite red where the mask is transparent and leave the rest alone. However, it does not do that. Instead, it colours the sprite red where the INPUT TEXTURE is transparent, and leaves the rest alone.
uniform sampler2D u_texture;
uniform sampler2D u_texture1;
varying vec2 v_texCoord;
varying vec2 v_resolution;
void main()
{
vec4 maskCol=texture2D(u_texture1, v_texCoord);
vec4 currentCol = texture2D(u_texture,v_texCoord);
vec4 newCol = currentCol;
if(maskCol.a == 0.0){
newCol.r = 1.0;
newCol.g = 0.0;
newCol.b = 0.0;
newCol.a = 1.0;
}
gl_FragColor = newCol;
}
Can anyone see anything wrong with this code? Apart from equality comparing the alpha value - which is not the problem here, I have checked, but I’m leaving it for simplicity.
Does v_resolution do anything?
glsl is fine.
Two ways to check:
1.Make sure that u_texture1 is a mask texture. Is the texture passing reversed?
2.The first glsl is correct because each newcol has a color. But the second glsl is not, so you can try to output newcol directly and see if the result is expected?
2022-12-29
I copied this glsl.
right is:
shader is:
texture1 is:
texture2 is
error is:
maybe try to change texture glTexParameteri or other Settings for textures!