FilmGrain Post Processing Filter

Just spitting out another filter, was looking for the random thing to use for it, had nothing much to add to that hehe. Took an hour or so but I think a lot of people that have no clue about shaders would love to use this, so I put it on here :wink:

I only added a strength variable, it appears that between 8 and 24 you obtain the best results without producing headaches. If you need more control, just say what you need.



No images this time (what on earth would you expect anyway, most compressions would probably get rid of most of the grain and I don’t see the point in showing black and white dots or tones of gray :wink: )



FilmGrainFilter.java

[java]package shaders;



import com.jme3.asset.AssetManager;

import com.jme3.material.Material;

import com.jme3.post.Filter;

import com.jme3.renderer.RenderManager;

import com.jme3.renderer.Renderer;

import com.jme3.renderer.ViewPort;



/**

  • A Post Processing filter that adds random noise to the final render to mimic
  • film grain.

    *
  • Based on an example from bond1 on thegamecreators:
  • http://forum.thegamecreators.com/?m=forum_view&amp;t=172965&amp;b=24<br />
    

*

  • @author: Roy Straver a.k.a. Baal Garnaal

    /

    public class FilmGrainFilter extends Filter{

    float time = 0.0f;

    float strength = 16.0f;



    public FilmGrainFilter() {

    super("FilmGrainFilter");

    }



    @Override

    public boolean isRequiresDepthTexture() {

    return false;

    }



    @Override

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

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

    material.setFloat("Time", time);

    material.setFloat("Strength", strength);

    }



    @Override

    public Material getMaterial() {

    return material;

    }



    @Override

    public void preRender(RenderManager renderManager, ViewPort viewPort) {

    }



    @Override

    public void cleanUpFilter(Renderer r) {

    }



    @Override

    public void preFrame(float tpf) {

    time += tpf;

    if (material != null)

    material.setFloat("Time", time);

    }



    /

  • Sets strength of the noise in the image

    /

    public void setStrength(float strength){

    this.strength = strength;

    if (material != null)

    material.setFloat("Strength", strength);

    }



    /

  • Returns strength of the noise in the image

    /

    public float getStrength(){

    return strength;

    }

    }[/java]



    FilmGrain.j3md

    [java]MaterialDef FilmGrain {



    MaterialParameters {

    Int NumSamples

    Int NumSamplesDepth

    Texture2D Texture

    Float Time

    Float Strength

    }



    Technique {

    VertexShader GLSL150: Common/MatDefs/Post/Post15.vert

    FragmentShader GLSL150: MatDefs/Post/FilmGrain15.frag



    WorldParameters {

    WorldViewProjectionMatrix

    }

    }



    Technique {

    VertexShader GLSL100: Common/MatDefs/Post/Post.vert

    FragmentShader GLSL100: MatDefs/Post/FilmGrain.frag



    WorldParameters {

    WorldViewProjectionMatrix

    }

    }



    Technique FixedFunc {

    }

    }[/java]



    FilmGrain15.frag

    [java]#import "Common/ShaderLib/MultiSample.glsllib"



    uniform COLORTEXTURE m_Texture;

    varying vec2 texCoord;



    uniform float m_Time;

    uniform float m_Strength;



    void main() {

    // Random, adding values to get rid of edge errors and mods that return 0

    float x = (texCoord.x+4) * (texCoord.y+4) * (m_Time
    10);

    vec4 grain = vec4(mod((mod(x, 13) + 1) * (mod(x, 123) + 1), 0.01)-0.005) * m_Strength;



    gl_FragColor = getColor(m_Texture, texCoord) + grain;

    }[/java]



    FilmGrain.frag

    [java]uniform sampler2D m_Texture;

    varying vec2 texCoord;



    uniform float m_Time;

    uniform float m_Strength;



    void main() {

    // Random, adding values to get rid of edge errors and mods that return 0

    float x = (texCoord.x+4) * (texCoord.y+4) * (m_Time*10);

    vec4 grain = vec4(mod((mod(x, 13) + 1) * (mod(x, 123) + 1), 0.01)-0.005) * m_Strength;



    gl_FragColor = texture2D(m_Texture, texCoord) + grain;

    }[/java]
2 Likes

The shader machine :smiley:

I actually felt sorry for not producing one each day :wink: (Well, not one that I put online anyway)



Edit: Again, it seems like the forum likes to add some extra load around the urls, perhaps you want to remove that if you put it in the core :wink: