shader模糊

2020-05-11  本文已影响0人  园Flora

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"

--图片模糊效果 spr图片 Radius虚化等级

function UICommon:setShader(spr,Radius)

    local maskOpacity = 0.1

    local pProgram = cc.GLProgram:createWithByteArrays(vertSource,fragSource)

    local glprogramstate = cc.GLProgramState:getOrCreateWithGLProgram(pProgram)

    local size = spr:getTexture():getContentSizeInPixels()

    spr:setGLProgramState(glprogramstate)

    glprogramstate:setUniformVec2("resolution", cc.p(size.width, size.height));

    glprogramstate:setUniformFloat("blurRadius", Radius);

    glprogramstate:setUniformFloat("sampleNum", 1.5)

    glprogramstate:setUniformVec2("pix_size", cc.p(0,0));

end

上一篇下一篇

猜你喜欢

热点阅读