BloomFilter, applied James patch to make sure the filter is properly re initialized when de downsampling factor is changed.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10943 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
experimental
rem..om 11 years ago
parent b77f5a422c
commit f8c317f593
  1. 643
      engine/src/core-effects/com/jme3/post/filters/BloomFilter.java

@ -1,314 +1,329 @@
/* /*
* Copyright (c) 2009-2012 jMonkeyEngine * Copyright (c) 2009-2012 jMonkeyEngine
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
* met: * met:
* *
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.post.filters; package com.jme3.post.filters;
import com.jme3.asset.AssetManager; import com.jme3.asset.AssetManager;
import com.jme3.export.InputCapsule; import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter; import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter; import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule; import com.jme3.export.OutputCapsule;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
import com.jme3.post.Filter; import com.jme3.post.Filter;
import com.jme3.renderer.RenderManager; import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort; import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.RenderQueue; import com.jme3.renderer.queue.RenderQueue;
import com.jme3.texture.Image.Format; import com.jme3.texture.Image.Format;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* BloomFilter is used to make objects in the scene have a glow effect.<br> * BloomFilter is used to make objects in the scene have a glow effect.<br>
* There are 2 mode : Scene and Objects.<br> * There are 2 mode : Scene and Objects.<br>
* Scene mode extracts the bright parts of the scene to make them glow<br> * Scene mode extracts the bright parts of the scene to make them glow<br>
* Object mode make objects glow according to their material's glowMap or their GlowColor<br> * Object mode make objects glow according to their material's glowMap or their GlowColor<br>
* @see <a href="http://jmonkeyengine.org/wiki/doku.php/jme3:advanced:bloom_and_glow">advanced:bloom_and_glow</a> for more details * @see <a href="http://jmonkeyengine.org/wiki/doku.php/jme3:advanced:bloom_and_glow">advanced:bloom_and_glow</a> for more details
* *
* @author Rémy Bouquet aka Nehon * @author Rémy Bouquet aka Nehon
*/ */
public class BloomFilter extends Filter { public class BloomFilter extends Filter {
/** /**
* GlowMode specifies if the glow will be applied to the whole scene,or to objects that have aglow color or a glow map * GlowMode specifies if the glow will be applied to the whole scene,or to objects that have aglow color or a glow map
*/ */
public enum GlowMode { public enum GlowMode {
/** /**
* Apply bloom filter to bright areas in the scene. * Apply bloom filter to bright areas in the scene.
*/ */
Scene, Scene,
/** /**
* Apply bloom only to objects that have a glow map or a glow color. * Apply bloom only to objects that have a glow map or a glow color.
*/ */
Objects, Objects,
/** /**
* Apply bloom to both bright parts of the scene and objects with glow map. * Apply bloom to both bright parts of the scene and objects with glow map.
*/ */
SceneAndObjects; SceneAndObjects;
} }
private GlowMode glowMode = GlowMode.Scene; private GlowMode glowMode = GlowMode.Scene;
//Bloom parameters //Bloom parameters
private float blurScale = 1.5f; private float blurScale = 1.5f;
private float exposurePower = 5.0f; private float exposurePower = 5.0f;
private float exposureCutOff = 0.0f; private float exposureCutOff = 0.0f;
private float bloomIntensity = 2.0f; private float bloomIntensity = 2.0f;
private float downSamplingFactor = 1; private float downSamplingFactor = 1;
private Pass preGlowPass; private Pass preGlowPass;
private Pass extractPass; private Pass extractPass;
private Pass horizontalBlur = new Pass(); private Pass horizontalBlur = new Pass();
private Pass verticalalBlur = new Pass(); private Pass verticalalBlur = new Pass();
private Material extractMat; private Material extractMat;
private Material vBlurMat; private Material vBlurMat;
private Material hBlurMat; private Material hBlurMat;
private int screenWidth; private int screenWidth;
private int screenHeight; private int screenHeight;
private RenderManager renderManager; private RenderManager renderManager;
private ViewPort viewPort; private ViewPort viewPort;
/** private AssetManager assetManager;
* Creates a Bloom filter private int initalWidth;
*/ private int initalHeight;
public BloomFilter() {
super("BloomFilter"); /**
} * Creates a Bloom filter
*/
/** public BloomFilter() {
* Creates the bloom filter with the specific glow mode super("BloomFilter");
* @param glowMode }
*/
public BloomFilter(GlowMode glowMode) { /**
this(); * Creates the bloom filter with the specific glow mode
this.glowMode = glowMode; * @param glowMode
} */
public BloomFilter(GlowMode glowMode) {
@Override this();
protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) { this.glowMode = glowMode;
this.renderManager = renderManager; }
this.viewPort = vp;
screenWidth = (int) Math.max(1, (w / downSamplingFactor)); @Override
screenHeight = (int) Math.max(1, (h / downSamplingFactor)); protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
// System.out.println(screenWidth + " " + screenHeight); this.renderManager = renderManager;
if (glowMode != GlowMode.Scene) { this.viewPort = vp;
preGlowPass = new Pass();
preGlowPass.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth); this.assetManager = manager;
} this.initalWidth = w;
this.initalHeight = h;
postRenderPasses = new ArrayList<Pass>();
//configuring extractPass screenWidth = (int) Math.max(1, (w / downSamplingFactor));
extractMat = new Material(manager, "Common/MatDefs/Post/BloomExtract.j3md"); screenHeight = (int) Math.max(1, (h / downSamplingFactor));
extractPass = new Pass() { // System.out.println(screenWidth + " " + screenHeight);
if (glowMode != GlowMode.Scene) {
@Override preGlowPass = new Pass();
public boolean requiresSceneAsTexture() { preGlowPass.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth);
return true; }
}
postRenderPasses = new ArrayList<Pass>();
@Override //configuring extractPass
public void beforeRender() { extractMat = new Material(manager, "Common/MatDefs/Post/BloomExtract.j3md");
extractMat.setFloat("ExposurePow", exposurePower); extractPass = new Pass() {
extractMat.setFloat("ExposureCutoff", exposureCutOff);
if (glowMode != GlowMode.Scene) { @Override
extractMat.setTexture("GlowMap", preGlowPass.getRenderedTexture()); public boolean requiresSceneAsTexture() {
} return true;
extractMat.setBoolean("Extract", glowMode != GlowMode.Objects); }
}
}; @Override
public void beforeRender() {
extractPass.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, extractMat); extractMat.setFloat("ExposurePow", exposurePower);
postRenderPasses.add(extractPass); extractMat.setFloat("ExposureCutoff", exposureCutOff);
if (glowMode != GlowMode.Scene) {
//configuring horizontal blur pass extractMat.setTexture("GlowMap", preGlowPass.getRenderedTexture());
hBlurMat = new Material(manager, "Common/MatDefs/Blur/HGaussianBlur.j3md"); }
horizontalBlur = new Pass() { extractMat.setBoolean("Extract", glowMode != GlowMode.Objects);
}
@Override };
public void beforeRender() {
hBlurMat.setTexture("Texture", extractPass.getRenderedTexture()); extractPass.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, extractMat);
hBlurMat.setFloat("Size", screenWidth); postRenderPasses.add(extractPass);
hBlurMat.setFloat("Scale", blurScale);
} //configuring horizontal blur pass
}; hBlurMat = new Material(manager, "Common/MatDefs/Blur/HGaussianBlur.j3md");
horizontalBlur = new Pass() {
horizontalBlur.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, hBlurMat);
postRenderPasses.add(horizontalBlur); @Override
public void beforeRender() {
//configuring vertical blur pass hBlurMat.setTexture("Texture", extractPass.getRenderedTexture());
vBlurMat = new Material(manager, "Common/MatDefs/Blur/VGaussianBlur.j3md"); hBlurMat.setFloat("Size", screenWidth);
verticalalBlur = new Pass() { hBlurMat.setFloat("Scale", blurScale);
}
@Override };
public void beforeRender() {
vBlurMat.setTexture("Texture", horizontalBlur.getRenderedTexture()); horizontalBlur.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, hBlurMat);
vBlurMat.setFloat("Size", screenHeight); postRenderPasses.add(horizontalBlur);
vBlurMat.setFloat("Scale", blurScale);
} //configuring vertical blur pass
}; vBlurMat = new Material(manager, "Common/MatDefs/Blur/VGaussianBlur.j3md");
verticalalBlur = new Pass() {
verticalalBlur.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, vBlurMat);
postRenderPasses.add(verticalalBlur); @Override
public void beforeRender() {
vBlurMat.setTexture("Texture", horizontalBlur.getRenderedTexture());
//final material vBlurMat.setFloat("Size", screenHeight);
material = new Material(manager, "Common/MatDefs/Post/BloomFinal.j3md"); vBlurMat.setFloat("Scale", blurScale);
material.setTexture("BloomTex", verticalalBlur.getRenderedTexture()); }
} };
verticalalBlur.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, vBlurMat);
@Override postRenderPasses.add(verticalalBlur);
protected Material getMaterial() {
material.setFloat("BloomIntensity", bloomIntensity);
return material; //final material
} material = new Material(manager, "Common/MatDefs/Post/BloomFinal.j3md");
material.setTexture("BloomTex", verticalalBlur.getRenderedTexture());
@Override }
protected void postQueue(RenderQueue queue) {
if (glowMode != GlowMode.Scene) {
renderManager.getRenderer().setBackgroundColor(ColorRGBA.BlackNoAlpha); protected void reInitFilter() {
renderManager.getRenderer().setFrameBuffer(preGlowPass.getRenderFrameBuffer()); initFilter(assetManager, renderManager, viewPort, initalWidth, initalHeight);
renderManager.getRenderer().clearBuffers(true, true, true); }
renderManager.setForcedTechnique("Glow");
renderManager.renderViewPortQueues(viewPort, false); @Override
renderManager.setForcedTechnique(null); protected Material getMaterial() {
renderManager.getRenderer().setFrameBuffer(viewPort.getOutputFrameBuffer()); material.setFloat("BloomIntensity", bloomIntensity);
} return material;
} }
/** @Override
* returns the bloom intensity protected void postQueue(RenderQueue queue) {
* @return if (glowMode != GlowMode.Scene) {
*/ renderManager.getRenderer().setBackgroundColor(ColorRGBA.BlackNoAlpha);
public float getBloomIntensity() { renderManager.getRenderer().setFrameBuffer(preGlowPass.getRenderFrameBuffer());
return bloomIntensity; renderManager.getRenderer().clearBuffers(true, true, true);
} renderManager.setForcedTechnique("Glow");
renderManager.renderViewPortQueues(viewPort, false);
/** renderManager.setForcedTechnique(null);
* intensity of the bloom effect default is 2.0 renderManager.getRenderer().setFrameBuffer(viewPort.getOutputFrameBuffer());
* @param bloomIntensity }
*/ }
public void setBloomIntensity(float bloomIntensity) {
this.bloomIntensity = bloomIntensity; /**
} * returns the bloom intensity
* @return
/** */
* returns the blur scale public float getBloomIntensity() {
* @return return bloomIntensity;
*/ }
public float getBlurScale() {
return blurScale; /**
} * intensity of the bloom effect default is 2.0
* @param bloomIntensity
/** */
* sets The spread of the bloom default is 1.5f public void setBloomIntensity(float bloomIntensity) {
* @param blurScale this.bloomIntensity = bloomIntensity;
*/ }
public void setBlurScale(float blurScale) {
this.blurScale = blurScale; /**
} * returns the blur scale
* @return
/** */
* returns the exposure cutoff<br> public float getBlurScale() {
* for more details see {@link #setExposureCutOff(float exposureCutOff)} return blurScale;
* @return }
*/
public float getExposureCutOff() { /**
return exposureCutOff; * sets The spread of the bloom default is 1.5f
} * @param blurScale
*/
/** public void setBlurScale(float blurScale) {
* Define the color threshold on which the bloom will be applied (0.0 to 1.0) this.blurScale = blurScale;
* @param exposureCutOff }
*/
public void setExposureCutOff(float exposureCutOff) { /**
this.exposureCutOff = exposureCutOff; * returns the exposure cutoff<br>
} * for more details see {@link #setExposureCutOff(float exposureCutOff)}
* @return
/** */
* returns the exposure power<br> public float getExposureCutOff() {
* form more details see {@link #setExposurePower(float exposurePower)} return exposureCutOff;
* @return }
*/
public float getExposurePower() { /**
return exposurePower; * Define the color threshold on which the bloom will be applied (0.0 to 1.0)
} * @param exposureCutOff
*/
/** public void setExposureCutOff(float exposureCutOff) {
* defines how many time the bloom extracted color will be multiplied by itself. default id 5.0<br> this.exposureCutOff = exposureCutOff;
* a high value will reduce rough edges in the bloom and somhow the range of the bloom area * }
* @param exposurePower
*/ /**
public void setExposurePower(float exposurePower) { * returns the exposure power<br>
this.exposurePower = exposurePower; * form more details see {@link #setExposurePower(float exposurePower)}
} * @return
*/
/** public float getExposurePower() {
* returns the downSampling factor<br> return exposurePower;
* form more details see {@link #setDownSamplingFactor(float downSamplingFactor)} }
* @return
*/ /**
public float getDownSamplingFactor() { * defines how many time the bloom extracted color will be multiplied by itself. default id 5.0<br>
return downSamplingFactor; * a high value will reduce rough edges in the bloom and somhow the range of the bloom area *
} * @param exposurePower
*/
/** public void setExposurePower(float exposurePower) {
* Sets the downSampling factor : the size of the computed texture will be divided by this factor. default is 1 for no downsampling this.exposurePower = exposurePower;
* A 2 value is a good way of widening the blur }
* @param downSamplingFactor
*/ /**
public void setDownSamplingFactor(float downSamplingFactor) { * returns the downSampling factor<br>
this.downSamplingFactor = downSamplingFactor; * form more details see {@link #setDownSamplingFactor(float downSamplingFactor)}
} * @return
*/
@Override public float getDownSamplingFactor() {
public void write(JmeExporter ex) throws IOException { return downSamplingFactor;
super.write(ex); }
OutputCapsule oc = ex.getCapsule(this);
oc.write(glowMode, "glowMode", GlowMode.Scene); /**
oc.write(blurScale, "blurScale", 1.5f); * Sets the downSampling factor : the size of the computed texture will be divided by this factor. default is 1 for no downsampling
oc.write(exposurePower, "exposurePower", 5.0f); * A 2 value is a good way of widening the blur
oc.write(exposureCutOff, "exposureCutOff", 0.0f); * @param downSamplingFactor
oc.write(bloomIntensity, "bloomIntensity", 2.0f); */
oc.write(downSamplingFactor, "downSamplingFactor", 1); public void setDownSamplingFactor(float downSamplingFactor) {
} this.downSamplingFactor = downSamplingFactor;
if (assetManager != null) // dirty isInitialised check
@Override reInitFilter();
public void read(JmeImporter im) throws IOException { }
super.read(im);
InputCapsule ic = im.getCapsule(this); @Override
glowMode = ic.readEnum("glowMode", GlowMode.class, GlowMode.Scene); public void write(JmeExporter ex) throws IOException {
blurScale = ic.readFloat("blurScale", 1.5f); super.write(ex);
exposurePower = ic.readFloat("exposurePower", 5.0f); OutputCapsule oc = ex.getCapsule(this);
exposureCutOff = ic.readFloat("exposureCutOff", 0.0f); oc.write(glowMode, "glowMode", GlowMode.Scene);
bloomIntensity = ic.readFloat("bloomIntensity", 2.0f); oc.write(blurScale, "blurScale", 1.5f);
downSamplingFactor = ic.readFloat("downSamplingFactor", 1); oc.write(exposurePower, "exposurePower", 5.0f);
} oc.write(exposureCutOff, "exposureCutOff", 0.0f);
} oc.write(bloomIntensity, "bloomIntensity", 2.0f);
oc.write(downSamplingFactor, "downSamplingFactor", 1);
}
@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
InputCapsule ic = im.getCapsule(this);
glowMode = ic.readEnum("glowMode", GlowMode.class, GlowMode.Scene);
blurScale = ic.readFloat("blurScale", 1.5f);
exposurePower = ic.readFloat("exposurePower", 5.0f);
exposureCutOff = ic.readFloat("exposureCutOff", 0.0f);
bloomIntensity = ic.readFloat("bloomIntensity", 2.0f);
downSamplingFactor = ic.readFloat("downSamplingFactor", 1);
}
}

Loading…
Cancel
Save