Post Processing effect

Hi all i need to send the escene to a shader as a texture, in order to apply a post processing effect, any clue how to do that?
thanks in advance!

Look at TestRenderToTexture

1 Like

Or… you can use the FiilterPostPorcessor and make your own Filter, because it’s what it’s meant for…

1 Like

Hi all thanks for your help, i whant to check with you if i’m going in the right way

  1. i want to implement this effect as a post filter (from scrach), as you recommend me
    (Shader Library) Swirl Post Processing Filter in GLSL | Geeks3D

  2. first of all i create the vertex and pixel shader

  3. i crate the material definition as follow
    [java]
    MaterialDef Swirl {

    MaterialParameters {
    Texture2D Texture
    Float rt_w
    Float rt_h
    }

    Technique {
    VertexShader GLSL100: Shaders/Swirl/Swirl.vert
    FragmentShader GLSL100: Shaders/Swirl/Swirl.frag

     WorldParameters {
         WorldViewProjectionMatrix
     }
    

    }

    Technique FixedFunc {
    }
    }

[/java]

  1. i crate a java class extendend form com.jme3.post.Filter; as follow

[java]

package com.prometeo.post;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.post.Filter;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;

public class Swirl extends Filter {

protected Material swirlMaterial;
protected Pass swirlPass; 

@Override
protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
     
    final int width = w;
    final int hight = h;        

// swirlMaterial = new Material(manager, “Common/MatDefs/Blur/Swirl.j3md”);
swirlPass = new Pass() {

        @Override
        public void beforeRender() {
            swirlMaterial.setTexture("Texture", swirlPass.getRenderedTexture());
            swirlMaterial.setFloat("rt_w", width);
            swirlMaterial.setFloat("rt_h", hight);
        }
    };

    


}

@Override
protected Material getMaterial() {
    throw new UnsupportedOperationException("Not supported yet.");
}

}
[/java]

what do you think it this steps correct, do you see any error en the code? before continue i really appreciate your opinions

thanks in advance!!

@juglarx said: 4) i crate a java class extendend form com.jme3.post.Filter; as follow [java]
@Override
protected Material getMaterial() {
    throw new UnsupportedOperationException("Not supported yet.");
}    

}
[/java]

this method has to return the material. Return your swirlMaterial.

Looking closer at the code.
Don’t use a Pass you really don’t need it there.

See how the ColorOverlayFilter is done, it’s the simplest one and yours is pretty straight forward on the java side at least.
really it should look like that
[java]
package com.prometeo.post;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.post.Filter;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;

public class Swirl extends Filter {

protected Material swirlMaterial;

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

    final int width = w;
    final int hight = h;        

    swirlMaterial = new Material(manager, “Common/MatDefs/Blur/Swirl.j3md”);
    swirlMaterial.setFloat(“rt_w”, width);
    swirlMaterial.setFloat(“rt_h”, hight);
    
}

@Override
protected Material getMaterial() {
   return swirlMaterial;
}

}
[/java]

Thanks nehon, i ran it, but i have an exception “IllegalArgumentException: Material parameter is not defined: NumSamples” and i never difined it, i need to define? can you give me a clue on what happend :frowning:
regards!

@nehon i add NumSamples to the Material def, and it’s pass, but i had a new error when compile the shader in real time, regarding to this lines

[java]
uniform float radius = 200.0;
uniform float angle = 0.8;
uniform vec2 center = vec2(400.0, 300);
[/java]

they say that “can not initializie this” and it true, i never overwrite it from the java code, so i chage the code to the follow

[java]
float radius = 200.0;
float angle = 0.8;
vec2 center = vec2(400.0, 300);
[/java]

now it compile fine But i got an wired effect, taking a look to the code i realize that i never pass the scene as a texture, i men in the frag shader i need to sent tex0, how can i sent this texture from the java code ?

i think this is one of the problem, what do you think ?

It seems you’re not very experimented regarding shaders. That’s fine, you’re trying to learn, but you should start from the basis instead of trying and poking around.
follow those tutorials :http://www.lighthouse3d.com/opengl/glsl/
they offer a great amount of valuable knowledge

Then read this : https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:jme3_shaders
more generally you can read the Materials, Light, Shadow section of this doc https://wiki.jmonkeyengine.org/legacy/doku.php/jme3
(all other section too, it can’t hurt)

