quick cocos2dx 让窗口变灰

Quick-x cocos2dx的各种变灰

  1. 使用quick封装好的滤镜效果filters,源码framework目录下有个filter.lua,直接使用里面的方法就可以了,一般都是对单个Sprite有效,而且只对单个哦

  使用方法:

 变灰:display.newFilteredSprite("a.png", unpack({"GRAY",{0.2, 0.3, 0.5, 0.1}})     )    
 或者先一个nil后面再加图:  local grayImg1 = display.newFilteredSprite( nil  , unpack({"GRAY",{0.2, 0.3, 0.5, 0.1}})  )  
再或者直接使用灰图 local grayImg1 = display.newGraySprite( nil  ,  {0.2, 0.3, 0.5, 0.1}  )  
--以下只对quick 2.2.X下有用 local texture  = cc.TextureCache:sharedTextureCache():addImage( "16.png") 
grayImg1:setTexture(texture)
grayImg1:addTo(node)
grayImg1:setPosition(img1:getPositionX(), img1:getPositionY())
具体有多少种滤镜呢?可以看samples\filters例子 

   MainScene._FILTERS = {
    -- colors
    {"GRAY",{0.2, 0.3, 0.5, 0.1}},
    {"RGB",{1, 0.5, 0.3}},
    {"HUE", {90}},
    {"BRIGHTNESS", {0.3}},
    {"SATURATION", {0}},
    {"CONTRAST", {2}},
    {"EXPOSURE", {2}},
    {"GAMMA", {2}},
    {"HAZE", {0.1, 0.2}},
    --{"SEPIA", {}},
    -- blurs
    {"GAUSSIAN_VBLUR", {7}},
    {"GAUSSIAN_HBLUR", {7}},
    {"ZOOM_BLUR", {4, 0.7, 0.7}},
    {"MOTION_BLUR", {5, 135}},
    -- others
    {"SHARPEN", {1, 1}},
    {{"GRAY", "GAUSSIAN_VBLUR", "GAUSSIAN_HBLUR"}, {nil, {10}, {10}}},
    {{"BRIGHTNESS", "CONTRAST"}, {{0.1}, {4}}},
    {{"HUE", "SATURATION", "BRIGHTNESS"}, {{240}, {1.5}, {-0.4}}},
}

  参考列表:

  • filter源码 GitHub>>>
  • display源码>>>

    上面办法有个缺点就是对一张图来灰化,因为Sprite嘛,对一个容器就无能为力了,要对整个容器,看下面这个方法

    2.setColor 确切来说,变色,不叫变灰,但是效果也是一样的,变成灰色而已, 好处是对他层下的整个容器有效果

    –白色跟任何色混合还是原来的颜色,和黑色变黑,红色和绿色混合会变成黄色 if not found then self._Canvas:setCascadeColorEnabled(true) self._Canvas:setColor(ccc3(80, 80, 80)) else self._Canvas:setColor(display.COLOR_WHITE) end

setCascadeColorEnabled 可以当前这个容器下的一级层起到变色效果,但对更深入和层就无招了,比较适合从CCS导入的容器

3. 使用Shader (着色器),但只有quick 3.X版本才引入了,这东西在cocos2dx里面用到,因为OpenGL嘛, 3D常用的嘛,看这个文章里面的图>>>

local function darkNode(node) 

 local vertDefaultSource = "\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"..
                       "}"

local pszFragSource = "#ifdef GL_ES \n" ..
                      "precision mediump float; \n" ..
                        "#endif \n" ..
                        "varying vec4 v_fragmentColor; \n" ..
                        "varying vec2 v_texCoord; \n" ..
                        "void main(void) \n" ..
                        "{ \n" ..
                         "vec4 c = texture2D(CC_Texture0, v_texCoord); \n" ..
                        "gl_FragColor.xyz = vec3(0.4*c.r + 0.4*c.g +0.4*c.b); \n"..
                        "gl_FragColor.w = c.w; \n"..
                        "}"

local pProgram = cc.GLProgram:createWithByteArrays(vertDefaultSource,pszFragSource)

pProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_POSITION,cc.VERTEX_ATTRIB_POSITION)
pProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_COLOR,cc.VERTEX_ATTRIB_COLOR)
pProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD,cc.VERTEX_ATTRIB_FLAG_TEX_COORDS)
pProgram:link()
pProgram:updateUniforms()
node:setGLProgram(pProgram)

end
local img1 = cc.uiloader:seekNodeByName(node, "Image_34_0")
 darkNode(img1   )

更多有纹理的CCTexture2D \ CCTexturePVR\ CCTextureCache \CCTextureAtlaslink>>>

以上代码来源于网络: – Link1 >>>

Leave a Reply

Your email address will not be published. Required fields are marked *