快速记录一下cocos2dx 3.x 全屏幕 blur 效果实现相关代码

快速记录一下cocos2dx 3.x 全屏幕 blur 效果实现相关代码,第1张

概述1. render texture 相关lua代码: function TestButtonMediator:getBlurRenderTexture() -- body logTimeBegin("getBlurRenderTexture") local size = cc.Director:getInstance():getWinSize() local re

1. render texture 相关lua代码:

function TestbuttonMediator:getBlurRenderTexture()    -- body    logTimeBegin("getBlurRenderTexture")    local size = cc.Director:getInstance():getWinSize()    local rendTexture = cc.RenderTexture:create(size.wIDth,size.height,3,35056) -- #define GL_DEPTH24_STENCIL8 0x88F0 (35056)    rendTexture:setAnchorPoint(cc.p(0,0))    rendTexture:setposition(size.wIDth/2,size.height/2)    rendTexture:begin()    CCDirector:sharedDirector():getRunningScene():visit();    rendTexture:endTolua(false)    local spr =BlurShader:create(rendTexture:getSprite())    spr:setBlurRadius(3)    spr:setSampleNum(3)    spr:getTexture():setAntiAliasTexParameters()    -- local colorVIEw = ccui.Layout:create();    -- colorVIEw:setContentSize(size)    -- colorVIEw:setBackGroundcolor(cc.c3b(0,0))    -- colorVIEw:setBackGroundcolorType(1)    -- colorVIEw:setBackGroundcolorOpacity(250)    -- colorVIEw:setAnchorPoint(cc.p(0,0))    -- colorVIEw:setposition(0,0)    -- rendTexture:addChild(colorVIEw)    logTimeEnd("getBlurRenderTexture")    return rendTexture,sprend


把 render texture 加到 node 上

    local rendTexture,spr = self:getBlurRenderTexture()    self:getVIEw():addChild(rendTexture)


2. shader 相关代码

vs ccShader_positionTexturecolor_noMVP.vert

attribute vec4 a_position;attribute vec2 a_texCoord;attribute vec4 a_color;#ifdef GL_ESvarying lowp vec4 v_fragmentcolor;varying mediump vec2 v_texCoord;#elsevarying vec4 v_fragmentcolor;varying vec2 v_texCoord;#endifvoID main(){    gl_position = CC_PMatrix * a_position;    v_fragmentcolor = a_color;    v_texCoord = a_texCoord;}

fs: example_Blur.fsh

#ifdef GL_ESprecision mediump float;#endifvarying vec4 v_fragmentcolor;varying vec2 v_texCoord;uniform float blurRadius;uniform float sampleNum;uniform float test;uniform vec2 resolution;vec4 blur(vec2);voID main(voID){    vec4 col = blur(v_texCoord); //* v_fragmentcolor.rgb;    gl_Fragcolor = vec4(col) * v_fragmentcolor;}vec4 blur(vec2 p){    //sampleNum = 2.0;    //blurRadius = 3.5;    if (blurRadius > 0.0 && sampleNum > 1.0)    {        vec4 col = vec4(0);        vec4 colaphale = vec4(0);        vec2 unit = 1.0 / resolution.xy;                float r = blurRadius;        float sampleStep = r / sampleNum;                float count = 0.0;                for(float x = -r; x < r; x += sampleStep)        {            for(float y = -r; y < r; y += sampleStep)            {                float weight = (r - abs(x)) * (r - abs(y));                col += texture2D(CC_Texture0,p + vec2(x * unit.x,y * unit.y)) * weight;                count += weight;                #ifdef  USE_ETC                    colaphale += texture2D(CC_Texture1,y * unit.y)) * weight;                #endif            }        }        #ifdef  USE_ETC            col.a = colaphale.r;        #endif        return col / count;    }    vec4 texcolor = texture2D(CC_Texture0,p);    #ifdef  USE_ETC        texcolor = FormatEtc(texcolor);    #endif    return texcolor;}



3. shader lua 交互相关代码

BlurShader = {}function BlurShader:create(sprite)    local shader = Spriteshader:create("BlurShader",sprite:isEtc())    shaderState = sprite:setLuaGLProgram(shader)    sprite.setResolution = function (self,wIDth,height)        shaderState:setUniformVec2("resolution",cc.p(wIDth,height))    end    sprite.setBlurRadius = function (self,radius)        shaderState:setUniformfloat("blurRadius",radius)    end    sprite.setSampleNum = function (self,sampleNum)        shaderState:setUniformfloat("sampleNum",sampleNum)    end    self:setDefaultValue(sprite)    return spriteend
总结

以上是内存溢出为你收集整理的快速记录一下cocos2dx 3.x 全屏幕 blur 效果实现相关代码全部内容,希望文章能够帮你解决快速记录一下cocos2dx 3.x 全屏幕 blur 效果实现相关代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1083483.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存