Hi,
Looking for how to achieve effects using shaders, I came to create a grayscale filter. If anyone wants to use it, feel free and do it!!
GrayScale Filter On
Filter: GrayScaleFilter.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.ViewPort;
public class GrayScaleFilter extends Filter {
@Override
protected Material getMaterial() {
return this.material;
}
@Override
protected void initFilter(final AssetManager manager, final RenderManager renderManager, final ViewPort vp,
final int w, final int h) {
this.material = new Material(manager, “Common/MatDefs/Post/GrayScale.j3md”);
}
}
[/java]
Material Def: GrayScale.j3md
[java]
MaterialDef GrayScale {
MaterialParameters {
Int NumSamples
Texture2D Texture
}
Technique {
VertexShader GLSL150: Common/MatDefs/Post/Post15.vert
FragmentShader GLSL150: Common/MatDefs/Post/GrayScale.frag
WorldParameters {
WorldViewProjectionMatrix
}
}
Technique {
VertexShader GLSL100: Common/MatDefs/Post/Post.vert
FragmentShader GLSL100: Common/MatDefs/Post/GrayScale.frag
WorldParameters {
WorldViewProjectionMatrix
}
}
}
[/java]
Frag Shader: GrayScale.frag
[java]
uniform sampler2D m_Texture;
varying vec2 texCoord;
void main() {
// Convert to grayscale
vec3 colour = texture2D(m_Texture, texCoord).xyz;
float gray = (colour.x + colour.y + colour.z) / 3.0;
vec3 grayscale = vec3(gray);
gl_FragColor = vec4(grayscale, 1.0);
}
[/java]
Test Case: TestGrayScale.java
[java]
import java.io.File;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.HttpZipLocator;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.util.SkyFactory;
public class TestGrayScale extends SimpleApplication {
private FilterPostProcessor fpp;
private boolean enabled = true;
private GrayScaleFilter grayScale;
// set default for applets
private static boolean useHttp = true;
public static void main(final String[] args) {
final File file = new File(“wildhouse.zip”);
if (file.exists()) {
TestGrayScale.useHttp = false;
}
final TestGrayScale app = new TestGrayScale();
app.start();
}
@Override
public void simpleInitApp() {
this.flyCam.setMoveSpeed(10);
final Node mainScene = new Node();
this.cam.setLocation(new Vector3f(-27.0f, 1.0f, 75.0f));
this.cam.setRotation(new Quaternion(0.03f, 0.9f, 0f, 0.4f));
// load sky
mainScene.attachChild(SkyFactory.createSky(this.assetManager, “Textures/Sky/Bright/BrightSky.dds”, false));
// create the geometry and attach it
// load the level from zip or http zip
if (TestGrayScale.useHttp) {
this.assetManager.registerLocator(“http://jmonkeyengine.googlecode.com/files/wildhouse.zip”,
HttpZipLocator.class);
} else {
this.assetManager.registerLocator(“wildhouse.zip”, ZipLocator.class);
}
final Spatial scene = this.assetManager.loadModel(“main.scene”);
final DirectionalLight sun = new DirectionalLight();
final Vector3f lightDir = new Vector3f(-0.37352666f, -0.50444174f, -0.7784704f);
sun.setDirection(lightDir);
sun.setColor(ColorRGBA.White.clone().multLocal(2));
scene.addLight(sun);
mainScene.attachChild(scene);
this.rootNode.attachChild(mainScene);
this.fpp = new FilterPostProcessor(this.assetManager);
this.fpp.setNumSamples(4);
this.grayScale = new GrayScaleFilter();
this.fpp.addFilter(this.grayScale);
this.viewPort.addProcessor(this.fpp);
this.initInputs();
}
private void initInputs() {
this.inputManager.addMapping(“toggle”, new KeyTrigger(KeyInput.KEY_SPACE));
final ActionListener acl = new ActionListener() {
@Override
public void onAction(final String name, final boolean keyPressed, final float tpf) {
if (name.equals(“toggle”) && keyPressed) {
if (TestGrayScale.this.enabled) {
TestGrayScale.this.enabled = false;
TestGrayScale.this.viewPort.removeProcessor(TestGrayScale.this.fpp);
} else {
TestGrayScale.this.enabled = true;
TestGrayScale.this.viewPort.addProcessor(TestGrayScale.this.fpp);
}
}
}
};
this.inputManager.addListener(acl, “toggle”);
}
}
[/java]