parent
6fb2d029d2
commit
caad16626e
@ -0,0 +1,183 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2016 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.shadow.next; |
||||||
|
|
||||||
|
import com.jme3.shadow.next.pssm.DirectionalShadowParameters; |
||||||
|
import com.jme3.light.DirectionalLight; |
||||||
|
import com.jme3.light.Light; |
||||||
|
import com.jme3.material.RenderState; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.post.SceneProcessor; |
||||||
|
import com.jme3.profile.AppProfiler; |
||||||
|
import com.jme3.renderer.RenderManager; |
||||||
|
import com.jme3.renderer.Renderer; |
||||||
|
import com.jme3.renderer.ViewPort; |
||||||
|
import com.jme3.renderer.queue.GeometryList; |
||||||
|
import com.jme3.renderer.queue.OpaqueComparator; |
||||||
|
import com.jme3.renderer.queue.RenderQueue; |
||||||
|
import com.jme3.shadow.next.pssm.DirectionalShadowMap; |
||||||
|
import com.jme3.texture.FrameBuffer; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* The 4th generation of shadow mapping in jME3. |
||||||
|
* <p> |
||||||
|
* This version is primarily focused on rendering in-pass shadows, so pre-pass |
||||||
|
* and subsequent stages are separated. |
||||||
|
* |
||||||
|
* @author Kirill Vainer |
||||||
|
*/ |
||||||
|
public class PreShadowRenderer implements SceneProcessor { |
||||||
|
|
||||||
|
private static final String PRE_SHADOW_TECHNIQUE_NAME = "PreShadow"; |
||||||
|
|
||||||
|
private RenderManager renderManager; |
||||||
|
private ViewPort viewPort; |
||||||
|
private final Vector3f[] points = new Vector3f[8]; |
||||||
|
private final GeometryList shadowCasters = new GeometryList(new OpaqueComparator()); |
||||||
|
private final List<ShadowMap> shadowMaps = new ArrayList<>(); |
||||||
|
private final RenderState prePassRenderState = RenderState.ADDITIONAL.clone(); |
||||||
|
|
||||||
|
private int textureSize = 1024; |
||||||
|
|
||||||
|
// parameters for directional lights
|
||||||
|
private final DirectionalShadowParameters directionalParams = new DirectionalShadowParameters(); |
||||||
|
|
||||||
|
public PreShadowRenderer() { |
||||||
|
for (int i = 0; i < points.length; i++) { |
||||||
|
points[i] = new Vector3f(); |
||||||
|
} |
||||||
|
|
||||||
|
prePassRenderState.setFaceCullMode(RenderState.FaceCullMode.Off); |
||||||
|
prePassRenderState.setColorWrite(false); |
||||||
|
prePassRenderState.setDepthWrite(true); |
||||||
|
prePassRenderState.setDepthTest(true); |
||||||
|
prePassRenderState.setPolyOffset(1.2f, 0); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void initialize(RenderManager rm, ViewPort vp) { |
||||||
|
this.renderManager = rm; |
||||||
|
this.viewPort = vp; |
||||||
|
} |
||||||
|
|
||||||
|
public DirectionalShadowParameters directional() { |
||||||
|
return directionalParams; |
||||||
|
} |
||||||
|
|
||||||
|
public void setPolyOffset(float factor, float units) { |
||||||
|
// TODO: might want to set this separately per model
|
||||||
|
prePassRenderState.setPolyOffset(factor, units); |
||||||
|
} |
||||||
|
|
||||||
|
public int getTextureSize() { |
||||||
|
return textureSize; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTextureSize(int textureSize) { |
||||||
|
// TODO: support changing texture size after shadow maps are created
|
||||||
|
this.textureSize = textureSize; |
||||||
|
} |
||||||
|
|
||||||
|
public void addLight(Light light) { |
||||||
|
ShadowMap shadowMap; |
||||||
|
switch (light.getType()) { |
||||||
|
case Directional: |
||||||
|
shadowMap = new DirectionalShadowMap( |
||||||
|
(DirectionalLight) light, |
||||||
|
textureSize, |
||||||
|
directionalParams.getNumSplits(), |
||||||
|
points); |
||||||
|
break; |
||||||
|
default: |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
|
||||||
|
light.setShadowMap(shadowMap); |
||||||
|
shadowMaps.add(shadowMap); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void reshape(ViewPort vp, int w, int h) { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isInitialized() { |
||||||
|
return this.viewPort != null; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void preFrame(float tpf) { |
||||||
|
} |
||||||
|
|
||||||
|
private void renderShadowMaps() { |
||||||
|
renderManager.setForcedRenderState(prePassRenderState); |
||||||
|
renderManager.setForcedTechnique(PRE_SHADOW_TECHNIQUE_NAME); |
||||||
|
|
||||||
|
for (ShadowMap shadowMap : shadowMaps) { |
||||||
|
switch (shadowMap.getLightType()) { |
||||||
|
case Directional: |
||||||
|
DirectionalShadowMap directionalShadow = (DirectionalShadowMap) shadowMap; |
||||||
|
directionalShadow.renderShadowMap(renderManager, viewPort, directionalParams, shadowCasters); |
||||||
|
break; |
||||||
|
default: |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Renderer renderer = renderManager.getRenderer(); |
||||||
|
renderer.setFrameBuffer(viewPort.getOutputFrameBuffer()); |
||||||
|
renderManager.setForcedRenderState(null); |
||||||
|
renderManager.setForcedTechnique(null); |
||||||
|
renderManager.setCamera(viewPort.getCamera(), false); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void postQueue(RenderQueue rq) { |
||||||
|
directionalParams.updateSplitPositions(viewPort.getCamera()); |
||||||
|
renderShadowMaps(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void postFrame(FrameBuffer out) { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cleanup() { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setProfiler(AppProfiler profiler) { |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,99 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2017 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.shadow.next; |
||||||
|
|
||||||
|
import com.jme3.asset.AssetManager; |
||||||
|
import com.jme3.material.Material; |
||||||
|
import com.jme3.renderer.RenderManager; |
||||||
|
import com.jme3.renderer.ViewPort; |
||||||
|
import com.jme3.scene.Node; |
||||||
|
import com.jme3.scene.Spatial; |
||||||
|
import com.jme3.scene.control.AbstractControl; |
||||||
|
import com.jme3.shader.VarType; |
||||||
|
import com.jme3.texture.Image; |
||||||
|
import com.jme3.texture.TextureArray; |
||||||
|
import com.jme3.ui.Picture; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Shows the shadow maps on the screen |
||||||
|
* |
||||||
|
* @author Kirill Vainer |
||||||
|
*/ |
||||||
|
final class ShadowDebugControl extends AbstractControl { |
||||||
|
|
||||||
|
private final List<Picture> pictures = new ArrayList<>(); |
||||||
|
|
||||||
|
public ShadowDebugControl(AssetManager assetManager, PreShadowArrayRenderer shadowRenderer) { |
||||||
|
TextureArray shadowMapArray = shadowRenderer.getShadowMapTexture(); |
||||||
|
Image shadowMap = shadowMapArray.getImage(); |
||||||
|
for (int i = 0; i < shadowMap.getDepth(); i++) { |
||||||
|
Picture picture = new Picture("Shadow Map " + i); |
||||||
|
picture.setPosition(20, i * 128 + 20); |
||||||
|
picture.setWidth(128); |
||||||
|
picture.setHeight(128); |
||||||
|
|
||||||
|
Material material = new Material(assetManager, "Common/MatDefs/Shadow/ShowShadowArray.j3md"); |
||||||
|
material.setTexture("ShadowMapArray", shadowMapArray); |
||||||
|
material.setFloat("ShadowMapSlice", i); |
||||||
|
picture.setMaterial(material); |
||||||
|
|
||||||
|
pictures.add(picture); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setSpatial(Spatial spatial) { |
||||||
|
if (spatial != null) { |
||||||
|
for (Picture picture : pictures) { |
||||||
|
((Node) spatial).detachChild(picture); |
||||||
|
} |
||||||
|
} |
||||||
|
super.setSpatial(spatial); |
||||||
|
if (spatial != null) { |
||||||
|
for (Picture picture : pictures) { |
||||||
|
((Node) spatial).attachChild(picture); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlUpdate(float tpf) { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlRender(RenderManager rm, ViewPort vp) { |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,87 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2016 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.shadow.next.pssm; |
||||||
|
|
||||||
|
import com.jme3.light.Light; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.renderer.Camera; |
||||||
|
import com.jme3.renderer.RenderManager; |
||||||
|
import com.jme3.renderer.Renderer; |
||||||
|
import com.jme3.renderer.ViewPort; |
||||||
|
import com.jme3.renderer.queue.GeometryList; |
||||||
|
import com.jme3.texture.FrameBuffer; |
||||||
|
import com.jme3.texture.Image; |
||||||
|
import com.jme3.texture.Texture.MagFilter; |
||||||
|
import com.jme3.texture.Texture.MinFilter; |
||||||
|
import com.jme3.texture.Texture.ShadowCompareMode; |
||||||
|
import com.jme3.texture.Texture2D; |
||||||
|
import com.jme3.shadow.next.ShadowMapSlice; |
||||||
|
|
||||||
|
public abstract class BaseShadowMapSlice<T extends Light> implements ShadowMapSlice<T> { |
||||||
|
|
||||||
|
protected final FrameBuffer frameBuffer; |
||||||
|
protected final Texture2D depthTexture; |
||||||
|
protected final Camera shadowCamera; |
||||||
|
protected final Vector3f[] points; |
||||||
|
|
||||||
|
public BaseShadowMapSlice(int size, Vector3f[] points) { |
||||||
|
this.depthTexture = new Texture2D(size, size, Image.Format.Depth16); |
||||||
|
this.depthTexture.setAnisotropicFilter(1); |
||||||
|
this.depthTexture.setShadowCompareMode(ShadowCompareMode.LessOrEqual); |
||||||
|
this.depthTexture.setMagFilter(MagFilter.Bilinear); |
||||||
|
this.depthTexture.setMinFilter(MinFilter.BilinearNoMipMaps); |
||||||
|
this.shadowCamera = new Camera(size, size); |
||||||
|
this.frameBuffer = new FrameBuffer(size, size, 1); |
||||||
|
this.frameBuffer.setDepthTexture(depthTexture); |
||||||
|
this.points = points; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void renderShadowMap(RenderManager renderManager, T light, ViewPort viewPort, GeometryList shadowCasters) { |
||||||
|
Renderer renderer = renderManager.getRenderer(); |
||||||
|
|
||||||
|
renderer.setFrameBuffer(frameBuffer); |
||||||
|
renderer.clearBuffers(false, true, false); |
||||||
|
|
||||||
|
if (shadowCasters.size() > 0) { |
||||||
|
renderManager.setCamera(shadowCamera, false); |
||||||
|
viewPort.getQueue().renderShadowQueue(shadowCasters, renderManager, shadowCamera, true); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Matrix4f getBiasedViewProjectionMatrix() { |
||||||
|
// return shadowCamera.getViewProjectionMatrix();
|
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,92 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2016 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.shadow.next.pssm; |
||||||
|
|
||||||
|
import com.jme3.light.DirectionalLight; |
||||||
|
import com.jme3.light.Light.Type; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.math.Vector4f; |
||||||
|
import com.jme3.renderer.RenderManager; |
||||||
|
import com.jme3.renderer.ViewPort; |
||||||
|
import com.jme3.renderer.queue.GeometryList; |
||||||
|
import com.jme3.shadow.next.ShadowMapSlice; |
||||||
|
import com.jme3.shadow.next.ShadowMap; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Kirill Vainer |
||||||
|
*/ |
||||||
|
public class DirectionalShadowMap implements ShadowMap<DirectionalLight> { |
||||||
|
|
||||||
|
private final DirectionalLight light; |
||||||
|
private final DirectionalShadowMapSlice[] splits; |
||||||
|
private final Vector3f projectionSplitPositions = new Vector3f(); |
||||||
|
|
||||||
|
public DirectionalShadowMap(DirectionalLight light, int textureSize, int numSplits, Vector3f[] points) { |
||||||
|
this.light = light; |
||||||
|
this.splits = new DirectionalShadowMapSlice[numSplits]; |
||||||
|
for (int i = 0; i < splits.length; i++) { |
||||||
|
this.splits[i] = new DirectionalShadowMapSlice(textureSize, points); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void renderShadowMap(RenderManager renderManager, ViewPort viewPort, DirectionalShadowParameters params, GeometryList shadowCasters) { |
||||||
|
projectionSplitPositions.set(params.getProjectionSplitPositions()); |
||||||
|
float[] splitPositionsViewSpace = params.getSplitPositions(); |
||||||
|
for (int i = 0; i < splits.length; i++) { |
||||||
|
float near = splitPositionsViewSpace[i]; |
||||||
|
float far = splitPositionsViewSpace[i + 1]; |
||||||
|
shadowCasters.clear(); |
||||||
|
splits[i].updateShadowCamera(viewPort, light, shadowCasters, near, far); |
||||||
|
splits[i].renderShadowMap(renderManager, light, viewPort, shadowCasters); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public Vector3f getProjectionSplitPositions() { |
||||||
|
return projectionSplitPositions; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int getNumSlices() { |
||||||
|
return splits.length; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public ShadowMapSlice getSlice(int index) { |
||||||
|
return splits[index]; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Type getLightType() { |
||||||
|
return Type.Directional; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,67 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2016 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.shadow.next.pssm; |
||||||
|
|
||||||
|
import com.jme3.light.DirectionalLight; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.renderer.ViewPort; |
||||||
|
import com.jme3.renderer.queue.GeometryList; |
||||||
|
import com.jme3.shadow.ShadowUtil; |
||||||
|
import com.jme3.texture.Texture2D; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Kirill Vainer |
||||||
|
*/ |
||||||
|
public class DirectionalShadowMapSlice extends BaseShadowMapSlice<DirectionalLight> { |
||||||
|
|
||||||
|
public DirectionalShadowMapSlice(int size, Vector3f[] points) { |
||||||
|
super(size, points); |
||||||
|
this.shadowCamera.setParallelProjection(true); |
||||||
|
} |
||||||
|
|
||||||
|
public void updateShadowCamera( |
||||||
|
ViewPort viewPort, |
||||||
|
DirectionalLight light, |
||||||
|
GeometryList shadowCasters, |
||||||
|
float near, |
||||||
|
float far) { |
||||||
|
ShadowUtil.updateFrustumPoints(viewPort.getCamera(), near, far, points); |
||||||
|
shadowCamera.lookAtDirection(light.getDirection(), shadowCamera.getUp()); |
||||||
|
|
||||||
|
int textureSize = frameBuffer.getWidth(); |
||||||
|
ShadowUtil.updateShadowCamera(viewPort, null, shadowCamera, points, shadowCasters, textureSize); |
||||||
|
} |
||||||
|
|
||||||
|
public Texture2D getTexture() { |
||||||
|
return depthTexture; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
#import "Common/ShaderLib/GLSLCompat.glsllib" |
||||||
|
|
||||||
|
uniform float m_ShadowMapSlice; |
||||||
|
uniform sampler2DArray m_ShadowMapArray; |
||||||
|
varying vec2 texCoord1; |
||||||
|
|
||||||
|
void main() { |
||||||
|
float shadow = texture2D(m_ShadowMapArray, vec3(texCoord1, m_ShadowMapSlice)).r; |
||||||
|
|
||||||
|
shadow = sqrt(shadow); |
||||||
|
|
||||||
|
// TODO: make it betterer |
||||||
|
gl_FragColor.rgb = vec3(shadow); |
||||||
|
gl_FragColor.a = 1.0; |
||||||
|
} |
@ -0,0 +1,17 @@ |
|||||||
|
MaterialDef Pre Shadow { |
||||||
|
|
||||||
|
MaterialParameters { |
||||||
|
TextureArray ShadowMapArray |
||||||
|
Float ShadowMapSlice |
||||||
|
} |
||||||
|
|
||||||
|
Technique { |
||||||
|
VertexShader GLSL150 : Common/MatDefs/Misc/Unshaded.vert |
||||||
|
FragmentShader GLSL150 : Common/MatDefs/Shadow/ShowShadowArray.frag |
||||||
|
|
||||||
|
WorldParameters { |
||||||
|
WorldViewProjectionMatrix |
||||||
|
ViewProjectionMatrix |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue