cocos2dx 水波纹Shader

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto sp = Sprite::create("water.png", Rect(0,0, visibleSize.width, visibleSize.height));
    addChild(sp);
    sp->setPosition(Point(visibleSize/2));

    auto TexCache = Director::getInstance()->getTextureCache();
    auto wave2 = TexCache->addImage("wave1.png");
    auto wave1 = TexCache->addImage("18.jpg");
    wave1->setTexParameters(Texture2D::TexParams{GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT});
    wave2->setTexParameters(Texture2D::TexParams{GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT});
    auto glprogram = GLProgram::createWithFilenames("water.vsh", "water.fsh");
    auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
    sp->setGLProgramState(glprogramstate);

    glprogramstate->setUniformTexture("u_wave1", wave1);
    glprogramstate->setUniformTexture("u_wave2", wave2);
    glprogramstate->setUniformFloat("saturateValue", 1.2);

    sp->setRotation3D(Vec3(-60,0,0));
    return true;
}

#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;

uniform sampler2D u_wave1;
uniform sampler2D u_wave2;
uniform float u_interpolate;
uniform float saturateValue;
float verticalSpeed = 0.3797;
float horizontalSpeed = 0.77;
void main() {
    vec2 textCoord1 = v_texCoord;
    textCoord1.x += verticalSpeed * CC_Time.x;
    textCoord1.y += horizontalSpeed * CC_Time.x;
    vec3 color = texture2D(u_wave1, textCoord1).xyz;
    color += texture2D(u_wave2, v_texCoord).xyz;
    if(color.x > saturateValue)
        color = vec3(1.0);
    else
        color = texture2D(CC_Texture0, v_texCoord).xyz;
    gl_FragColor = vec4(color, 1.0);
}

  

再优化了一下

原文地址:https://www.cnblogs.com/VindyLeong/p/4201675.html