From 0bdf1ac69b46b2c67148680b68d43a99d04d94de Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 29 Jun 2017 22:50:38 +0800 Subject: [PATCH] Configurable alpha parameter for webgl construction --- cocos2d/core/CCGame.js | 2 +- cocos2d/core/platform/CCMacro.js | 15 +++++++++++++++ cocos2d/shaders/CCGLStateCache.js | 12 ++++++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index e3f1b58f6c..359cd057b8 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -672,7 +672,7 @@ var game = { this._renderContext = cc._renderContext = cc.webglContext = cc.create3DContext(localCanvas, { 'stencil': true, - 'alpha': true, + 'alpha': cc.macro.ENABLE_TRANSPARENT_CANVAS, 'antialias': cc.sys.isMobile }); } diff --git a/cocos2d/core/platform/CCMacro.js b/cocos2d/core/platform/CCMacro.js index 6f5a603041..b2f79ac662 100644 --- a/cocos2d/core/platform/CCMacro.js +++ b/cocos2d/core/platform/CCMacro.js @@ -1908,6 +1908,21 @@ cc.macro = { * @default 64 */ DOWNLOAD_MAX_CONCURRENT: 64, + + /** + * !#en + * Boolean that indicates if the canvas contains an alpha channel, default sets to false for better performance. + * Though if you want to make your canvas background transparent and show other dom elements at the background, + * you can set it to true before `cc.game.run`. + * Web only. + * !#zh + * 用于设置 Canvas 背景是否支持 alpha 通道,默认为 false,这样可以有更高的性能表现。 + * 如果你希望 Canvas 背景是透明的,并显示背后的其他 DOM 元素,你可以在 `cc.game.run` 之前将这个值设为 true。 + * 仅支持 Web + * @property {Boolean} ENABLE_TRANSPARENT_CANVAS + * @default false + */ + ENABLE_TRANSPARENT_CANVAS: false, }; /** diff --git a/cocos2d/shaders/CCGLStateCache.js b/cocos2d/shaders/CCGLStateCache.js index d48aa6f352..3b62ddb972 100644 --- a/cocos2d/shaders/CCGLStateCache.js +++ b/cocos2d/shaders/CCGLStateCache.js @@ -105,14 +105,14 @@ cc.gl.deleteProgram = function (program) { * @param {Number} dfactor */ cc.gl.setBlending = function (sfactor, dfactor) { - var ctx = cc._renderContext; - if ((sfactor === ctx.ONE) && (dfactor === ctx.ZERO)) { - ctx.disable(ctx.BLEND); + var gl = cc._renderContext; + if ((sfactor === gl.ONE) && (dfactor === gl.ZERO)) { + gl.disable(gl.BLEND); } else { - ctx.enable(ctx.BLEND); - cc._renderContext.blendFunc(sfactor,dfactor); + gl.enable(gl.BLEND); + gl.blendFunc(sfactor,dfactor); //TODO need fix for WebGL - //ctx.blendFuncSeparate(ctx.SRC_ALPHA, dfactor, sfactor, dfactor); + // gl.blendFuncSeparate(sfactor, dfactor, sfactor, dfactor); } };