cocos2dx CCSprite高斯模糊 lua实现

不废话 上代码

local vertSource = "\n"..
    "attribute vec4 a_position; \n" ..
    "attribute vec2 a_texCoord; \n" ..
    "attribute vec4 a_color; \n" ..
    "#ifdef GL_ES \n" .. 
    "varying lowp vec4 v_fragmentColor;\n" ..
    "varying mediump vec2 v_texCoord;\n" ..
    "#else \n" ..
    "varying vec4 v_fragmentColor;\n" ..
    "varying vec2 v_texCoord;\n" ..
    "#endif\n" ..
    "void main()\n" ..
    "{\n" .. 
    " gl_Position = CC_PMatrix * a_position;\n"..
    " v_fragmentColor = a_color;\n"..
    " v_texCoord = a_texCoord;\n" ..
"}\n"


local fragSource =  "\n" ..
"#ifdef GL_ES \n" ..
"precision mediump float; \n" ..
"#endif \n" ..
"varying vec4 v_fragmentColor; \n" ..
"varying vec2 v_texCoord; \n" ..
"uniform vec2 resolution; \n" ..
"uniform float blurRadius;\n" ..
"uniform float sampleNum; \n" ..
"vec4 blur(vec2);\n" ..
"\n" ..

"void main(void)\n" ..
"{\n" ..
"    vec4 col = blur(v_texCoord); //* v_fragmentColor.rgb;\n" ..
"    gl_FragColor = vec4(col) * v_fragmentColor;\n" ..
"}\n" ..
"\n" ..

"vec4 blur(vec2 p)\n" ..
"{\n" ..
"    if (blurRadius > 0.0 && sampleNum > 1.0)\n" ..
"    {\n" ..
"        vec4 col = vec4(0);\n" ..
"        vec2 unit = 1.0 / resolution.xy;\n" ..
" \n" ..       
"        float r = blurRadius;\n" ..
"        float sampleStep = r / sampleNum;\n" ..
"\n" ..        
"        float count = 0.0;\n" ..
"\n" ..        
"        for(float x = -r; x < r; x += sampleStep)\n" ..
"        {\n" ..
"            for(float y = -r; y < r; y += sampleStep)\n" ..
"            {\n" ..
"                float weight = (r - abs(x)) * (r - abs(y));\n" ..
"                col += texture2D(CC_Texture0, p + vec2(x * unit.x, y * unit.y)) * weight;\n" ..
"                count += weight;\n" ..
"            }\n" ..
"        }\n" ..
"\n" ..        
"        return col / count;\n" ..
"    }\n" ..
"\n" .. 
"    return texture2D(CC_Texture0, p);\n" ..
"}\n"


local function setShader(spr)
    local maskOpacity = 0.1
    local pProgram = cc.GLProgram:createWithByteArrays(vertSource,fragSource)
    local glprogramstate = zcc.GLProgramState:getOrCreateWithGLProgram(pProgram)

    local size = spr:getTexture():getContentSizeInPixels()
    spr:setGLProgramState(glprogramstate)
    glprogramstate:setUniformVec2("resolution", cc.p(size.width, size.height));
    glprogramstate:setUniformFloat("blurRadius", 10);
    glprogramstate:setUniformFloat("sampleNum", 5)
    glprogramstate:setUniformVec2("pix_size", cc.p(0,0));
end

示例如下

--bg为将要让其模糊的图片
local bg = cc.Sprite:createWithSpriteFrameName("bg.jpg")   
self:addChild(bg)
setShader(bg)

ps: 如果要做切换场景背景模糊建议:
1,获取当前屏幕截图
2,根据当前屏幕截图的Size,创空纹理然后对空纹理进行模糊(setShader)
3,将第二步被模糊后的空纹理,盖在截图层之上,完美
(附上截图函数如下,传入node或sprite)

function getRenderSprite(node, renderSize, noStencil, render, fileName)
    local point = node:getAnchorPoint()
    local pos = cc.p(node:getPosition())
    local ingor = node:isIgnoreAnchorPointForPosition()

    local width, height = 0, 0
    local widthCut, heightCut = 0, 0
    local x, y = 0, 0

    local rect = node:getContentSize()--node:boundingBox()
    width = rect.width
    height = rect.height
    if renderSize ~= nil then
    widthCut = renderSize.width or width
    heightCut = renderSize.height or height
    x = renderSize.x or 0
    y = renderSize.y or 0
    else
    widthCut = width
    heightCut = height
    end
    width = math.max(width, widthCut)
    height = math.max(height, heightCut)
    if width == 0 or height == 0 then 
    logd("getRenderSprite error, width or height is zero")
    end

    local outTexture = render
    if noStencil and outTexture == nil then
    outTexture = cc.RenderTexture:create(width, height)
    elseif outTexture == nil then
    outTexture = cc.RenderTexture:create(width, height, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888, gl.DEPTH24_STENCIL8_OES)
    end

    node:setPosition(cc.p(0, 0))
    node:setAnchorPoint(cc.p(0, 0))

    outTexture:beginWithClear(0, 0, 0, 0, 0, 0)
    node:visit()
    outTexture:endToLua()

    node:setPosition(pos)
    node:setAnchorPoint(point)

    local newSprite = cc.Sprite:create()
    newSprite:setSpriteFrame(outTexture:getSprite():getSpriteFrame())
    newSprite:getTexture():setAntiAliasTexParameters()

    newSprite:setFlippedY(true)
    newSprite:setPosition(pos)
    newSprite:setAnchorPoint(point)
    newSprite:ignoreAnchorPointForPosition(ingor)
    newSprite:setTextureRect(cc.rect(x, y, widthCut, heightCut))

    -- test
        if  fileName then 
       outTexture:saveToFile(fileName, true, nil)
      end
    return newSprite
end
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值