update stable to 3.0.6, see http://hub.jmonkeyengine.org/forum/topic/candidate-changes-for-3-0-6/
git-svn-id: https://jmonkeyengine.googlecode.com/svn/branches/3.0final@11112 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
fb02f3d95c
commit
8ff93b862b
@ -1,314 +1,329 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
package com.jme3.post.filters; |
||||
|
||||
import com.jme3.asset.AssetManager; |
||||
import com.jme3.export.InputCapsule; |
||||
import com.jme3.export.JmeExporter; |
||||
import com.jme3.export.JmeImporter; |
||||
import com.jme3.export.OutputCapsule; |
||||
import com.jme3.material.Material; |
||||
import com.jme3.math.ColorRGBA; |
||||
import com.jme3.post.Filter; |
||||
import com.jme3.renderer.RenderManager; |
||||
import com.jme3.renderer.ViewPort; |
||||
import com.jme3.renderer.queue.RenderQueue; |
||||
import com.jme3.texture.Image.Format; |
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
|
||||
/** |
||||
* BloomFilter is used to make objects in the scene have a glow effect.<br> |
||||
* There are 2 mode : Scene and Objects.<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> |
||||
* @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 |
||||
*/ |
||||
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 |
||||
*/ |
||||
public enum GlowMode { |
||||
|
||||
/** |
||||
* Apply bloom filter to bright areas in the scene. |
||||
*/ |
||||
Scene, |
||||
/** |
||||
* Apply bloom only to objects that have a glow map or a glow color. |
||||
*/ |
||||
Objects, |
||||
/** |
||||
* Apply bloom to both bright parts of the scene and objects with glow map. |
||||
*/ |
||||
SceneAndObjects; |
||||
} |
||||
|
||||
private GlowMode glowMode = GlowMode.Scene; |
||||
//Bloom parameters
|
||||
private float blurScale = 1.5f; |
||||
private float exposurePower = 5.0f; |
||||
private float exposureCutOff = 0.0f; |
||||
private float bloomIntensity = 2.0f; |
||||
private float downSamplingFactor = 1; |
||||
private Pass preGlowPass; |
||||
private Pass extractPass; |
||||
private Pass horizontalBlur = new Pass(); |
||||
private Pass verticalalBlur = new Pass(); |
||||
private Material extractMat; |
||||
private Material vBlurMat; |
||||
private Material hBlurMat; |
||||
private int screenWidth; |
||||
private int screenHeight; |
||||
private RenderManager renderManager; |
||||
private ViewPort viewPort; |
||||
|
||||
/** |
||||
* Creates a Bloom filter |
||||
*/ |
||||
public BloomFilter() { |
||||
super("BloomFilter"); |
||||
} |
||||
|
||||
/** |
||||
* Creates the bloom filter with the specific glow mode |
||||
* @param glowMode |
||||
*/ |
||||
public BloomFilter(GlowMode glowMode) { |
||||
this(); |
||||
this.glowMode = glowMode; |
||||
} |
||||
|
||||
@Override |
||||
protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) { |
||||
this.renderManager = renderManager; |
||||
this.viewPort = vp; |
||||
screenWidth = (int) Math.max(1, (w / downSamplingFactor)); |
||||
screenHeight = (int) Math.max(1, (h / downSamplingFactor)); |
||||
// System.out.println(screenWidth + " " + screenHeight);
|
||||
if (glowMode != GlowMode.Scene) { |
||||
preGlowPass = new Pass(); |
||||
preGlowPass.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth); |
||||
} |
||||
|
||||
postRenderPasses = new ArrayList<Pass>(); |
||||
//configuring extractPass
|
||||
extractMat = new Material(manager, "Common/MatDefs/Post/BloomExtract.j3md"); |
||||
extractPass = new Pass() { |
||||
|
||||
@Override |
||||
public boolean requiresSceneAsTexture() { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public void beforeRender() { |
||||
extractMat.setFloat("ExposurePow", exposurePower); |
||||
extractMat.setFloat("ExposureCutoff", exposureCutOff); |
||||
if (glowMode != GlowMode.Scene) { |
||||
extractMat.setTexture("GlowMap", preGlowPass.getRenderedTexture()); |
||||
} |
||||
extractMat.setBoolean("Extract", glowMode != GlowMode.Objects); |
||||
} |
||||
}; |
||||
|
||||
extractPass.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, extractMat); |
||||
postRenderPasses.add(extractPass); |
||||
|
||||
//configuring horizontal blur pass
|
||||
hBlurMat = new Material(manager, "Common/MatDefs/Blur/HGaussianBlur.j3md"); |
||||
horizontalBlur = new Pass() { |
||||
|
||||
@Override |
||||
public void beforeRender() { |
||||
hBlurMat.setTexture("Texture", extractPass.getRenderedTexture()); |
||||
hBlurMat.setFloat("Size", screenWidth); |
||||
hBlurMat.setFloat("Scale", blurScale); |
||||
} |
||||
}; |
||||
|
||||
horizontalBlur.init(renderManager.getRenderer(), screenWidth, screenHeight, 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", screenHeight); |
||||
vBlurMat.setFloat("Scale", blurScale); |
||||
} |
||||
}; |
||||
|
||||
verticalalBlur.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, vBlurMat); |
||||
postRenderPasses.add(verticalalBlur); |
||||
|
||||
|
||||
//final material
|
||||
material = new Material(manager, "Common/MatDefs/Post/BloomFinal.j3md"); |
||||
material.setTexture("BloomTex", verticalalBlur.getRenderedTexture()); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected Material getMaterial() { |
||||
material.setFloat("BloomIntensity", bloomIntensity); |
||||
return material; |
||||
} |
||||
|
||||
@Override |
||||
protected void postQueue(RenderQueue queue) { |
||||
if (glowMode != GlowMode.Scene) { |
||||
renderManager.getRenderer().setBackgroundColor(ColorRGBA.BlackNoAlpha); |
||||
renderManager.getRenderer().setFrameBuffer(preGlowPass.getRenderFrameBuffer()); |
||||
renderManager.getRenderer().clearBuffers(true, true, true); |
||||
renderManager.setForcedTechnique("Glow"); |
||||
renderManager.renderViewPortQueues(viewPort, false); |
||||
renderManager.setForcedTechnique(null); |
||||
renderManager.getRenderer().setFrameBuffer(viewPort.getOutputFrameBuffer()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* returns the bloom intensity |
||||
* @return |
||||
*/ |
||||
public float getBloomIntensity() { |
||||
return bloomIntensity; |
||||
} |
||||
|
||||
/** |
||||
* intensity of the bloom effect default is 2.0 |
||||
* @param bloomIntensity |
||||
*/ |
||||
public void setBloomIntensity(float bloomIntensity) { |
||||
this.bloomIntensity = bloomIntensity; |
||||
} |
||||
|
||||
/** |
||||
* returns the blur scale |
||||
* @return |
||||
*/ |
||||
public float getBlurScale() { |
||||
return blurScale; |
||||
} |
||||
|
||||
/** |
||||
* sets The spread of the bloom default is 1.5f |
||||
* @param blurScale |
||||
*/ |
||||
public void setBlurScale(float blurScale) { |
||||
this.blurScale = blurScale; |
||||
} |
||||
|
||||
/** |
||||
* returns the exposure cutoff<br> |
||||
* for more details see {@link #setExposureCutOff(float exposureCutOff)} |
||||
* @return |
||||
*/ |
||||
public float getExposureCutOff() { |
||||
return exposureCutOff; |
||||
} |
||||
|
||||
/** |
||||
* Define the color threshold on which the bloom will be applied (0.0 to 1.0) |
||||
* @param exposureCutOff |
||||
*/ |
||||
public void setExposureCutOff(float exposureCutOff) { |
||||
this.exposureCutOff = exposureCutOff; |
||||
} |
||||
|
||||
/** |
||||
* returns the exposure power<br> |
||||
* form more details see {@link #setExposurePower(float exposurePower)} |
||||
* @return |
||||
*/ |
||||
public float getExposurePower() { |
||||
return exposurePower; |
||||
} |
||||
|
||||
/** |
||||
* defines how many time the bloom extracted color will be multiplied by itself. default id 5.0<br> |
||||
* 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) { |
||||
this.exposurePower = exposurePower; |
||||
} |
||||
|
||||
/** |
||||
* returns the downSampling factor<br> |
||||
* form more details see {@link #setDownSamplingFactor(float downSamplingFactor)} |
||||
* @return |
||||
*/ |
||||
public float getDownSamplingFactor() { |
||||
return downSamplingFactor; |
||||
} |
||||
|
||||
/** |
||||
* Sets the downSampling factor : the size of the computed texture will be divided by this factor. default is 1 for no downsampling |
||||
* A 2 value is a good way of widening the blur |
||||
* @param downSamplingFactor |
||||
*/ |
||||
public void setDownSamplingFactor(float downSamplingFactor) { |
||||
this.downSamplingFactor = downSamplingFactor; |
||||
} |
||||
|
||||
@Override |
||||
public void write(JmeExporter ex) throws IOException { |
||||
super.write(ex); |
||||
OutputCapsule oc = ex.getCapsule(this); |
||||
oc.write(glowMode, "glowMode", GlowMode.Scene); |
||||
oc.write(blurScale, "blurScale", 1.5f); |
||||
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); |
||||
} |
||||
} |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
package com.jme3.post.filters; |
||||
|
||||
import com.jme3.asset.AssetManager; |
||||
import com.jme3.export.InputCapsule; |
||||
import com.jme3.export.JmeExporter; |
||||
import com.jme3.export.JmeImporter; |
||||
import com.jme3.export.OutputCapsule; |
||||
import com.jme3.material.Material; |
||||
import com.jme3.math.ColorRGBA; |
||||
import com.jme3.post.Filter; |
||||
import com.jme3.renderer.RenderManager; |
||||
import com.jme3.renderer.ViewPort; |
||||
import com.jme3.renderer.queue.RenderQueue; |
||||
import com.jme3.texture.Image.Format; |
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
|
||||
/** |
||||
* BloomFilter is used to make objects in the scene have a glow effect.<br> |
||||
* There are 2 mode : Scene and Objects.<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> |
||||
* @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 |
||||
*/ |
||||
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 |
||||
*/ |
||||
public enum GlowMode { |
||||
|
||||
/** |
||||
* Apply bloom filter to bright areas in the scene. |
||||
*/ |
||||
Scene, |
||||
/** |
||||
* Apply bloom only to objects that have a glow map or a glow color. |
||||
*/ |
||||
Objects, |
||||
/** |
||||
* Apply bloom to both bright parts of the scene and objects with glow map. |
||||
*/ |
||||
SceneAndObjects; |
||||
} |
||||
|
||||
private GlowMode glowMode = GlowMode.Scene; |
||||
//Bloom parameters
|
||||
private float blurScale = 1.5f; |
||||
private float exposurePower = 5.0f; |
||||
private float exposureCutOff = 0.0f; |
||||
private float bloomIntensity = 2.0f; |
||||
private float downSamplingFactor = 1; |
||||
private Pass preGlowPass; |
||||
private Pass extractPass; |
||||
private Pass horizontalBlur = new Pass(); |
||||
private Pass verticalalBlur = new Pass(); |
||||
private Material extractMat; |
||||
private Material vBlurMat; |
||||
private Material hBlurMat; |
||||
private int screenWidth; |
||||
private int screenHeight; |
||||
private RenderManager renderManager; |
||||
private ViewPort viewPort; |
||||
|
||||
private AssetManager assetManager; |
||||
private int initalWidth; |
||||
private int initalHeight; |
||||
|
||||
/** |
||||
* Creates a Bloom filter |
||||
*/ |
||||
public BloomFilter() { |
||||
super("BloomFilter"); |
||||
} |
||||
|
||||
/** |
||||
* Creates the bloom filter with the specific glow mode |
||||
* @param glowMode |
||||
*/ |
||||
public BloomFilter(GlowMode glowMode) { |
||||
this(); |
||||
this.glowMode = glowMode; |
||||
} |
||||
|
||||
@Override |
||||
protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) { |
||||
this.renderManager = renderManager; |
||||
this.viewPort = vp; |
||||
|
||||
this.assetManager = manager; |
||||
this.initalWidth = w; |
||||
this.initalHeight = h; |
||||
|
||||
screenWidth = (int) Math.max(1, (w / downSamplingFactor)); |
||||
screenHeight = (int) Math.max(1, (h / downSamplingFactor)); |
||||
// System.out.println(screenWidth + " " + screenHeight);
|
||||
if (glowMode != GlowMode.Scene) { |
||||
preGlowPass = new Pass(); |
||||
preGlowPass.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth); |
||||
} |
||||
|
||||
postRenderPasses = new ArrayList<Pass>(); |
||||
//configuring extractPass
|
||||
extractMat = new Material(manager, "Common/MatDefs/Post/BloomExtract.j3md"); |
||||
extractPass = new Pass() { |
||||
|
||||
@Override |
||||
public boolean requiresSceneAsTexture() { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public void beforeRender() { |
||||
extractMat.setFloat("ExposurePow", exposurePower); |
||||
extractMat.setFloat("ExposureCutoff", exposureCutOff); |
||||
if (glowMode != GlowMode.Scene) { |
||||
extractMat.setTexture("GlowMap", preGlowPass.getRenderedTexture()); |
||||
} |
||||
extractMat.setBoolean("Extract", glowMode != GlowMode.Objects); |
||||
} |
||||
}; |
||||
|
||||
extractPass.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, extractMat); |
||||
postRenderPasses.add(extractPass); |
||||
|
||||
//configuring horizontal blur pass
|
||||
hBlurMat = new Material(manager, "Common/MatDefs/Blur/HGaussianBlur.j3md"); |
||||
horizontalBlur = new Pass() { |
||||
|
||||
@Override |
||||
public void beforeRender() { |
||||
hBlurMat.setTexture("Texture", extractPass.getRenderedTexture()); |
||||
hBlurMat.setFloat("Size", screenWidth); |
||||
hBlurMat.setFloat("Scale", blurScale); |
||||
} |
||||
}; |
||||
|
||||
horizontalBlur.init(renderManager.getRenderer(), screenWidth, screenHeight, 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", screenHeight); |
||||
vBlurMat.setFloat("Scale", blurScale); |
||||
} |
||||
}; |
||||
|
||||
verticalalBlur.init(renderManager.getRenderer(), screenWidth, screenHeight, Format.RGBA8, Format.Depth, 1, vBlurMat); |
||||
postRenderPasses.add(verticalalBlur); |
||||
|
||||
|
||||
//final material
|
||||
material = new Material(manager, "Common/MatDefs/Post/BloomFinal.j3md"); |
||||
material.setTexture("BloomTex", verticalalBlur.getRenderedTexture()); |
||||
} |
||||
|
||||
|
||||
protected void reInitFilter() { |
||||
initFilter(assetManager, renderManager, viewPort, initalWidth, initalHeight); |
||||
} |
||||
|
||||
@Override |
||||
protected Material getMaterial() { |
||||
material.setFloat("BloomIntensity", bloomIntensity); |
||||
return material; |
||||
} |
||||
|
||||
@Override |
||||
protected void postQueue(RenderQueue queue) { |
||||
if (glowMode != GlowMode.Scene) { |
||||
renderManager.getRenderer().setBackgroundColor(ColorRGBA.BlackNoAlpha); |
||||
renderManager.getRenderer().setFrameBuffer(preGlowPass.getRenderFrameBuffer()); |
||||
renderManager.getRenderer().clearBuffers(true, true, true); |
||||
renderManager.setForcedTechnique("Glow"); |
||||
renderManager.renderViewPortQueues(viewPort, false); |
||||
renderManager.setForcedTechnique(null); |
||||
renderManager.getRenderer().setFrameBuffer(viewPort.getOutputFrameBuffer()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* returns the bloom intensity |
||||
* @return |
||||
*/ |
||||
public float getBloomIntensity() { |
||||
return bloomIntensity; |
||||
} |
||||
|
||||
/** |
||||
* intensity of the bloom effect default is 2.0 |
||||
* @param bloomIntensity |
||||
*/ |
||||
public void setBloomIntensity(float bloomIntensity) { |
||||
this.bloomIntensity = bloomIntensity; |
||||
} |
||||
|
||||
/** |
||||
* returns the blur scale |
||||
* @return |
||||
*/ |
||||
public float getBlurScale() { |
||||
return blurScale; |
||||
} |
||||
|
||||
/** |
||||
* sets The spread of the bloom default is 1.5f |
||||
* @param blurScale |
||||
*/ |
||||
public void setBlurScale(float blurScale) { |
||||
this.blurScale = blurScale; |
||||
} |
||||
|
||||
/** |
||||
* returns the exposure cutoff<br> |
||||
* for more details see {@link #setExposureCutOff(float exposureCutOff)} |
||||
* @return |
||||
*/ |
||||
public float getExposureCutOff() { |
||||
return exposureCutOff; |
||||
} |
||||
|
||||
/** |
||||
* Define the color threshold on which the bloom will be applied (0.0 to 1.0) |
||||
* @param exposureCutOff |
||||
*/ |
||||
public void setExposureCutOff(float exposureCutOff) { |
||||
this.exposureCutOff = exposureCutOff; |
||||
} |
||||
|
||||
/** |
||||
* returns the exposure power<br> |
||||
* form more details see {@link #setExposurePower(float exposurePower)} |
||||
* @return |
||||
*/ |
||||
public float getExposurePower() { |
||||
return exposurePower; |
||||
} |
||||
|
||||
/** |
||||
* defines how many time the bloom extracted color will be multiplied by itself. default id 5.0<br> |
||||
* 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) { |
||||
this.exposurePower = exposurePower; |
||||
} |
||||
|
||||
/** |
||||
* returns the downSampling factor<br> |
||||
* form more details see {@link #setDownSamplingFactor(float downSamplingFactor)} |
||||
* @return |
||||
*/ |
||||
public float getDownSamplingFactor() { |
||||
return downSamplingFactor; |
||||
} |
||||
|
||||
/** |
||||
* Sets the downSampling factor : the size of the computed texture will be divided by this factor. default is 1 for no downsampling |
||||
* A 2 value is a good way of widening the blur |
||||
* @param downSamplingFactor |
||||
*/ |
||||
public void setDownSamplingFactor(float downSamplingFactor) { |
||||
this.downSamplingFactor = downSamplingFactor; |
||||
if (assetManager != null) // dirty isInitialised check
|
||||
reInitFilter(); |
||||
} |
||||
|
||||
@Override |
||||
public void write(JmeExporter ex) throws IOException { |
||||
super.write(ex); |
||||
OutputCapsule oc = ex.getCapsule(this); |
||||
oc.write(glowMode, "glowMode", GlowMode.Scene); |
||||
oc.write(blurScale, "blurScale", 1.5f); |
||||
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); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,53 @@ |
||||
/* |
||||
* To change this template, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package com.jme3.water; |
||||
|
||||
import com.jme3.math.Plane; |
||||
import com.jme3.math.Vector3f; |
||||
import com.jme3.renderer.Camera; |
||||
import com.jme3.util.TempVars; |
||||
|
||||
/** |
||||
* |
||||
* @author Nehon |
||||
*/ |
||||
public class WaterUtils { |
||||
|
||||
public static void updateReflectionCam(Camera reflectionCam, Plane plane, Camera sceneCam){ |
||||
|
||||
TempVars vars = TempVars.get(); |
||||
//Temp vects for reflection cam orientation calculation
|
||||
Vector3f sceneTarget = vars.vect1; |
||||
Vector3f reflectDirection = vars.vect2; |
||||
Vector3f reflectUp = vars.vect3; |
||||
Vector3f reflectLeft = vars.vect4; |
||||
Vector3f camLoc = vars.vect5; |
||||
camLoc = plane.reflect(sceneCam.getLocation(), camLoc); |
||||
reflectionCam.setLocation(camLoc); |
||||
reflectionCam.setFrustum(sceneCam.getFrustumNear(), |
||||
sceneCam.getFrustumFar(), |
||||
sceneCam.getFrustumLeft(), |
||||
sceneCam.getFrustumRight(), |
||||
sceneCam.getFrustumTop(), |
||||
sceneCam.getFrustumBottom()); |
||||
reflectionCam.setParallelProjection(sceneCam.isParallelProjection()); |
||||
|
||||
sceneTarget.set(sceneCam.getLocation()).addLocal(sceneCam.getDirection()); |
||||
reflectDirection = plane.reflect(sceneTarget, reflectDirection); |
||||
reflectDirection.subtractLocal(camLoc); |
||||
|
||||
sceneTarget.set(sceneCam.getLocation()).subtractLocal(sceneCam.getUp()); |
||||
reflectUp = plane.reflect(sceneTarget, reflectUp); |
||||
reflectUp.subtractLocal(camLoc); |
||||
|
||||
sceneTarget.set(sceneCam.getLocation()).addLocal(sceneCam.getLeft()); |
||||
reflectLeft = plane.reflect(sceneTarget, reflectLeft); |
||||
reflectLeft.subtractLocal(camLoc); |
||||
|
||||
reflectionCam.setAxes(reflectLeft, reflectUp, reflectDirection); |
||||
|
||||
vars.release(); |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,245 +1,298 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
package com.jme3.util; |
||||
|
||||
import com.jme3.asset.AssetManager; |
||||
import com.jme3.asset.TextureKey; |
||||
import com.jme3.bounding.BoundingSphere; |
||||
import com.jme3.material.Material; |
||||
import com.jme3.math.Vector3f; |
||||
import com.jme3.renderer.queue.RenderQueue.Bucket; |
||||
import com.jme3.scene.Geometry; |
||||
import com.jme3.scene.Spatial; |
||||
import com.jme3.scene.shape.Sphere; |
||||
import com.jme3.texture.Image; |
||||
import com.jme3.texture.Image.Format; |
||||
import com.jme3.texture.Texture; |
||||
import com.jme3.texture.TextureCubeMap; |
||||
import java.nio.ByteBuffer; |
||||
import java.util.ArrayList; |
||||
|
||||
/** |
||||
* <code>SkyFactory</code> is used to create jME {@link Spatial}s that can |
||||
* be attached to the scene to display a sky image in the background. |
||||
* |
||||
* @author Kirill Vainer |
||||
*/ |
||||
public class SkyFactory { |
||||
|
||||
/** |
||||
* Creates a sky using the given texture (cubemap or spheremap). |
||||
* |
||||
* @param assetManager The asset manager to use to load materials |
||||
* @param texture Texture to use for the sky |
||||
* @param normalScale The normal scale is multiplied by the 3D normal |
||||
* to get a texture coordinate. Use Vector3f.UNIT_XYZ to not apply |
||||
* and transformation to the normal. |
||||
* @param sphereMap The way the texture is used |
||||
* depends on this value:<br> |
||||
* <ul> |
||||
* <li>true: Its a Texture2D with the pixels arranged for |
||||
* <a href="http://en.wikipedia.org/wiki/Sphere_mapping">sphere mapping</a>.</li> |
||||
* <li>false: Its either a TextureCubeMap or Texture2D. If its a Texture2D |
||||
* then the image is taken from it and is inserted into a TextureCubeMap</li> |
||||
* </ul> |
||||
* @return A spatial representing the sky |
||||
*/ |
||||
public static Spatial createSky(AssetManager assetManager, Texture texture, Vector3f normalScale, boolean sphereMap) { |
||||
return createSky(assetManager, texture, normalScale, sphereMap, 10); |
||||
} |
||||
|
||||
/** |
||||
* Creates a sky using the given texture (cubemap or spheremap). |
||||
* |
||||
* @param assetManager The asset manager to use to load materials |
||||
* @param texture Texture to use for the sky |
||||
* @param normalScale The normal scale is multiplied by the 3D normal |
||||
* to get a texture coordinate. Use Vector3f.UNIT_XYZ to not apply |
||||
* and transformation to the normal. |
||||
* @param sphereMap The way the texture is used |
||||
* depends on this value:<br> |
||||
* <ul> |
||||
* <li>true: Its a Texture2D with the pixels arranged for |
||||
* <a href="http://en.wikipedia.org/wiki/Sphere_mapping">sphere mapping</a>.</li> |
||||
* <li>false: Its either a TextureCubeMap or Texture2D. If its a Texture2D |
||||
* then the image is taken from it and is inserted into a TextureCubeMap</li> |
||||
* </ul> |
||||
* @param sphereRadius If specified, this will be the sky sphere's radius. |
||||
* This should be the camera's near plane for optimal quality. |
||||
* @return A spatial representing the sky |
||||
*/ |
||||
public static Spatial createSky(AssetManager assetManager, Texture texture, Vector3f normalScale, boolean sphereMap, int sphereRadius) { |
||||
if (texture == null) { |
||||
throw new IllegalArgumentException("texture cannot be null"); |
||||
} |
||||
final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true); |
||||
|
||||
Geometry sky = new Geometry("Sky", sphereMesh); |
||||
sky.setQueueBucket(Bucket.Sky); |
||||
sky.setCullHint(Spatial.CullHint.Never); |
||||
sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO)); |
||||
|
||||
Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md"); |
||||
|
||||
skyMat.setVector3("NormalScale", normalScale); |
||||
if (sphereMap) { |
||||
skyMat.setBoolean("SphereMap", sphereMap); |
||||
} else if (!(texture instanceof TextureCubeMap)) { |
||||
// make sure its a cubemap
|
||||
Image img = texture.getImage(); |
||||
texture = new TextureCubeMap(); |
||||
texture.setImage(img); |
||||
} |
||||
skyMat.setTexture("Texture", texture); |
||||
sky.setMaterial(skyMat); |
||||
|
||||
return sky; |
||||
} |
||||
|
||||
private static void checkImage(Image image) { |
||||
// if (image.getDepth() != 1)
|
||||
// throw new IllegalArgumentException("3D/Array images not allowed");
|
||||
|
||||
if (image.getWidth() != image.getHeight()) { |
||||
throw new IllegalArgumentException("Image width and height must be the same"); |
||||
} |
||||
|
||||
if (image.getMultiSamples() != 1) { |
||||
throw new IllegalArgumentException("Multisample textures not allowed"); |
||||
} |
||||
} |
||||
|
||||
private static void checkImagesForCubeMap(Image... images) { |
||||
if (images.length == 1) { |
||||
return; |
||||
} |
||||
|
||||
Format fmt = images[0].getFormat(); |
||||
int width = images[0].getWidth(); |
||||
int height = images[0].getHeight(); |
||||
|
||||
ByteBuffer data = images[0].getData(0); |
||||
int size = data != null ? data.capacity() : 0; |
||||
|
||||
checkImage(images[0]); |
||||
|
||||
for (int i = 1; i < images.length; i++) { |
||||
Image image = images[i]; |
||||
checkImage(images[i]); |
||||
if (image.getFormat() != fmt) { |
||||
throw new IllegalArgumentException("Images must have same format"); |
||||
} |
||||
if (image.getWidth() != width || image.getHeight() != height) { |
||||
throw new IllegalArgumentException("Images must have same resolution"); |
||||
} |
||||
ByteBuffer data2 = image.getData(0); |
||||
if (data2 != null){ |
||||
if (data2.capacity() != size) { |
||||
throw new IllegalArgumentException("Images must have same size"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down, Vector3f normalScale) { |
||||
return createSky(assetManager, west, east, north, south, up, down, normalScale, 10); |
||||
} |
||||
|
||||
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down, Vector3f normalScale, int sphereRadius) { |
||||
final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true); |
||||
Geometry sky = new Geometry("Sky", sphereMesh); |
||||
sky.setQueueBucket(Bucket.Sky); |
||||
sky.setCullHint(Spatial.CullHint.Never); |
||||
sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO)); |
||||
|
||||
Image westImg = west.getImage(); |
||||
Image eastImg = east.getImage(); |
||||
Image northImg = north.getImage(); |
||||
Image southImg = south.getImage(); |
||||
Image upImg = up.getImage(); |
||||
Image downImg = down.getImage(); |
||||
|
||||
checkImagesForCubeMap(westImg, eastImg, northImg, southImg, upImg, downImg); |
||||
|
||||
Image cubeImage = new Image(westImg.getFormat(), westImg.getWidth(), westImg.getHeight(), null); |
||||
|
||||
cubeImage.addData(westImg.getData(0)); |
||||
cubeImage.addData(eastImg.getData(0)); |
||||
|
||||
cubeImage.addData(downImg.getData(0)); |
||||
cubeImage.addData(upImg.getData(0)); |
||||
|
||||
cubeImage.addData(southImg.getData(0)); |
||||
cubeImage.addData(northImg.getData(0)); |
||||
|
||||
if (westImg.getEfficentData() != null){ |
||||
// also consilidate efficient data
|
||||
ArrayList<Object> efficientData = new ArrayList<Object>(6); |
||||
efficientData.add(westImg.getEfficentData()); |
||||
efficientData.add(eastImg.getEfficentData()); |
||||
efficientData.add(downImg.getEfficentData()); |
||||
efficientData.add(upImg.getEfficentData()); |
||||
efficientData.add(southImg.getEfficentData()); |
||||
efficientData.add(northImg.getEfficentData()); |
||||
cubeImage.setEfficentData(efficientData); |
||||
} |
||||
|
||||
TextureCubeMap cubeMap = new TextureCubeMap(cubeImage); |
||||
cubeMap.setAnisotropicFilter(0); |
||||
cubeMap.setMagFilter(Texture.MagFilter.Bilinear); |
||||
cubeMap.setMinFilter(Texture.MinFilter.NearestNoMipMaps); |
||||
cubeMap.setWrap(Texture.WrapMode.EdgeClamp); |
||||
|
||||
Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md"); |
||||
skyMat.setTexture("Texture", cubeMap); |
||||
skyMat.setVector3("NormalScale", normalScale); |
||||
sky.setMaterial(skyMat); |
||||
|
||||
return sky; |
||||
} |
||||
|
||||
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down) { |
||||
return createSky(assetManager, west, east, north, south, up, down, Vector3f.UNIT_XYZ); |
||||
} |
||||
|
||||
public static Spatial createSky(AssetManager assetManager, Texture texture, boolean sphereMap) { |
||||
return createSky(assetManager, texture, Vector3f.UNIT_XYZ, sphereMap); |
||||
} |
||||
|
||||
public static Spatial createSky(AssetManager assetManager, String textureName, boolean sphereMap) { |
||||
TextureKey key = new TextureKey(textureName, true); |
||||
key.setGenerateMips(true); |
||||
key.setAsCube(!sphereMap); |
||||
Texture tex = assetManager.loadTexture(key); |
||||
return createSky(assetManager, tex, sphereMap); |
||||
} |
||||
} |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
package com.jme3.util; |
||||
|
||||
import com.jme3.asset.AssetManager; |
||||
import com.jme3.asset.TextureKey; |
||||
import com.jme3.bounding.BoundingSphere; |
||||
import com.jme3.material.Material; |
||||
import com.jme3.math.Vector3f; |
||||
import com.jme3.renderer.queue.RenderQueue.Bucket; |
||||
import com.jme3.scene.Geometry; |
||||
import com.jme3.scene.Spatial; |
||||
import com.jme3.scene.shape.Sphere; |
||||
import com.jme3.texture.Image; |
||||
import com.jme3.texture.Image.Format; |
||||
import com.jme3.texture.Texture; |
||||
import com.jme3.texture.TextureCubeMap; |
||||
import java.nio.ByteBuffer; |
||||
import java.util.ArrayList; |
||||
|
||||
/** |
||||
* <code>SkyFactory</code> is used to create jME {@link Spatial}s that can |
||||
* be attached to the scene to display a sky image in the background. |
||||
* |
||||
* @author Kirill Vainer |
||||
*/ |
||||
public class SkyFactory { |
||||
|
||||
/** |
||||
* Create a sky with radius=10 using the given cubemap or spheremap texture. |
||||
* |
||||
* For the sky to be visible, its radius must fall between the near and far |
||||
* planes of the camera's frustrum. |
||||
* |
||||
* @param assetManager from which to load materials |
||||
* @param texture to use |
||||
* @param normalScale The normal scale is multiplied by the 3D normal to get |
||||
* a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and |
||||
* transformation to the normal. |
||||
* @param sphereMap determines how the texture is used:<br> |
||||
* <ul> |
||||
* <li>true: The texture is a Texture2D with the pixels arranged for |
||||
* <a href="http://en.wikipedia.org/wiki/Sphere_mapping">sphere |
||||
* mapping</a>.</li> |
||||
* <li>false: The texture is either a TextureCubeMap or Texture2D. If it is |
||||
* a Texture2D then the image is taken from it and is inserted into a |
||||
* TextureCubeMap</li> |
||||
* </ul> |
||||
* @return a new spatial representing the sky, ready to be attached to the |
||||
* scene graph |
||||
*/ |
||||
public static Spatial createSky(AssetManager assetManager, Texture texture, |
||||
Vector3f normalScale, boolean sphereMap) { |
||||
return createSky(assetManager, texture, normalScale, sphereMap, 10); |
||||
} |
||||
|
||||
/** |
||||
* Create a sky using the given cubemap or spheremap texture. |
||||
* |
||||
* @param assetManager from which to load materials |
||||
* @param texture to use |
||||
* @param normalScale The normal scale is multiplied by the 3D normal to get |
||||
* a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and |
||||
* transformation to the normal. |
||||
* @param sphereMap determines how the texture is used:<br> |
||||
* <ul> |
||||
* <li>true: The texture is a Texture2D with the pixels arranged for |
||||
* <a href="http://en.wikipedia.org/wiki/Sphere_mapping">sphere |
||||
* mapping</a>.</li> |
||||
* <li>false: The texture is either a TextureCubeMap or Texture2D. If it is |
||||
* a Texture2D then the image is taken from it and is inserted into a |
||||
* TextureCubeMap</li> |
||||
* </ul> |
||||
* @param sphereRadius the sky sphere's radius: for the sky to be visible, |
||||
* its radius must fall between the near and far planes of the camera's |
||||
* frustrum |
||||
* @return a new spatial representing the sky, ready to be attached to the |
||||
* scene graph |
||||
*/ |
||||
public static Spatial createSky(AssetManager assetManager, Texture texture, |
||||
Vector3f normalScale, boolean sphereMap, int sphereRadius) { |
||||
if (texture == null) { |
||||
throw new IllegalArgumentException("texture cannot be null"); |
||||
} |
||||
final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true); |
||||
|
||||
Geometry sky = new Geometry("Sky", sphereMesh); |
||||
sky.setQueueBucket(Bucket.Sky); |
||||
sky.setCullHint(Spatial.CullHint.Never); |
||||
sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO)); |
||||
|
||||
Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md"); |
||||
|
||||
skyMat.setVector3("NormalScale", normalScale); |
||||
if (sphereMap) { |
||||
skyMat.setBoolean("SphereMap", sphereMap); |
||||
} else if (!(texture instanceof TextureCubeMap)) { |
||||
// make sure its a cubemap
|
||||
Image img = texture.getImage(); |
||||
texture = new TextureCubeMap(); |
||||
texture.setImage(img); |
||||
} |
||||
skyMat.setTexture("Texture", texture); |
||||
sky.setMaterial(skyMat); |
||||
|
||||
return sky; |
||||
} |
||||
|
||||
private static void checkImage(Image image) { |
||||
// if (image.getDepth() != 1)
|
||||
// throw new IllegalArgumentException("3D/Array images not allowed");
|
||||
|
||||
if (image.getWidth() != image.getHeight()) { |
||||
throw new IllegalArgumentException("Image width and height must be the same"); |
||||
} |
||||
|
||||
if (image.getMultiSamples() != 1) { |
||||
throw new IllegalArgumentException("Multisample textures not allowed"); |
||||
} |
||||
} |
||||
|
||||
private static void checkImagesForCubeMap(Image... images) { |
||||
if (images.length == 1) { |
||||
return; |
||||
} |
||||
|
||||
Format fmt = images[0].getFormat(); |
||||
int width = images[0].getWidth(); |
||||
int height = images[0].getHeight(); |
||||
|
||||
ByteBuffer data = images[0].getData(0); |
||||
int size = data != null ? data.capacity() : 0; |
||||
|
||||
checkImage(images[0]); |
||||
|
||||
for (int i = 1; i < images.length; i++) { |
||||
Image image = images[i]; |
||||
checkImage(images[i]); |
||||
if (image.getFormat() != fmt) { |
||||
throw new IllegalArgumentException("Images must have same format"); |
||||
} |
||||
if (image.getWidth() != width || image.getHeight() != height) { |
||||
throw new IllegalArgumentException("Images must have same resolution"); |
||||
} |
||||
ByteBuffer data2 = image.getData(0); |
||||
if (data2 != null){ |
||||
if (data2.capacity() != size) { |
||||
throw new IllegalArgumentException("Images must have same size"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Create a cube-mapped sky with radius=10 using six textures. |
||||
* |
||||
* For the sky to be visible, its radius must fall between the near and far |
||||
* planes of the camera's frustrum. |
||||
* |
||||
* @param assetManager from which to load materials |
||||
* @param west texture for the western face of the cube |
||||
* @param east texture for the eastern face of the cube |
||||
* @param north texture for the northern face of the cube |
||||
* @param south texture for the southern face of the cube |
||||
* @param up texture for the top face of the cube |
||||
* @param down texture for the bottom face of the cube |
||||
* @param normalScale The normal scale is multiplied by the 3D normal to get |
||||
* a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and |
||||
* transformation to the normal. |
||||
* @return a new spatial representing the sky, ready to be attached to the |
||||
* scene graph |
||||
*/ |
||||
public static Spatial createSky(AssetManager assetManager, Texture west, |
||||
Texture east, Texture north, Texture south, Texture up, |
||||
Texture down, Vector3f normalScale) { |
||||
return createSky(assetManager, west, east, north, south, up, down, |
||||
normalScale, 10); |
||||
} |
||||
|
||||
/** |
||||
* Create a cube-mapped sky using six textures. |
||||
* |
||||
* @param assetManager from which to load materials |
||||
* @param west texture for the western face of the cube |
||||
* @param east texture for the eastern face of the cube |
||||
* @param north texture for the northern face of the cube |
||||
* @param south texture for the southern face of the cube |
||||
* @param up texture for the top face of the cube |
||||
* @param down texture for the bottom face of the cube |
||||
* @param normalScale The normal scale is multiplied by the 3D normal to get |
||||
* a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and |
||||
* transformation to the normal. |
||||
* @param sphereRadius the sky sphere's radius: for the sky to be visible, |
||||
* its radius must fall between the near and far planes of the camera's |
||||
* frustrum |
||||
* @return a new spatial representing the sky, ready to be attached to the |
||||
* scene graph |
||||
*/ |
||||
public static Spatial createSky(AssetManager assetManager, Texture west, |
||||
Texture east, Texture north, Texture south, Texture up, |
||||
Texture down, Vector3f normalScale, float sphereRadius) { |
||||
final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true); |
||||
Geometry sky = new Geometry("Sky", sphereMesh); |
||||
sky.setQueueBucket(Bucket.Sky); |
||||
sky.setCullHint(Spatial.CullHint.Never); |
||||
sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO)); |
||||
|
||||
Image westImg = west.getImage(); |
||||
Image eastImg = east.getImage(); |
||||
Image northImg = north.getImage(); |
||||
Image southImg = south.getImage(); |
||||
Image upImg = up.getImage(); |
||||
Image downImg = down.getImage(); |
||||
|
||||
checkImagesForCubeMap(westImg, eastImg, northImg, southImg, upImg, downImg); |
||||
|
||||
Image cubeImage = new Image(westImg.getFormat(), westImg.getWidth(), westImg.getHeight(), null); |
||||
|
||||
cubeImage.addData(westImg.getData(0)); |
||||
cubeImage.addData(eastImg.getData(0)); |
||||
|
||||
cubeImage.addData(downImg.getData(0)); |
||||
cubeImage.addData(upImg.getData(0)); |
||||
|
||||
cubeImage.addData(southImg.getData(0)); |
||||
cubeImage.addData(northImg.getData(0)); |
||||
|
||||
if (westImg.getEfficentData() != null){ |
||||
// also consilidate efficient data
|
||||
ArrayList<Object> efficientData = new ArrayList<Object>(6); |
||||
efficientData.add(westImg.getEfficentData()); |
||||
efficientData.add(eastImg.getEfficentData()); |
||||
efficientData.add(downImg.getEfficentData()); |
||||
efficientData.add(upImg.getEfficentData()); |
||||
efficientData.add(southImg.getEfficentData()); |
||||
efficientData.add(northImg.getEfficentData()); |
||||
cubeImage.setEfficentData(efficientData); |
||||
} |
||||
|
||||
TextureCubeMap cubeMap = new TextureCubeMap(cubeImage); |
||||
cubeMap.setAnisotropicFilter(0); |
||||
cubeMap.setMagFilter(Texture.MagFilter.Bilinear); |
||||
cubeMap.setMinFilter(Texture.MinFilter.NearestNoMipMaps); |
||||
cubeMap.setWrap(Texture.WrapMode.EdgeClamp); |
||||
|
||||
Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md"); |
||||
skyMat.setTexture("Texture", cubeMap); |
||||
skyMat.setVector3("NormalScale", normalScale); |
||||
sky.setMaterial(skyMat); |
||||
|
||||
return sky; |
||||
} |
||||
|
||||
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down) { |
||||
return createSky(assetManager, west, east, north, south, up, down, Vector3f.UNIT_XYZ); |
||||
} |
||||
|
||||
public static Spatial createSky(AssetManager assetManager, Texture texture, boolean sphereMap) { |
||||
return createSky(assetManager, texture, Vector3f.UNIT_XYZ, sphereMap); |
||||
} |
||||
|
||||
public static Spatial createSky(AssetManager assetManager, String textureName, boolean sphereMap) { |
||||
TextureKey key = new TextureKey(textureName, true); |
||||
key.setGenerateMips(true); |
||||
key.setAsCube(!sphereMap); |
||||
Texture tex = assetManager.loadTexture(key); |
||||
return createSky(assetManager, tex, sphereMap); |
||||
} |
||||
} |
||||
|
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 42 KiB |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue