...

7 Likes

Hahah… cool. I think someone was just asking about this sort of processor yesterday.

Sorry to ask noob questions,


  1. why there is 2 frag shader files?(MotionBlur15.frag and MotionBlur.frag)


  2. motion blur doesn’t work on vertex that’s why there is not vertex shader, is my assumption is correct?



    Great work btw! :slight_smile:

Cool, thanks :slight_smile:

NIce! Btw, I would like to see this filter in action with my eyes :)…

@iamcreasy said:
1. why there is 2 frag shader files?(MotionBlur15.frag and MotionBlur.frag)


Because he is supporting two different levels of GLSL.

@iamcreasy said:
2. motion blur doesn't work on vertex that's why there is not vertex shader, is my assumption is correct?


These are post processing effects and so are always done on a full screen quad and the standard post.vert that comes with JME already sets that up fine.
1 Like
@pspeed said:
Hahah... cool. I think someone was just asking about this sort of processor yesterday.


Yepp, I did :P I can supply you with the other version of motion blur if you want (backproject current pixelposition to the last frames pixelposition to determine pixelvelocity in screen space).
@phate666 thanks for building this, now I don't have to :)

this filter is like in many cool FPS games (cryengine games…).



Will it be in the jme core?

@mifth said:
this filter is like in many cool FPS games (cryengine games...).

Will it be in the jme core?

That's very likely.

Thanks @phate666 once again ;)

With so many shaders coming up I am itching to write one of my own! :slight_smile:



@phate666 is it possible to have per object motion blur?

@Phate666

I tried the Filter.

I made some changes if you don’t mind.

Now it’s a stand alone filter.

Also instead of copying a byteBuffer to save the previous frame I copy directly the FrameBuffer (directly on the GPU) : this is way faster and you don’t have to convert the texture from BGRA in the shader as it’s already a RGBA.



I also introduced a frameOffset variable that is the amount of time to wait between the saved frames. This is to make it frame rate independent, because at hi frame rate the effect is less noticeable (or not at all) compared to low frame rates

I have just a 10% performance hit when I enable the filter. which make it a cheap motion blur.

However the effect is not that great (very very simple though), so i’m gonna look around and try to implement a velocity buffer based motion blur and see what it’s like.

I don’t know yet if this one will be integrated in the core.



However this one could be a nice alternative if you want a nice effect with not that much performance hit so i will post the code here.

I found that it gives its best at a steep angle when going forward, it gives a nice speed impression.



MotionBlurFilter.java

[java]

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;

import com.jme3.texture.Image.Format;

import com.jme3.texture.FrameBuffer;

import com.jme3.texture.Texture2D;



/**

  • Simple Motion Blur Filter

    *
  • @author Phate666
  • @version 03.12.2011

    */

    public class MotionBlurFilter extends Filter {



    private Texture2D previous;

    private FrameBuffer prevFb;

    private FrameBuffer prevSceneFb;

    private Renderer renderer;

    private float strength = 0.5f;

    private float frameOffset = 0.04f;

    private float cpt = 0;



    @Override

    protected void initFilter(AssetManager manager,

    RenderManager renderManager, ViewPort vp, int w, int h) {

    renderer = renderManager.getRenderer();



    prevFb = new FrameBuffer(w, h, 1);

    prevFb.setColorBuffer(Format.RGBA8);

    prevFb.setDepthBuffer(Format.Depth);

    previous = new Texture2D(w, h, Format.RGBA8);

    prevFb.setColorTexture(previous);



    material = new Material(manager, “Common/MatDefs/Post/MotionBlur.j3md”);





    material.setTexture(“Previous”, previous);

    material.setFloat(“strength”, strength);

    }



    @Override

    protected void postFrame(RenderManager renderManager, ViewPort viewPort, FrameBuffer prevFilterBuffer, FrameBuffer sceneBuffer) {

    //keeping reference on the scene frame buffer

    prevSceneFb = sceneBuffer;

    }



    @Override

    public void preFrame(float tpf) {

    if (enabled) {

    //computing time since last saved frame

    cpt += tpf;

    if (cpt >= frameOffset) {

    //we reached the frame offset, let’s save the previous frame

    if (prevSceneFb != null) {

    //copying the frmae buffers

    renderer.copyFrameBuffer(prevSceneFb, prevFb);

    }

    //reseting time

    cpt = 0;

    }

    }



    }



    @Override

    public void setEnabled(boolean enabled) {

    super.setEnabled(enabled);

    if (enabled) {

    renderer.copyFrameBuffer(prevSceneFb, prevFb);

    }

    }



    @Override

    protected Material getMaterial() {

    return material;

    }



    public float getStrength() {

    return strength;

    }



    public void setStrength(float strength) {

    this.strength = strength;

    if (material != null) {

    material.setFloat(“strength”, strength);

    }

    }



    public float getFrameOffset() {

    return frameOffset;

    }



    public void setFrameOffset(float frameOffset) {

    this.frameOffset = frameOffset;

    }

    }

    [/java]



    MotionBlur.frag

