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:
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;
}
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:
unsigned int texture1, texture2;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
shader is:
vec4 maskCol=texture2D(texture2, TexCoord);
vec4 currentCol = texture2D(texture1,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;
}
FragColor = newCol;
texture1 is:
texture2 is
error is:
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
maybe try to change texture glTexParameteri or other Settings for textures!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.