Unsharp mask post filter

This is a sharpening filter for use in jME :slight_smile:


slit det med hälsan :stuck_out_tongue:


package mygame;

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;

import com.jme3.texture.Image.Format;

import java.util.ArrayList;



  • @author kwando


    public class USMFilter extends Filter {

    private Material material;

    private Pass horizontalBlur = new Pass();

    private Pass verticalalBlur = new Pass();

    private Material hBlurMat;

    private Material vBlurMat;

    private float blurScale = 1.5f;

    private int width;

    private int height;

    private float amount = 0.2f;

    private float downSamplingFactor = 0.5f;


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

    width = (int)(w

    height = (int)(h*downSamplingFactor);

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

    hBlurMat = new Material(manager, "Common/MatDefs/Blur/HGaussianBlur.j3md");

    vBlurMat = new Material(manager, "Common/MatDefs/Blur/VGaussianBlur.j3md");

    postRenderPasses = new ArrayList<Filter.Pass>();

    horizontalBlur = new Pass() {


    public boolean requiresSceneAsTexture() {

    return true;



    public void beforeRender() {

    hBlurMat.setFloat("Size", width);

    hBlurMat.setFloat("Scale", blurScale);



    horizontalBlur.init(renderManager.getRenderer(), width, height, Format.RGBA8, Format.Depth, 1, hBlurMat);


    //configuring vertical blur pass

    vBlurMat = new Material(manager, "Common/MatDefs/Blur/VGaussianBlur.j3md");

    verticalalBlur = new Pass() {


    public void beforeRender() {

    vBlurMat.setTexture("Texture", horizontalBlur.getRenderedTexture());

    vBlurMat.setFloat("Size", height);

    vBlurMat.setFloat("Scale", blurScale);



    verticalalBlur.init(renderManager.getRenderer(), width, height, Format.RGBA8, Format.Depth, 1, vBlurMat);


    material.setTexture("BlurTexture", verticalalBlur.getRenderedTexture());



    protected Material getMaterial() {

    material.setFloat("Amount", amount);

    return material;


    public float getAmount() {

    return amount;


    public void setAmount(float amount) {

    this.amount = amount;


    public float getBlurScale() {

    return blurScale;


    public void setBlurScale(float blurScale) {

    this.blurScale = blurScale;



MaterialDef UnsharpMask {
MaterialParameters {
    Texture2D Texture
			Texture2D BlurTexture
			Float Amount

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

    WorldParameters {


uniform sampler2D m_Texture; uniform sampler2D m_BlurTexture; uniform float m_Amount;

const vec3 lumConv = vec3(0.27, 0.67, 0.06);

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

	float colorLum = dot(color.rgb, lumConv);
	float blurLum = dot(blured.rgb, lumConv);

	float diff = colorLum-blurLum;
	gl_FragColor = vec4(diff * m_Amount) + color;



nice !

Doing the unsharp mask on the depth buffer instead creates a cheap SSAO effect :slight_smile: will try that later :stuck_out_tongue:

I don’ see any difference on the image. But i suppose it works. :slight_smile:

@kwando said:
Doing the unsharp mask on the depth buffer instead creates a cheap SSAO effect :) will try that later :P

mhhh what do you mean?

That’s nice too!

Works very well from the pics. Looks like we have a shader guru in the room. Watch out people. :wink:

@nehon This paper explains it I think :slight_smile: it’s not real ambient occlusion but can give a nice impression at least. Basicly one does edge detection on the depth buffer and just overlays this on the final image :slight_smile:


@madjack guru is a strong word, unsharp mask is an old technique used already in analog photography :slight_smile: (at least Wikipedia thinks so).

lol Don’t worry with what wikipedia says. You’ve done more shaders than any other normal users here, so in my book that makes you a connaisseur. :wink: [Note that I used the REAL word here, not some crappy anglicized “connoisseur”] /goofing off