Unsharp mask post filter

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

http://imgur.com/KvV7m

slit det med hälsan :stuck_out_tongue:

[java]

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;



    @Override

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

    width = (int)(w
    downSamplingFactor);

    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() {



    @Override

    public boolean requiresSceneAsTexture() {

    return true;

    }



    @Override

    public void beforeRender() {

    hBlurMat.setFloat("Size", width);

    hBlurMat.setFloat("Scale", blurScale);

    }

    };

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

    postRenderPasses.add(horizontalBlur);



    //configuring vertical blur pass

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

    verticalalBlur = new Pass() {



    @Override

    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);

    postRenderPasses.add(verticalalBlur);



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

    }



    @Override

    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;

    }

    }

    [/java]
Code:
MaterialDef UnsharpMask {
MaterialParameters {
    Texture2D Texture
			Texture2D BlurTexture
			Float Amount
}

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

    WorldParameters {
    }
}

}

Code:
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;

}

4 Likes

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:



http://graphics.uni-konstanz.de/publikationen/2006/unsharp_masking/Luft%20et%20al.%20--%20Image%20Enhancement%20by%20Unsharp%20Masking%20the%20Depth%20Buffer.pdf



@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