Shader传参

java->shader传参

//shader传值
    private static final String UNIFORM_FLASH_PHASE     = "flashPhase";
    private static final String UNIFORM_PROGRESS        = "progress";
    private static final String UNIFORM_FLASHINTENSITY  = "flashIntensity";
    private static final String UNIFORM_FLASHZOOMEFFECT = "flashZoomEffect";
    private static final String UNIFORM_FLASHCOLOR      = "flashColor";
    private  int  handlerflash_phase ;
    private  int  handlerprogress ;
    private  int  handlerflashintensity ;
    private  int  handlerflashzoomeffect ;
    private  int  handlerflashcolor ;

    private float flashPhase;
    private float progress;
    private float flashIntensity;
    private float flashZoomEffect;
    private float r = 1.0f,g = 1.0f,b = 1.0f;

    protected void initShaderHandles() {
        super.initShaderHandles();
        progress = 0;
        flashPhase = 0.6f;
        flashIntensity = 3.0f;
        flashZoomEffect = 0.5f;
        this.r = 1.0f;
        this.g = 1.0f;
        this.b = 1.0f;
        handlerflash_phase =   GLES20.glGetUniformLocation(programHandle,UNIFORM_FLASH_PHASE);
        handlerprogress =   GLES20.glGetUniformLocation(programHandle,UNIFORM_PROGRESS   );
        handlerflashintensity =   GLES20.glGetUniformLocation(programHandle,UNIFORM_FLASHINTENSITY);
        handlerflashzoomeffect =   GLES20.glGetUniformLocation(programHandle,UNIFORM_FLASHZOOMEFFECT);
        handlerflashcolor =   GLES20.glGetUniformLocation(programHandle,UNIFORM_FLASHCOLOR );
        GLES20.glUniform3f(handlerflashcolor, r, g, b );
        GLES20.glUniform1f(handlerprogress, progress);
        GLES20.glUniform1f(handlerflash_phase, flashPhase);
        GLES20.glUniform1f(handlerflashintensity, flashIntensity);
        GLES20.glUniform1f(handlerflashzoomeffect, flashZoomEffect);
}

    @Override
    protected String getFragmentShader() {
        return "precision highp float;\n" +
                "\n" +
                "varying highp vec2 textureCoordinate;\n" +
                "\n" +
                "uniform sampler2D inputImageTexture0;\n" +
                "uniform sampler2D inputImageTexture1;\n" +
                "\n" +
                "uniform float progress;\n" +
                "\n" +
                "uniform float flashPhase; // if 0.0, the image directly turn grayscale, if 0.9, the grayscale transition phase is very important\n" +
                "uniform float flashIntensity;\n" +
                "uniform float flashZoomEffect;\n" +
                "\n" +
                "uniform vec3 flashColor;\n" +
                "\n" +
                "const float flashVelocity = 3.0;\n" +
                "\n" +
                "void main() {\n" +
                "    vec2 p = textureCoordinate;\n" +
                "    vec4 fc = texture2D(inputImageTexture0, p);\n" +
                "    vec4 tc = texture2D(inputImageTexture1, p);\n" +
                "    float intensity = mix(1.0, 2.0*distance(p, vec2(0.5, 0.5)), flashZoomEffect) * flashIntensity * pow(smoothstep(flashPhase, 0.0, distance(0.5, progress)), flashVelocity);\n" +
                "    vec4 c = mix(texture2D(inputImageTexture0, p), texture2D(inputImageTexture1, p), smoothstep(0.5*(1.0-flashPhase), 0.5*(1.0+flashPhase), progress));\n" +
                "    c += intensity * vec4(flashColor, 1.0);\n" +
                "    gl_FragColor = c;\n" +
                "}\n";//super.getFragmentShader();
    }

你可能感兴趣的:(Shader传参)