• cocos2dx 流光着色器


    添加一个片面着色器

    #ifdef GL_ES
    precision mediump float;
    #endif
    
    varying vec4 v_fragmentColor;
    varying vec2 v_texCoord;
    
    uniform float k;//斜率
    uniform float b;
    
    void main() {
        float flickerWidth = 0.15;//光的宽度
        vec4 outColor = vec4(1, 1, 1, 1);
    
        outColor *= texture2D(CC_Texture0, v_texCoord);
         float y = k * v_texCoord.x + b;
        float y1 = 1.0 - v_texCoord.y;
        if(y1 >= y && y1 <= y + flickerWidth)
        {
            vec4 white = vec4(200, 200, 200, 128);
            outColor *= white;
        }
    
        outColor *= v_fragmentColor;
    
        gl_FragColor = outColor;
    }

    创建一个sprite,并且使用该着色器,其中顶点着色器使用cocos默认的

    _sprite = Sprite::create("Images/grossinis_sister1.png");
    addChild(_sprite);
    _sprite->setPosition(Vec2(s.width / 2, s.height / 2));
    
    GLProgram* glprogram = GLProgram::createWithFilenames("Shaders/MultiTexture.vsh", "Shaders/MultiTexture.fsh");
    auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
    _sprite->setGLProgramState(glprogramstate);
    
    glprogramstate->setUniformFloat("k", 1);//设置45度斜向

    float m_fb = 1.0f;//用于变化y轴位置
    float m_fbMax = 1.0f;
    float m_fbMin = -1.0f;

    创建一个定时器更新b值

    _scheduler->schedule(schedule_selector(ShaderMultiTexture::run), this, 0, false);
    
    
    void ShaderMultiTexture::run(float fDelay)
    {
        auto programState = _sprite->getGLProgramState();
        programState->setUniformFloat("b", m_fb);
    
        m_fb -= 0.03f;
        if (m_fb < m_fbMin)
        {
            m_fb = m_fbMax;
        }
    }

    转载请注明出处,from 博客园 HemJohn

  • 相关阅读:
    SEO搜索引擎
    SEO
    编程的智慧
    ES6编程规范
    面试题集
    motto
    motto
    JS
    motto
    Linux
  • 原文地址:https://www.cnblogs.com/HemJohn/p/13613706.html
Copyright © 2020-2023  润新知