Cocos2d-x 3.1.1 Study Notes [15] Shader coloring er, cocos2d-x3.1.1

Source: Internet
Author: User

Cocos2d-x 3.1.1 Study Notes [15] Shader coloring er, cocos2d-x3.1.1


First, two files are required.

Gray. fsh

varying vec4 v_fragmentColor;varying vec2 v_texCoord;void main(){vec4 v_orColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);float gray = dot(v_orColor.rgb, vec3(0.299, 0.587, 0.114));gl_FragColor = vec4(gray, gray, gray, v_orColor.a);}

Gray. vsh

attribute vec4 a_position;attribute vec2 a_texCoord;attribute vec4 a_color;varying vec4 v_fragmentColor;varying vec2 v_texCoord;void main(){    gl_Position = CC_MVPMatrix * a_position;v_fragmentColor = a_color;v_texCoord = a_texCoord;}


Declare a function

void shader(Sprite* sprite);

Implementation Functions

void Nice::shader(Sprite* sprite){    auto glprogram = GLProgram::createWithFilenames("gray.vsh", "gray.fsh");    auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);    sprite->setGLProgramState(glprogramstate);}

In the future, we need to implement this shader effect for a certain genie, so we can directly implement shader (sp ).

    sp_point = Sprite::create("newAlwaysShow.png");    sp_point->setAnchorPoint(Vec2::ZERO);    sp_point->setPosition(Vec2(200, 200));    addChild(sp_point);    shader(sp_point);















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.