then look at this filter :
http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core-effects/com/jme3/post/filters/ColorOverlayFilter.java
and its associated material definition and shaders
http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core-effects/Common/MatDefs/Post/Overlay.j3md
http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core-effects/Common/MatDefs/Post/Overlay.frag

For a starter remove the first technique in the j3md. It’s used for multisampled filters, so let it out for now.
Don’t mind the vert shaders in the definition just reuse the one already declared.
replaces the overlay.frag shader with yours (i guess you already went through his step).

NumSample is mandatory you need to declare it at least in the j3md, just copy paste.
Texture is also mandatory, that’s what will be fed with the scene texture.
you have to declare it in your frag shader like this

uniform sampler2D m_Texture;

Forget about your tex0, this is the texture you have to use.
uniforms can’t be initialized like you did in your previous post, because they are supposed to be external parameters fed to the shader.
From what I gather these values are constants so the second declaration you posted is correct .
Except for one thing
this line

 vec2 center = vec2(400.0, 300);

should be

 vec2 center = vec2(400.0, 300.0);

that’s important and may cause your shader to fail on some hardware if you forget this .0. 300 is an int, 300.0 is a float.
Please make yourself a favor and read the documents I linked, things will be a lot easier.

nehon Thanks fot the links and the reply, i already read it… i’ll come back with new question in a few :slight_smile: regards

hi Nehon, i back with my newby shaders question :stuck_out_tongue: i read all the links as you advice me. I copy the vertex shader from

http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core-effects/Common/MatDefs/Post/Post15.vert

and after that y paste the frag

http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core-effects/Common/MatDefs/Post/Overlay.frag

it is work fine so far, but now i want to make a filter (i’m already have the .java file) and when i put a new frag shader i get a a gray screen and not my scene…

here is the frag

[java]

uniform sampler2D m_Texture;
void main ()
{
vec2 uv = gl_TexCoord[0].xy;
vec4 c = texture2D(m_Texture, uv);

c += texture2D(m_Texture, uv+0.001);
c += texture2D(m_Texture, uv+0.003);
c += texture2D(m_Texture, uv+0.005);
c += texture2D(m_Texture, uv+0.007);
c += texture2D(m_Texture, uv+0.009);
c += texture2D(m_Texture, uv+0.011);

c += texture2D(m_Texture, uv-0.001);
c += texture2D(m_Texture, uv-0.003);
c += texture2D(m_Texture, uv-0.005);
c += texture2D(m_Texture, uv-0.007);
c += texture2D(m_Texture, uv-0.009);
c += texture2D(m_Texture, uv-0.011);

c.rgb = vec3((c.r+c.g+c.b)/3.0);
c = c / 9.5;
gl_FragColor = c;
}

[/java]

i think the problem is in m_Texture and this uniform dosen’t have “scene as a texture” can you give me some clue of what is happening?

kind regards

You don’t have to (and shouldn’t in fact) copy the Post15.vert. You should just refer to the original as-is in your j3md file. You will cut and paste the ColorOverlayFilter.java into your own class. You will cut and past the Overlay.frag (Note: dot FRAG) into your own .frag file and you will cut and paste Overlay.j3md into your own .j3md file. Then you will edit that file to point to your Overlay.frag (Note: dot FRAG) do not change the line that points to Post15.vert or whatever.

Change your version of the ColorOverlayFilter.java to point to your new .j3md. Don’t make any other changes. Just verify that the color overlay filter works in its newly named form before you go messing with other parts.

If, between mine and nehon’s post, you can’t get “your own version of the color overlay filter” working… then shaders are probably too difficult for you right now and you might try looking into them again when you have more general experience.

What pspeed said but don’t do it with Post15.vert. do it with Post.vert. I told you 15 prefixed files are for newer version of glsl and that you should focus on having something working before having something optimized.

Also this can’t work and could explain your grey screen:
[java]
vec2 uv = gl_TexCoord[0].xy;
[/java]
just declare a varying like this before your main function
[java]
varying vec2 texCoord;
[/java]
then in your main do
[java]
vec2 uv = texCoord;
[/java]
And this should work.

Awesome Nehon !!! it’s work!!