美文网首页
Libgdx直接修该Batch 的 Fragment Shad

Libgdx直接修该Batch 的 Fragment Shad

作者: 贼噶人 | 来源:发表于2019-08-15 11:16 被阅读0次
image.png image.png

 String fragmentShader = "#ifdef GL_ES\n" //
                + "#define LOWP lowp\n" //
                + "precision mediump float;\n" //
                + "#else\n" //
                + "#define LOWP \n" //
                + "#endif\n" //
                + "varying LOWP vec4 v_color;\n" //
                + "varying vec2 v_texCoords;\n" //
                + "uniform sampler2D u_texture;\n" //
                + "void main()\n"//
                + "{\n"
                +"vec2 tex_offset =vec2(1.0/300.0,1.0/300.0);\n"
                +"vec4 orColor=texture2D(u_texture,v_texCoords);\n"
                +"float orAlpha=orColor.a;\n"
                +"float weight[5] = float[] (0.227027f, 0.1945946f, 0.1216216f, 0.054054f,0.016216f);\n"
                +"vec3 color=orColor.rgb*weight[0];\n"
                +"int i = 0;\n"
                +"for(i=1;i<5;i++){\n"
                +"color+=texture2D(u_texture,v_texCoords+vec2(0.0,tex_offset.y * float(i))).rgb*weight[i];\n"
                +"color+=texture2D(u_texture,v_texCoords-vec2(0.0,tex_offset.y * float(i))).rgb*weight[i];\n"
                +"}\n"
                +"gl_FragColor = v_color * vec4(color,orAlpha);\n"
                + "}";


   /** Returns a new instance of the default shader used by SpriteBatch for GL2 when no shader is specified. */
    static public ShaderProgram createDefaultShader () {
        String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
                + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
                + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
                + "uniform mat4 u_projTrans;\n" //
                + "varying vec4 v_color;\n" //
                + "varying vec2 v_texCoords;\n" //
                + "\n" //
                + "void main()\n" //
                + "{\n" //
                + "   v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
                + "   v_color.a = v_color.a * (255.0/254.0);\n" //
                + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
                + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
                + "}\n";
        String fragmentShader = "#ifdef GL_ES\n" //
                + "#define LOWP lowp\n" //
                + "precision mediump float;\n" //
                + "#else\n" //
                + "#define LOWP \n" //
                + "#endif\n" //
                + "varying LOWP vec4 v_color;\n" //
                + "varying vec2 v_texCoords;\n" //
                + "uniform sampler2D u_texture;\n" //
                + "void main()\n"//
                + "{\n" //
                + "vec2 samplingOffset = 1.0/100.0 * (1.0 - v_texCoords) * (0.0f,0.5f);\n"
                + "vec4 fragmentColor = texture2D(u_texture, v_texCoords) * 0.18;\n"
                + "fragmentColor += texture2D(u_texture, v_texCoords + " +
                "samplingOffset) * 0.15;\n"
                +"fragmentColor += texture2D(u_texture, v_texCoords + (2.0 * " +
                "samplingOffset)) *  0.12;\n"
                +"fragmentColor += texture2D(u_texture, v_texCoords + (3.0 * " +
                "samplingOffset)) * 0.09;\n"
                +"fragmentColor += texture2D(u_texture, v_texCoords + (4.0 * " +
                "samplingOffset)) * 0.05;\n"
                +"fragmentColor += texture2D(u_texture, v_texCoords - samplingOffset) * 0.15;\n"
                +"fragmentColor += texture2D(u_texture, v_texCoords - (2.0 * " +
                "samplingOffset)) *  0.12;\n"
                +"fragmentColor += texture2D(u_texture, v_texCoords - (3.0 * " +
                "samplingOffset)) * 0.09;\n"
                +"fragmentColor += texture2D(u_texture, v_texCoords - (4.0 * " +
                "samplingOffset)) * 0.05;\n"
                +"gl_FragColor = fragmentColor;\n" //
                + "}";
        ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
        if (shader.isCompiled() == false) throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());
        return shader;
    }

  public BaseStage() {
        super(new ScalingViewport(Scaling.fillY, width, height)
                , new PolygonSpriteBatch(2000,createDefaultShader()));
        Vector2 v = new Vector2();
        screenToStageCoordinates(v);
        left = v.x;
        right = getWidth() - left;
        sounds = new BaseSound();
    }

相关文章

网友评论

      本文标题:Libgdx直接修该Batch 的 Fragment Shad

      本文链接:https://www.haomeiwen.com/subject/frgajctx.html