<br /> uniform sampler2D m_Texture;<br /> uniform sampler2D m_Previous;<br /> uniform float m_strength;<br /> <br /> varying vec2 texCoord;<br /> <br /> void main() {<br /> vec4 current = texture2D(m_Texture,texCoord);<br /> vec4 previous = texture2D(m_Previous,texCoord);<br /> <br /> vec4 final=mix(current, previous, vec4(m_strength,m_strength,m_strength,m_strength));<br /> <br /> gl_FragColor = final;<br /> }<br />



MotionBlur15.frag

<br /> #import "Common/ShaderLib/MultiSample.glsllib"<br /> <br /> uniform COLORTEXTURE m_Texture;<br /> uniform COLORTEXTURE m_Previous;<br /> uniform float m_strength;<br /> <br /> in vec2 texCoord;<br /> <br /> void main() {<br /> vec4 current = texture2D(m_Texture,texCoord);<br /> vec4 previous = texture2D(m_Previous,texCoord);<br /> <br /> vec4 final=mix(current, previous, vec4(m_strength,m_strength,m_strength,m_strength));<br /> <br /> gl_FragColor = final;<br /> }<br />



Usage:

[java]

FilterPostProcessor fpp = new FilterPostProcessor(assetManager);

MotionBlurFilter filter = new MotionBlurFilter();



fpp.addFilter(filter);



viewPort.addProcessor(fpp);

[/java]



the j3md is the same

@nehon , or possibly that one to the JME core?

Cannot find the j3md file needed to use this filter. Could anyone re-post it? Thanks!



edit: i could make it myself:



[java]

MaterialDef MotionBlur {



MaterialParameters {

Texture2D Texture

Texture2D Previous

Float strength

}



Technique {

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

FragmentShader GLSL150: Common/MatDefs/Post/MotionBlur15.frag



WorldParameters {

WorldViewProjectionMatrix

}

}



Technique {

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

FragmentShader GLSL100: Common/MatDefs/Post/MotionBlur.frag



WorldParameters {

WorldViewProjectionMatrix

}

}



Technique FixedFunc {

}

}

[/java]

Anyone know why this doesn’t work anymore? Wanted to test it out.
Getting this error:
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.IllegalArgumentException: Material parameter is not defined: NumSamples

<cite>@nepheline said:</cite> Anyone know why this doesn't work anymore? Wanted to test it out. Getting this error: SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main] java.lang.IllegalArgumentException: Material parameter is not defined: NumSamples

In the Material Def file… a post filter requires:

Texture
DepthTexture (if required by filter)
NumSamples

Um… and one other that escapes me atm.

1 Like
<cite>@t0neg0d said:</cite> In the Material Def file... a post filter requires:

Texture
DepthTexture (if required by filter)
NumSamples

Um… and one other that escapes me atm.


Ah silly me! Thanks.
All the Material Def file needed was:
MaterialParameters {
Int NumSamples
Texture2D Texture
Texture2D Previous
Float strength
}