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);
}

  

再优化了一下

posted @   VindyLeong  阅读(4141)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示