From a5fdc07fcace0ad95e83c67dc363b2e5facdba84 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 19 Oct 2016 10:19:14 +0800 Subject: [PATCH] Fixes label wrong blending and improves performance while enabling outline. (#16717) --- cocos/renderer/ccShader_Label_outline.frag | 27 +++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/cocos/renderer/ccShader_Label_outline.frag b/cocos/renderer/ccShader_Label_outline.frag index 3b01c0a365..c4d8311225 100644 --- a/cocos/renderer/ccShader_Label_outline.frag +++ b/cocos/renderer/ccShader_Label_outline.frag @@ -15,13 +15,28 @@ uniform vec4 u_textColor; void main() { vec4 sample = texture2D(CC_Texture0, v_texCoord); - float fontAlpha = sample.a; - float outlineAlpha = sample.r; - if ((fontAlpha + outlineAlpha) > 0.0){ - vec4 color = u_textColor * fontAlpha + u_effectColor * (1.0 - fontAlpha); - gl_FragColor = v_fragmentColor * vec4( color.rgb,max(fontAlpha,outlineAlpha)*color.a); + float fontAlpha = sample.a; // sample.a == 1 indicates text, a == 0 indicates outline + float outlineAlpha = sample.r; // sample.r always > 0 + + if (u_effectColor.a > 0.0) // draw outline + { + if (fontAlpha < 1.0) + { + gl_FragColor = v_fragmentColor * vec4(u_effectColor.rgb, u_effectColor.a * outlineAlpha); + } + else + { + discard; // While drawing outline, text should not be drawn since it will be drawn in next step. + // discard this pixel could improve a little and fix wrong alpha blending if text contains + // alpha channel. + } } - else { + else if (fontAlpha > 0.0) // draw text + { + gl_FragColor = v_fragmentColor * vec4(u_textColor.rgb, u_textColor.a * fontAlpha); + } + else // discard the pixel in texture rectangle which is transparent + { discard; } }