在代码身上,用心去飞翔

你是我的小啊小code,怎么爱你都不嫌多

cocos2d-js 图片灰度处理

使用shader把图片进行灰度处理,并支持切回原色

潜在bug:如果调用两次灰度处理,则无法进行色彩还原

LayoutConfig.setWidgetEffectWithEnabled = function (widget,enable) {
    if(!widget){return;}
    widget.setTouchEnabled(enable);
    LayoutConfig.setWidgetGrayEffect(widget,!enable);
}
LayoutConfig.setWidgetGrayEffect = function (widget,enable) {
    var shader = LayoutConfig.getShaderMap(enable ? "ui_gray" : "ui_normal");
    if (widget) {
        var children = widget.getChildren();
        for (var i in children) {
            LayoutConfig.setWidgetGrayEffect(children[i], enable);
        }

        if (!widget.getString) {
            widget.setShaderProgram(shader);
        }

        if (widget.getVirtualRenderer) {
            var children = widget.getVirtualRenderer().getChildren();
            for (var i in children) {
                LayoutConfig.setWidgetGrayEffect(children[i], enable);
            }

            if (!widget.getVirtualRenderer().getString) {
                widget.getVirtualRenderer().setShaderProgram(shader);
            }
        }
    }
};
LayoutConfig.getShaderMap = function (n) {
    var _s = this._shaderMap[n];
    if (_s) {
        //cc.log("LayoutConfig.getShaderMap: " + n + " Cache");
        return _s;
    }

    var b_n = "res/shader/" + n;
    var v_n = b_n + ".vsh";
    var f_n = b_n + ".fsh";

    _s = new cc.GLProgram(v_n, f_n);
    _s.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
    _s.addAttribute(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR);
    _s.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
    _s.link();
    _s.updateUniforms();

    this._shaderMap[n] = _s;

    //cc.log("LayoutConfig.getShaderMap: " + n + " New");
    return _s;
};



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhenyu5211314/article/details/52367169
文章标签: cocos2d-x shader 灰度
个人分类: Js
上一篇cocos2d-js 实现观察者模式
下一篇cocos2d Android 适配华为导航栏
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