zoukankan      html  css  js  c++  java
  • 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);
    }

      

    再优化了一下

  • 相关阅读:
    HTML入门学习
    C#结课报告
    Xaml 页面布局学习
    cocos2dx使用pthread注意事项[zt]
    张艾迪(创始人):期待改变世界的力量
    张艾迪(创始人):年少创业与干净的我
    张艾迪(创始人):工作的时候
    张艾迪(创始人):出现在世界224C之前的这些时间
    张艾迪(创始人):世界前三大互联网公司
    张艾迪(创始人):世界前三大互联网巨头公司
  • 原文地址:https://www.cnblogs.com/VindyLeong/p/4201675.html
Copyright © 2011-2022 走看看