美文网首页
Shader笔记2-灰度

Shader笔记2-灰度

作者: work_xiao | 来源:发表于2016-11-30 12:01 被阅读0次

         变灰shader很简单,就是改变了一下rgb的值。0.2126,0.7152,0.0722这几个系数是根据人眼对rgb这三种基本颜色识别的强弱算出来的。

顶点着色器 Grayscale.vert

//顶点着色器

attribute vec4 a_position;   //顶点坐标

attribute vec2 a_texCoord;  //纹理坐标

attribute vec4 a_color;        //顶点颜色

//varying 顶点shader和片段shader之间相互传递的参数。

varying vec4 v_fragmentColor;

varying vec2 v_texCoord;

void main()

{

        gl_Position = CC_PMatrix * a_position;

        v_fragmentColor = a_color;

        v_texCoord = a_texCoord;

}

片段着色器Grayscale.frag

//片段着色器

varying vec4 v_fragmentColor;

varying vec2 v_texCoord;

void main(void)

{

         vec4 c = texture2D(CC_Texture0, v_texCoord);

          //变灰就是改变了一下rgb的值。

          //0.2126,0.7152,0.0722这几个系数据说是根据人眼对rgb这三种基本颜色识别的强弱算出来的。

          gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b);

         gl_FragColor.w = c.w;

}

使用示例

//精灵

auto splash = Sprite::create("HelloWorld.png");

splash->setPosition(100, 100);

this->addChild(splash, 1);

//vert:顶点着色器  frag:片段着色器

auto glprogram = GLProgram::createWithFilenames("Grayscale.vert", "Grayscale.frag");

auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);

splash->setGLProgramState(glprogramstate);

效果

相关文章

网友评论

      本文标题:Shader笔记2-灰度

      本文链接:https://www.haomeiwen.com/subject/hahopttx.html