Added cloneable and jmeCloneable interfaces to shadows renders and filters

fix-456
javasabr 8 years ago
parent 1be4a48223
commit 6c3100f929
  1. 22
      jme3-core/src/main/java/com/jme3/shadow/AbstractShadowFilter.java
  2. 32
      jme3-core/src/main/java/com/jme3/shadow/AbstractShadowRenderer.java
  3. 25
      jme3-core/src/main/java/com/jme3/shadow/DirectionalLightShadowRenderer.java
  4. 17
      jme3-core/src/main/java/com/jme3/shadow/PointLightShadowRenderer.java
  5. 18
      jme3-core/src/main/java/com/jme3/shadow/SpotLightShadowRenderer.java

@ -45,6 +45,8 @@ 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.FrameBuffer; import com.jme3.texture.FrameBuffer;
import com.jme3.util.clone.Cloner;
import com.jme3.util.clone.JmeCloneable;
import java.io.IOException; import java.io.IOException;
@ -55,7 +57,7 @@ import java.io.IOException;
* *
* @author Rémy Bouquet aka Nehon * @author Rémy Bouquet aka Nehon
*/ */
public abstract class AbstractShadowFilter<T extends AbstractShadowRenderer> extends Filter { public abstract class AbstractShadowFilter<T extends AbstractShadowRenderer> extends Filter implements Cloneable, JmeCloneable {
protected T shadowRenderer; protected T shadowRenderer;
protected ViewPort viewPort; protected ViewPort viewPort;
@ -303,17 +305,31 @@ public abstract class AbstractShadowFilter<T extends AbstractShadowRenderer> ext
return shadowRenderer.getEdgeFilteringMode(); return shadowRenderer.getEdgeFilteringMode();
} }
@Override
public AbstractShadowFilter<T> jmeClone() {
try {
return (AbstractShadowFilter<T>) super.clone();
} catch (final CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
@Override
public void cloneFields(final Cloner cloner, final Object original) {
material = cloner.clone(material);
shadowRenderer = cloner.clone(shadowRenderer);
shadowRenderer.setPostShadowMaterial(material);
}
@Override @Override
public void write(JmeExporter ex) throws IOException { public void write(JmeExporter ex) throws IOException {
super.write(ex); super.write(ex);
OutputCapsule oc = ex.getCapsule(this); OutputCapsule oc = ex.getCapsule(this);
} }
@Override @Override
public void read(JmeImporter im) throws IOException { public void read(JmeImporter im) throws IOException {
super.read(im); super.read(im);
InputCapsule ic = im.getCapsule(this); InputCapsule ic = im.getCapsule(this);
} }
} }

