ColorCorrectionFilter [updated with examples]

A cheap little filter that applies a color correction to the rendered scene. It includes 4 predefined colorcorrection matrices.



Mix between monochrome, blue sepia, green sepia, brown sepia and color!



Usage:

[java]

ColorCorrectionFilter ccf = new ColorCorrectionFilter();

fpp.addFilter(ccf); // add to FIlterPostProcessor

ccf.setMonochrome(2f);

ccf.setColor(1f);

[/java]

Settings above will create a colormatrix with 1/3 color and 2/3 monochrome and will give you a sort of washed out image.



[java]

package mygame;



import com.jme3.asset.AssetManager;

import com.jme3.material.Material;

import com.jme3.math.Matrix4f;

import com.jme3.post.Filter;

import com.jme3.renderer.RenderManager;

import com.jme3.renderer.ViewPort;



/**

*

  • @author kwando

    */

    public class ColorCorrectionFilter extends Filter {



    private static Matrix4f MONOCHROME = new Matrix4f(0.299f, 0.587f, 0.184f, 0,

    0.299f, 0.587f, 0.184f, 0,

    0.299f, 0.587f, 0.184f, 0,

    0, 0, 0, 1);

    private static Matrix4f BROWN_SEPIA = new Matrix4f(0.299f, 0.587f, 0.184f, 0.1f,

    0.299f, 0.587f, 0.184f, 0.018f,

    0.299f, 0.587f, 0.184f, -0.090f,

    0, 0, 0, 1);

    private static Matrix4f BLUE_SEPIA = new Matrix4f(0.299f, 0.587f, 0.184f, -0.090f,

    0.299f, 0.587f, 0.184f, 0.018f,

    0.299f, 0.587f, 0.184f, 0.1f,

    0, 0, 0, 1);

    private static Matrix4f GREEN_SEPIA = new Matrix4f(0.299f, 0.f, 0.184f, -0.090f,

    0.299f, 0.587f, 0.184f, 0.1f,

    0.299f, 0.587f, 0.184f, 0.1f,

    0, 0, 0, 1);

    private float monochrome = 0f;

    private float brownSepia = 0;

    private float blueSepia = 0f;

    private float greenSepia = 0;

    private float color = 1f;

    private float contrast = 1;

    private float brightness = 0;

    private float gamma = 1;

    private Matrix4f colorCorrectionMatrix = new Matrix4f();

    private Matrix4f tmp = new Matrix4f();

    private boolean changed = true;



    @Override

    protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {

    material = new Material(manager, "MatDefs/ColorCorrection.j3md");

    }



    private void computeMatrix() {

    MONOCHROME.mult(monochrome, colorCorrectionMatrix);

    colorCorrectionMatrix.addLocal(BROWN_SEPIA.mult(brownSepia, tmp));

    colorCorrectionMatrix.addLocal(BLUE_SEPIA.mult(blueSepia, tmp));

    colorCorrectionMatrix.addLocal(GREEN_SEPIA.mult(greenSepia, tmp));

    colorCorrectionMatrix.addLocal(Matrix4f.IDENTITY.mult(color, tmp));





    float total = monochrome + brownSepia + blueSepia + greenSepia + color;

    colorCorrectionMatrix = colorCorrectionMatrix.mult(1.0f / total);

    }



    public void setColor(float amount) {

    this.color = amount;

    changed = true;

    }



    public void setBlueSepia(float amount) {

    this.blueSepia = amount;

    changed = true;

    }



    public void setGreenSepia(float amount) {

    this.greenSepia = amount;

    changed = true;

    }



    public void setMonochrome(float amount) {

    this.monochrome = amount;

    changed = true;

    }



    public void setMonochromatic() {

    color = 0;

    monochrome = 1;

    blueSepia = 0;

    brownSepia = 0;

    greenSepia = 0;

    }



    @Override

    protected Material getMaterial() {

    if(changed){

    computeMatrix();

    material.setMatrix4("ColorCorrection", colorCorrectionMatrix);

    changed = false;

    }

    material.setFloat("Contrast", contrast);

    material.setFloat("Brightness", brightness);

    material.setFloat("Gamma", gamma);

    return material;

    }



    private void setGamma(float gamma) {

    this.gamma = gamma;

    }



    public void setBrownSepia(float amount) {

    this.brownSepia = amount;

    changed = true;

    }

    }

    [/java]
Code:
MaterialDef ColorCorrection {
MaterialParameters {
    Texture2D Texture
    Matrix4 ColorCorrection
    Float Contrast
    Float Brightness
    Float Gamma
}

Technique {
    VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
    FragmentShader GLSL100: Shaders/ColorCorrection.frag

    WorldParameters {
    }
    Defines {
		}
}

}

Fragment shader.
Code:
uniform sampler2D m_Texture; uniform float g_Time; uniform mat4 m_ColorCorrection; uniform float m_Contrast; uniform float m_Brightness; uniform float m_Gamma;

varying vec2 texCoord;
void main() {
vec4 color = texture2D(m_Texture, texCoord);

gl_FragColor = m_ColorCorrection  * color;
gl_FragColor.r = pow(gl_FragColor.r, m_Gamma);
gl_FragColor.g = pow(gl_FragColor.g, m_Gamma);
gl_FragColor.b = pow(gl_FragColor.b, m_Gamma);

gl_FragColor.rgb = (gl_FragColor.rgb * m_Contrast + m_Brightness);

}

3 Likes

Hmm, color correction is a wrong name for it… will post images as soon as I find a good testscene…

This is the original image, no color modifications.

http://i.imgur.com/8KKPoh.png



These are examples of what can be done with this simple filter :slight_smile:

http://i.imgur.com/G0Z3Hh.png

http://i.imgur.com/ngSAMh.png

http://i.imgur.com/GS0Xth.png

http://i.imgur.com/o4kvFh.png

That’s nice.



Oddly the last two look pretty much the same though.



About the shader, you didn’t post the .vert, so I imagine it’s a basic .vert?

1 Like

It uses the jME standard Post.vert

Code:
Technique { VertexShader GLSL100: Common/MatDefs/Post/Post.vert

Yeah, some of the pics became kind of similar :P. My testscene is also kind of dark which makes the result not as clear...
1 Like

Wow! Good filter!

Do you want to add it to ShaderBlow project?

http://code.google.com/p/jme-glsl-shaders/



ShaderBlow project was downloaded about 300 times already.

@kwando said:
Hmm, color correction is a wrong name for it..

So what did you end up calling this technique? :P

ColorFilteringFilter…

ColoringFilterizationFilter

ToneColoringFilteringFilter



Anyway the third shot looks perfect.

@erlend_sh said:
So what did you end up calling this technique? :P

Maybe call it ColorCoercion. ;)
1 Like
@kwando said:
It uses the jME standard Post.vert
Code:
Technique { VertexShader GLSL100: Common/MatDefs/Post/Post.vert

Yeah, some of the pics became kind of similar :P. My testscene is also kind of dark which makes the result not as clear...


Ah, good. Didn't notice that.
1 Like

I like ColorFilteringFilter :slight_smile:

ColorSpaceFilter?



Though I guess it’s not doing a true color-space transform in many ways it seems close. At any rate, it’s the best I could come up with on short notice.



cool stuff, though.

I saw the list of image space shaders that unity has, and one of my numerous quests is to make our list longer (and better) :stuck_out_tongue:



ColorSpaceFilter is the winner I think :slight_smile: