cats-oss / android-gpuimage

Android filters based on OpenGL (idea from GPUImage for iOS)
8.98k stars 2.26k forks source link

How to add grain and adjust? #481

Open candrwow opened 4 years ago

candrwow commented 4 years ago

I want add grain and adjust it,how to do it? Thank you.

monxarat commented 4 years ago

You mus extends from GPUImageFilter and override FRAGMENT_SHADER and VERTEX_SHADER and defines some function relationship.

FRAGMENT_SHADER

precision mediump float;

uniform sampler2D background;
uniform float effect;
uniform float time;
uniform vec2 resolution;

varying vec2 screenPosition;

#pragma glslify: grain = require(../)
#pragma glslify: blend = require(glsl-blend-soft-light)
#pragma glslify: luma = require(glsl-luma)

void main() {
    vec3 texColor = texture2D(background, screenPosition).rgb;

    if (effect == 1.0) {
        //some large grain for this demo
        float zoom = 0.35;
        vec3 g = vec3(grain(screenPosition, resolution * zoom, time));

        //get the luminance of the image
        float luminance = luma(texColor);
        vec3 desaturated = vec3(luminance);

        //now blend the noise over top the backround 
        //in our case soft-light looks pretty good
        vec3 color = blend(desaturated, g);

        //and further reduce the noise strength based on some 
        //threshold of the background luminance
        float response = smoothstep(0.05, 0.5, luminance);
        color = mix(color, desaturated, pow(response,2.0));

        gl_FragColor = vec4(color, 1.0);
    } else {
        gl_FragColor = vec4(texColor, 1.0);
    }
}

VERTEX_SHADER

precision mediump float;

attribute vec2 position;
varying vec2 screenPosition;

void main() {
  screenPosition = (position + 1.0) * 0.5;
  screenPosition.y = 1.0 - screenPosition.y;
  gl_Position = vec4(position, 1.0, 1.0);
}

See more http://kodegarden.org/#0e71c4612401e027866b1d7c0cf1113d8ffaa445

aahanverma00710 commented 4 years ago

Cold you please Give Brief example of this I have implemented this but still not working gives me a black image

Here is the code public class GPUImageGrainFilter extends GPUImageFilter { public static final String HIGHLIGHT_SHADOW_FRAGMENT_SHADER = ""+ "precision mediump float;\n" + "\n" + "uniform sampler2D background;\n" + "uniform float effect;\n" + "uniform float time;\n" + "uniform vec2 resolution;\n" + "\n" + "varying vec2 screenPosition;\n" + "\n" + "#pragma glslify: grain = require(../)\n" + "#pragma glslify: blend = require(glsl-blend-soft-light)\n" + "#pragma glslify: luma = require(glsl-luma)\n" + "\n" + "void main() {\n" + " vec3 texColor = texture2D(background, screenPosition).rgb;\n" + "\n" + " if (effect == 1.0) {\n" + " //some large grain for this demo\n" + " float zoom = 0.35;\n" + " vec3 g = vec3(grain(screenPosition, resolution * zoom, time));\n" + "\n" + " //get the luminance of the image\n" + " float luminance = luma(texColor);\n" + " vec3 desaturated = vec3(luminance);\n" + "\n" + " //now blend the noise over top the backround \n" + " //in our case soft-light looks pretty good\n" + " vec3 color = blend(desaturated, g);\n" + "\n" + " //and further reduce the noise strength based on some \n" + " //threshold of the background luminance\n" + " float response = smoothstep(0.05, 0.5, luminance);\n" + " color = mix(color, desaturated, pow(response,2.0));\n" + "\n" + " gl_FragColor = vec4(color, 1.0);\n" + " } else {\n" + " gl_FragColor = vec4(texColor, 1.0);\n" + " }\n" + "}";

public static final String VERTEX_SHADER =""+
        "precision mediump float;\n" +
        "\n" +
        "attribute vec2 position;\n" +
        "varying vec2 screenPosition;\n" +
        "\n" +
        "void main() {\n" +
        "  screenPosition = (position + 1.0) * 0.5;\n" +
        "  screenPosition.y = 1.0 - screenPosition.y;\n" +
        "  gl_Position = vec4(position, 1.0, 1.0);\n" +
        "}";

float grain ;
int grainLocation ;
@Override
public void onInit() {
    super.onInit();
    grainLocation = GLES20.glGetUniformLocation(getProgram(), "glsl-film-grain");
}

@Override
public void onInitialized() {
    super.onInitialized();
    setGrain(grain);

}
public GPUImageGrainFilter() {
    this(2f);
}
public GPUImageGrainFilter(float grain) {
    super(VERTEX_SHADER, HIGHLIGHT_SHADOW_FRAGMENT_SHADER);
    this.grain = grain;
}

public void setGrain(float grain) {
    this.grain = grain;
    setFloat(grainLocation,grain);
}

}

williankl commented 1 year ago

Any news here, was anybody able to make this work? I am getting the black image just as @aahanverma00710. pls halp

VkrDevelops commented 7 months ago

Also getting the same results. Is anybody here who got the solution?