@ -62,10 +62,13 @@ import com.jme3.texture.Texture.MinFilter;
import com.jme3.texture.Texture.ShadowCompareMode; import com.jme3.texture.Texture.ShadowCompareMode;
import com.jme3.texture.Texture2D; import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture; import com.jme3.ui.Picture;
import com.jme3.util.clone.Cloner;
import com.jme3.util.clone.JmeCloneable;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
/** /**
* abstract shadow renderer that holds commons feature to have for a shadow * abstract shadow renderer that holds commons feature to have for a shadow
@ -73,7 +76,9 @@ import java.util.List;
* *
* @author Rémy Bouquet aka Nehon * @author Rémy Bouquet aka Nehon
*/ */
public abstract class AbstractShadowRenderer implements SceneProcessor, Savable { public abstract class AbstractShadowRenderer implements SceneProcessor, Savable, JmeCloneable, Cloneable {
protected static final Logger logger = Logger.getLogger(AbstractShadowRenderer.class.getName());
protected int nbShadowMaps = 1; protected int nbShadowMaps = 1;
protected float shadowMapSize; protected float shadowMapSize;
@ -147,7 +152,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
} }
protected void init(AssetManager assetManager, int nbShadowMaps, int shadowMapSize) { private void init(AssetManager assetManager, int nbShadowMaps, int shadowMapSize) {
this.postshadowMat = new Material(assetManager, "Common/MatDefs/Shadow/PostShadow.j3md"); this.postshadowMat = new Material(assetManager, "Common/MatDefs/Shadow/PostShadow.j3md");
shadowFB = new FrameBuffer[nbShadowMaps]; shadowFB = new FrameBuffer[nbShadowMaps];
shadowMaps = new Texture2D[nbShadowMaps]; shadowMaps = new Texture2D[nbShadowMaps];
@ -677,8 +682,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
} }
/** /**
* returns true if the light source bounding box is in the view frustum * @return true if the light source bounding box is in the view frustum
* @return
*/ */
protected abstract boolean checkCulling(Camera viewCam); protected abstract boolean checkCulling(Camera viewCam);
@ -747,7 +751,6 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
@Deprecated @Deprecated
public void setFlushQueues(boolean flushQueues) {} public void setFlushQueues(boolean flushQueues) {}
/** /**
* returns the pre shadows pass render state. * returns the pre shadows pass render state.
* use it to adjust the RenderState parameters of the pre shadow pass. * use it to adjust the RenderState parameters of the pre shadow pass.
@ -789,13 +792,28 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
return renderBackFacesShadows != null?renderBackFacesShadows:false; return renderBackFacesShadows != null?renderBackFacesShadows:false;
} }
@Override
public Object jmeClone() {
try {
return super.clone();
} catch (final CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
@Override
public void cloneFields(final Cloner cloner, final Object original) {
forcedRenderState = cloner.clone(forcedRenderState);
init(assetManager, nbShadowMaps, (int) shadowMapSize);
}
/** /**
* De-serialize this instance, for example when loading from a J3O file. * De-serialize this instance, for example when loading from a J3O file.
* *
* @param im importer (not null) * @param im importer (not null)
*/ */
public void read(JmeImporter im) throws IOException { public void read(JmeImporter im) throws IOException {
InputCapsule ic = (InputCapsule) im.getCapsule(this); InputCapsule ic = im.getCapsule(this);
assetManager = im.getAssetManager(); assetManager = im.getAssetManager();
nbShadowMaps = ic.readInt("nbShadowMaps", 1); nbShadowMaps = ic.readInt("nbShadowMaps", 1);
shadowMapSize = ic.readFloat("shadowMapSize", 0f); shadowMapSize = ic.readFloat("shadowMapSize", 0f);
@ -814,7 +832,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
* @param ex exporter (not null) * @param ex exporter (not null)
*/ */
public void write(JmeExporter ex) throws IOException { public void write(JmeExporter ex) throws IOException {
OutputCapsule oc = (OutputCapsule) ex.getCapsule(this); OutputCapsule oc = ex.getCapsule(this);
oc.write(nbShadowMaps, "nbShadowMaps", 1); oc.write(nbShadowMaps, "nbShadowMaps", 1);
oc.write(shadowMapSize, "shadowMapSize", 0); oc.write(shadowMapSize, "shadowMapSize", 0);
oc.write(shadowIntensity, "shadowIntensity", 0.7f); oc.write(shadowIntensity, "shadowIntensity", 0.7f);

@ -46,6 +46,8 @@ import com.jme3.renderer.queue.GeometryList;
import com.jme3.renderer.queue.RenderQueue; import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.util.clone.Cloner;
import java.io.IOException; import java.io.IOException;
/** /**
@ -113,8 +115,6 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
//nothing to do //nothing to do
} }
/** /**
* return the light used to cast shadows * return the light used to cast shadows
* *
@ -136,6 +136,12 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
@Override @Override
protected void updateShadowCams(Camera viewCam) { protected void updateShadowCams(Camera viewCam) {
// it needs to support for editors
if (light == null) {
logger.warning("The light can't be null for a " + getClass().getName());
return;
}
float zFar = zFarOverride; float zFar = zFarOverride;
if (zFar == 0) { if (zFar == 0) {
zFar = viewCam.getFrustumFar(); zFar = viewCam.getFrustumFar();
@ -249,10 +255,8 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
this.lambda = lambda; this.lambda = lambda;
} }
/** /**
* retruns true if stabilization is enabled * @return true if stabilization is enabled
* @return
*/ */
public boolean isEnabledStabilization() { public boolean isEnabledStabilization() {
return stabilize; return stabilize;
@ -268,10 +272,17 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
this.stabilize = stabilize; this.stabilize = stabilize;
} }
@Override
public void cloneFields(final Cloner cloner, final Object original) {
light = cloner.clone(light);
init(nbShadowMaps, (int) shadowMapSize);
super.cloneFields(cloner, original);
}
@Override @Override
public void read(JmeImporter im) throws IOException { public void read(JmeImporter im) throws IOException {
super.read(im); super.read(im);
InputCapsule ic = (InputCapsule) im.getCapsule(this); InputCapsule ic = im.getCapsule(this);
lambda = ic.readFloat("lambda", 0.65f); lambda = ic.readFloat("lambda", 0.65f);
zFarOverride = ic.readInt("zFarOverride", 0); zFarOverride = ic.readInt("zFarOverride", 0);
light = (DirectionalLight) ic.readSavable("light", null); light = (DirectionalLight) ic.readSavable("light", null);
@ -283,7 +294,7 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
@Override @Override
public void write(JmeExporter ex) throws IOException { public void write(JmeExporter ex) throws IOException {
super.write(ex); super.write(ex);
OutputCapsule oc = (OutputCapsule) ex.getCapsule(this); OutputCapsule oc = ex.getCapsule(this);
oc.write(lambda, "lambda", 0.65f); oc.write(lambda, "lambda", 0.65f);
oc.write(zFarOverride, "zFarOverride", 0); oc.write(zFarOverride, "zFarOverride", 0);
oc.write(light, "light", null); oc.write(light, "light", null);

@ -46,6 +46,8 @@ import com.jme3.scene.Geometry;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.util.TempVars; import com.jme3.util.TempVars;
import com.jme3.util.clone.Cloner;
import java.io.IOException; import java.io.IOException;
/** /**
@ -100,7 +102,8 @@ public class PointLightShadowRenderer extends AbstractShadowRenderer {
protected void updateShadowCams(Camera viewCam) { protected void updateShadowCams(Camera viewCam) {
if (light == null) { if (light == null) {
throw new IllegalStateException("The light can't be null for a " + this.getClass().getName()); logger.warning("The light can't be null for a " + getClass().getName());
return;
} }
//bottom //bottom
@ -197,10 +200,18 @@ public class PointLightShadowRenderer extends AbstractShadowRenderer {
this.light = light; this.light = light;
} }
@Override
public void cloneFields(final Cloner cloner, final Object original) {
light = cloner.clone(light);
init((int) shadowMapSize);
frustums = null;
super.cloneFields(cloner, original);
}
@Override @Override
public void read(JmeImporter im) throws IOException { public void read(JmeImporter im) throws IOException {
super.read(im); super.read(im);
InputCapsule ic = (InputCapsule) im.getCapsule(this); InputCapsule ic = im.getCapsule(this);
light = (PointLight) ic.readSavable("light", null); light = (PointLight) ic.readSavable("light", null);
init((int) shadowMapSize); init((int) shadowMapSize);
} }
@ -208,7 +219,7 @@ public class PointLightShadowRenderer extends AbstractShadowRenderer {
@Override @Override
public void write(JmeExporter ex) throws IOException { public void write(JmeExporter ex) throws IOException {
super.write(ex); super.write(ex);
OutputCapsule oc = (OutputCapsule) ex.getCapsule(this); OutputCapsule oc = ex.getCapsule(this);
oc.write(light, "light", null); oc.write(light, "light", null);
} }

@ -47,6 +47,8 @@ import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.util.TempVars; import com.jme3.util.TempVars;
import com.jme3.util.clone.Cloner;
import java.io.IOException; import java.io.IOException;
/** /**
@ -123,6 +125,11 @@ public class SpotLightShadowRenderer extends AbstractShadowRenderer {
@Override @Override
protected void updateShadowCams(Camera viewCam) { protected void updateShadowCams(Camera viewCam) {
if (light == null) {
logger.warning("The light can't be null for a " + getClass().getName());
return;
}
float zFar = zFarOverride; float zFar = zFarOverride;
if (zFar == 0) { if (zFar == 0) {
zFar = viewCam.getFrustumFar(); zFar = viewCam.getFrustumFar();
@ -139,7 +146,6 @@ public class SpotLightShadowRenderer extends AbstractShadowRenderer {
shadowCam.update(); shadowCam.update();
shadowCam.updateViewProjection(); shadowCam.updateViewProjection();
} }
@Override @Override
@ -186,11 +192,17 @@ public class SpotLightShadowRenderer extends AbstractShadowRenderer {
material.clearParam("LightDir"); material.clearParam("LightDir");
} }
@Override
public void cloneFields(final Cloner cloner, final Object original) {
light = cloner.clone(light);
init((int) shadowMapSize);
super.cloneFields(cloner, original);
}
@Override @Override
public void read(JmeImporter im) throws IOException { public void read(JmeImporter im) throws IOException {
super.read(im); super.read(im);
InputCapsule ic = (InputCapsule) im.getCapsule(this); InputCapsule ic = im.getCapsule(this);
zFarOverride = ic.readInt("zFarOverride", 0); zFarOverride = ic.readInt("zFarOverride", 0);
light = (SpotLight) ic.readSavable("light", null); light = (SpotLight) ic.readSavable("light", null);
fadeInfo = (Vector2f) ic.readSavable("fadeInfo", null); fadeInfo = (Vector2f) ic.readSavable("fadeInfo", null);
@ -202,7 +214,7 @@ public class SpotLightShadowRenderer extends AbstractShadowRenderer {
@Override @Override
public void write(JmeExporter ex) throws IOException { public void write(JmeExporter ex) throws IOException {
super.write(ex); super.write(ex);
OutputCapsule oc = (OutputCapsule) ex.getCapsule(this); OutputCapsule oc = ex.getCapsule(this);
oc.write(zFarOverride, "zFarOverride", 0); oc.write(zFarOverride, "zFarOverride", 0);
oc.write(light, "light", null); oc.write(light, "light", null);
oc.write(fadeInfo, "fadeInfo", null); oc.write(fadeInfo, "fadeInfo", null);

Loading…
Cancel
Save