Search code examples
androidopengl-es-2.0cocos2d-xshadercocos2d-x-3.0

Why doesn't my simple fragment shader work on iOS but not Android under Cocos2d-x 3.2?


I have modified a working grey_scale fragment shader to change all of the non-transparent pixels to purple. For some reason it works great on iOS but on Android the transparent parts of the image are visible (although mostly transparent). Can anybody see what I am doing wrong?

The working grey_scale shader contains the lines that are commented out. I added the last line.

#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;

void main(void)
{
    vec4 c = texture2D(CC_Texture0, v_texCoord);

    //gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b);
    //gl_FragColor.w = c.w;

    gl_FragColor = vec4(0.5, 0.0, 0.4, c.w);
}

Solution

  • It turns out that I need to apply the alpha to all of the colors:

        gl_FragColor = vec4(0.5*c.w, 0.0*c.w, 0.4*c.w, c.w);
    

    I am sure why the old method didn't work. The image uses premultiplied alpha, so I guess the cocos render assumes it (or was somehow told to use it by TexturePacker). So the shader needs to re-multiply the color values by the alpha in order to behave the same way?