Documented com.jme3.shadow

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7631 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 14 years ago
parent bcef1b1e5c
commit 446275775f
  1. 84
      engine/src/desktop-fx/com/jme3/shadow/BasicShadowRenderer.java
  2. 159
      engine/src/desktop-fx/com/jme3/shadow/PssmShadowRenderer.java
  3. 15
      engine/src/desktop-fx/com/jme3/shadow/PssmShadowUtil.java
  4. 19
      engine/src/desktop-fx/com/jme3/shadow/ShadowCamera.java
  5. 166
      engine/src/desktop-fx/com/jme3/shadow/ShadowUtil.java

@ -29,7 +29,6 @@
* 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;
import com.jme3.material.Material;
@ -48,74 +47,98 @@ import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture;
/**
* BasicShadowRenderer uses standard shadow mapping with one map
* it's useful to render shadows in a small scene, but edges might look a bit jagged.
*
* @author Kirill Vainer
*/
public class BasicShadowRenderer implements SceneProcessor {
private RenderManager renderManager;
private ViewPort viewPort;
private FrameBuffer shadowFB;
private Texture2D shadowMap;
private Camera shadowCam;
private Material preshadowMat;
private Material postshadowMat;
private Picture dispPic = new Picture("Picture");
private boolean noOccluders = false;
private Vector3f[] points = new Vector3f[8];
private Vector3f direction = new Vector3f();
public BasicShadowRenderer(AssetManager manager, int size){
shadowFB = new FrameBuffer(size,size,1);
shadowMap = new Texture2D(size,size,Format.Depth);
/**
* Creates a BasicShadowRenderer
* @param manager the asset manager
* @param size the size of the shadow map (the map is square)
*/
public BasicShadowRenderer(AssetManager manager, int size) {
shadowFB = new FrameBuffer(size, size, 1);
shadowMap = new Texture2D(size, size, Format.Depth);
shadowFB.setDepthTexture(shadowMap);
shadowCam = new Camera(size,size);
preshadowMat = new Material(manager, "Common/MatDefs/Shadow/PreShadow.j3md");
shadowCam = new Camera(size, size);
preshadowMat = new Material(manager, "Common/MatDefs/Shadow/PreShadow.j3md");
postshadowMat = new Material(manager, "Common/MatDefs/Shadow/PostShadow.j3md");
postshadowMat.setTexture("ShadowMap", shadowMap);
dispPic.setTexture(manager, shadowMap, false);
for (int i = 0; i < points.length; i++){
for (int i = 0; i < points.length; i++) {
points[i] = new Vector3f();
}
}
public void initialize(RenderManager rm, ViewPort vp){
public void initialize(RenderManager rm, ViewPort vp) {
renderManager = rm;
viewPort = vp;
reshape(vp, vp.getCamera().getWidth(), vp.getCamera().getHeight());
}
public boolean isInitialized(){
public boolean isInitialized() {
return viewPort != null;
}
/**
* returns the light direction used for this processor
* @return
*/
public Vector3f getDirection() {
return direction;
}
/**
* sets the light direction to use to computs shadows
* @param direction
*/
public void setDirection(Vector3f direction) {
this.direction.set(direction).normalizeLocal();
}
/**
* debug only
* @return
*/
public Vector3f[] getPoints() {
return points;
}
public Camera getShadowCamera(){
/**
* debug only
* returns the shadow camera
* @return
*/
public Camera getShadowCamera() {
return shadowCam;
}
public void postQueue(RenderQueue rq){
public void postQueue(RenderQueue rq) {
GeometryList occluders = rq.getShadowQueueContent(ShadowMode.Cast);
if (occluders.size() == 0){
if (occluders.size() == 0) {
noOccluders = true;
return;
}else{
} else {
noOccluders = false;
}
@ -124,13 +147,13 @@ public class BasicShadowRenderer implements SceneProcessor {
// update frustum points based on current camera
Camera viewCam = viewPort.getCamera();
ShadowUtil.updateFrustumPoints(viewCam,
viewCam.getFrustumNear(),
viewCam.getFrustumFar(),
1.0f,
points);
viewCam.getFrustumNear(),
viewCam.getFrustumFar(),
1.0f,
points);
Vector3f frustaCenter = new Vector3f();
for (Vector3f point : points){
for (Vector3f point : points) {
frustaCenter.addLocal(point);
}
frustaCenter.multLocal(1f / 8f);
@ -139,7 +162,7 @@ public class BasicShadowRenderer implements SceneProcessor {
shadowCam.setProjectionMatrix(null);
shadowCam.setParallelProjection(true);
// shadowCam.setFrustumPerspective(45, 1, 1, 20);
shadowCam.lookAtDirection(direction, Vector3f.UNIT_Y);
shadowCam.update();
shadowCam.setLocation(frustaCenter);
@ -154,7 +177,7 @@ public class BasicShadowRenderer implements SceneProcessor {
renderManager.setForcedMaterial(preshadowMat);
r.setFrameBuffer(shadowFB);
r.clearBuffers(false,true,false);
r.clearBuffers(false, true, false);
viewPort.getQueue().renderShadowQueue(ShadowMode.Cast, renderManager, shadowCam, true);
r.setFrameBuffer(viewPort.getOutputFrameBuffer());
@ -162,12 +185,16 @@ public class BasicShadowRenderer implements SceneProcessor {
renderManager.setCamera(viewCam, false);
}
public Picture getDisplayPicture(){
/**
* debug only
* @return
*/
public Picture getDisplayPicture() {
return dispPic;
}
public void postFrame(FrameBuffer out){
if (!noOccluders){
public void postFrame(FrameBuffer out) {
if (!noOccluders) {
postshadowMat.setMatrix4("LightViewProjectionMatrix", shadowCam.getViewProjectionMatrix());
renderManager.setForcedMaterial(postshadowMat);
viewPort.getQueue().renderShadowQueue(ShadowMode.Receive, renderManager, viewPort.getCamera(), true);
@ -186,5 +213,4 @@ public class BasicShadowRenderer implements SceneProcessor {
dispPic.setWidth(w / 5f);
dispPic.setHeight(h / 5f);
}
}

@ -29,7 +29,6 @@
* 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;
import com.jme3.material.Material;
@ -57,36 +56,42 @@ import com.jme3.texture.Texture.ShadowCompareMode;
import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture;
/**
* PssmShadow renderer use Parrallel Split Shadow Mapping technique (pssm)<br>
* It splits the view frustum in several parts and compute a shadow map for each one.<br>
* splits are distributed so that the closer they are from the camera, the smaller they are to maximize the resolution used of the shadow map.<br>
* This result in a better quality shadow than standard shadow mapping.<br>
* for more informations on this read this http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html<br>
*
* @author Rémy Bouquet aka Nehon
*/
public class PssmShadowRenderer implements SceneProcessor {
/**
* <code>FilterMode</code> specifies how shadows are filtered
*/
public enum FilterMode {
/**
* Shadows are not filtered. Nearest sample is used, causing in blocky
* shadows.
*/
Nearest,
/**
* Bilinear filtering is used. Has the potential of being hardware
* accelerated on some GPUs
*/
Bilinear,
/**
* Dither-based sampling is used, very cheap but can look bad
* at low resolutions.
*/
Dither,
/**
* 4x4 percentage-closer filtering is used. Shadows will be smoother
* at the cost of performance
*/
PCF4,
/**
* 8x8 percentage-closer filtering is used. Shadows will be smoother
* at the cost of performance
@ -94,19 +99,21 @@ public class PssmShadowRenderer implements SceneProcessor {
PCF8
}
/**
* Specifies the shadow comparison mode
*/
public enum CompareMode {
/**
* Shadow depth comparisons are done by using shader code
*/
Software,
/**
* Shadow depth comparisons are done by using the GPU's dedicated
* shadowing pipeline.
*/
Hardware;
}
private int nbSplits = 3;
private float lambda = 0.65f;
private float shadowIntensity = 0.7f;
@ -119,21 +126,17 @@ public class PssmShadowRenderer implements SceneProcessor {
private Camera shadowCam;
private Material preshadowMat;
private Material postshadowMat;
private GeometryList splitOccluders = new GeometryList(new OpaqueComparator());
private Matrix4f[] lightViewProjectionsMatrices;
private ColorRGBA splits;
private float[] splitsArray;
private boolean noOccluders = false;
private Vector3f direction = new Vector3f();
private AssetManager assetManager;
private boolean debug = false;
private float edgesThickness = 1.0f;
private FilterMode filterMode;
private CompareMode compareMode;
private Picture[] dispPic;
private Vector3f[] points = new Vector3f[8];
@ -154,26 +157,26 @@ public class PssmShadowRenderer implements SceneProcessor {
dispPic = new Picture[nbSplits];
lightViewProjectionsMatrices = new Matrix4f[nbSplits];
splits = new ColorRGBA();
splitsArray = new float[nbSplits+1];
splitsArray = new float[nbSplits + 1];
//DO NOT COMMENT THIS (it prevent the OSX incomplete read buffer crash)
dummyTex= new Texture2D(size, size, Format.RGBA8);
dummyTex = new Texture2D(size, size, Format.RGBA8);
preshadowMat = new Material(manager, "Common/MatDefs/Shadow/PreShadow.j3md");
preshadowMat = new Material(manager, "Common/MatDefs/Shadow/PreShadow.j3md");
postshadowMat = new Material(manager, "Common/MatDefs/Shadow/PostShadowPSSM.j3md");
for (int i = 0; i < nbSplits; i++) {
lightViewProjectionsMatrices[i] = new Matrix4f();
shadowFB[i] = new FrameBuffer(size, size, 1);
shadowMaps[i] = new Texture2D(size, size, Format.Depth);
shadowFB[i].setDepthTexture(shadowMaps[i]);
//DO NOT COMMENT THIS (it prevent the OSX incomplete read buffer crash)
shadowFB[i].setColorTexture(dummyTex);
postshadowMat.setTexture("ShadowMap" + i, shadowMaps[i]);
//quads for debuging purpose
dispPic[i] = new Picture("Picture" + i);
dispPic[i].setTexture(manager, shadowMaps[i], false);
@ -190,22 +193,28 @@ public class PssmShadowRenderer implements SceneProcessor {
}
}
public void setFilterMode(FilterMode filterMode){
if (filterMode == null)
/**
* Sets the filtering mode for shadow edges see {@link FilterMode} for more info
* @param filterMode
*/
public void setFilterMode(FilterMode filterMode) {
if (filterMode == null) {
throw new NullPointerException();
}
if (this.filterMode == filterMode)
if (this.filterMode == filterMode) {
return;
}
this.filterMode = filterMode;
postshadowMat.setInt("FilterMode", filterMode.ordinal());
postshadowMat.setFloat("PCFEdge", edgesThickness);
if (compareMode == CompareMode.Hardware){
for (Texture2D shadowMap : shadowMaps){
if (filterMode == FilterMode.Bilinear){
if (compareMode == CompareMode.Hardware) {
for (Texture2D shadowMap : shadowMaps) {
if (filterMode == FilterMode.Bilinear) {
shadowMap.setMagFilter(MagFilter.Bilinear);
shadowMap.setMinFilter(MinFilter.BilinearNoMipMaps);
}else{
} else {
shadowMap.setMagFilter(MagFilter.Nearest);
shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
}
@ -213,25 +222,31 @@ public class PssmShadowRenderer implements SceneProcessor {
}
}
/**
* sets the shadow compare mode see {@link CompareMode} for more info
* @param compareMode
*/
public void setCompareMode(CompareMode compareMode) {
if (compareMode == null)
if (compareMode == null) {
throw new NullPointerException();
}
if (this.compareMode == compareMode)
if (this.compareMode == compareMode) {
return;
}
this.compareMode = compareMode;
for (Texture2D shadowMap : shadowMaps){
if (compareMode == CompareMode.Hardware){
for (Texture2D shadowMap : shadowMaps) {
if (compareMode == CompareMode.Hardware) {
shadowMap.setShadowCompareMode(ShadowCompareMode.LessOrEqual);
if (filterMode == FilterMode.Bilinear){
if (filterMode == FilterMode.Bilinear) {
shadowMap.setMagFilter(MagFilter.Bilinear);
shadowMap.setMinFilter(MinFilter.BilinearNoMipMaps);
}else{
} else {
shadowMap.setMagFilter(MagFilter.Nearest);
shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
}
} else{
} else {
shadowMap.setShadowCompareMode(ShadowCompareMode.Off);
shadowMap.setMagFilter(MagFilter.Nearest);
shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
@ -280,30 +295,40 @@ public class PssmShadowRenderer implements SceneProcessor {
return viewPort != null;
}
/**
* returns the light direction used by the processor
* @return
*/
public Vector3f getDirection() {
return direction;
}
/**
* Sets the light direction to use to compute shadows
* @param direction
*/
public void setDirection(Vector3f direction) {
this.direction.set(direction).normalizeLocal();
}
@SuppressWarnings("fallthrough")
@SuppressWarnings("fallthrough")
public void postQueue(RenderQueue rq) {
GeometryList occluders = rq.getShadowQueueContent(ShadowMode.Cast);
if (occluders.size() == 0)
GeometryList occluders = rq.getShadowQueueContent(ShadowMode.Cast);
if (occluders.size() == 0) {
return;
}
GeometryList receivers = rq.getShadowQueueContent(ShadowMode.Receive);
if (receivers.size() == 0)
if (receivers.size() == 0) {
return;
}
Camera viewCam = viewPort.getCamera();
float zFar = zFarOverride;
if (zFar == 0) {
zFar=viewCam.getFrustumFar();
// zFar = PssmShadowUtil.computeZFar(occluders, receivers, viewCam);
zFar = viewCam.getFrustumFar();
// zFar = PssmShadowUtil.computeZFar(occluders, receivers, viewCam);
}
// System.out.println("Zfar : "+zFar);
ShadowUtil.updateFrustumPoints(viewCam, viewCam.getFrustumNear(), zFar, 1.0f, points);
@ -322,7 +347,7 @@ public class PssmShadowRenderer implements SceneProcessor {
PssmShadowUtil.updateFrustumSplits(splitsArray, viewCam.getFrustumNear(), zFar, lambda);
switch (splitsArray.length){
switch (splitsArray.length) {
case 5:
splits.a = splitsArray[4];
case 4:
@ -345,25 +370,7 @@ public class PssmShadowRenderer implements SceneProcessor {
ShadowUtil.updateFrustumPoints(viewCam, splitsArray[i], splitsArray[i + 1], 1.0f, points);
//Updating shadow cam with curent split frustra
// if(cropShadows){
ShadowUtil.updateShadowCamera(occluders, receivers, shadowCam, points, splitOccluders);
// }else{
// ShadowUtil.updateShadowCamera(shadowCam, points);
// }
//displaying the current splitted frustrum and the associated cropped light frustrums in wireframe.
//only for debuging purpose
// if (debug) {
// viewPort.attachScene(createFrustum(points, i));
// Vector3f[] pts = new Vector3f[8];
// for (int j = 0; j < pts.length; j++) {
// pts[j] = new Vector3f();
// }
// ShadowUtil.updateFrustumPoints2(shadowCam, pts);
// viewPort.attachScene(createFrustum(pts, i));
// if (i == nbSplits-1) {
// debug = false;
// }
// }
ShadowUtil.updateShadowCamera(occluders, receivers, shadowCam, points, splitOccluders);
//saving light view projection matrix for this split
lightViewProjectionsMatrices[i] = shadowCam.getViewProjectionMatrix().clone();
@ -373,9 +380,7 @@ public class PssmShadowRenderer implements SceneProcessor {
r.clearBuffers(false, true, false);
// render shadow casters to shadow map
viewPort.getQueue().renderShadowQueue(splitOccluders, renderManager, shadowCam, true);
//viewPort.getQueue().renderShadowQueue(ShadowMode.Cast, renderManager, shadowCam, i == nbSplits - 1);
viewPort.getQueue().renderShadowQueue(splitOccluders, renderManager, shadowCam, true);
}
occluders.clear();
//restore setting for future rendering
@ -383,11 +388,11 @@ public class PssmShadowRenderer implements SceneProcessor {
renderManager.setForcedMaterial(null);
renderManager.setForcedTechnique(null);
renderManager.setCamera(viewCam, false);
}
//debug only : displays depth shadow maps
public void displayShadowMap(Renderer r) {
private void displayShadowMap(Renderer r) {
Camera cam = viewPort.getCamera();
renderManager.setCamera(cam, true);
int h = cam.getHeight();
@ -421,8 +426,9 @@ public class PssmShadowRenderer implements SceneProcessor {
renderManager.setCamera(cam, false);
}
if (debug)
if (debug) {
displayShadowMap(renderManager.getRenderer());
}
}
public void preFrame(float tpf) {
@ -434,6 +440,11 @@ public class PssmShadowRenderer implements SceneProcessor {
public void reshape(ViewPort vp, int w, int h) {
}
/**
* returns the labda parameter<br>
* see {@link setLambda(float lambda)}
* @return
*/
public float getLambda() {
return lambda;
}
@ -450,6 +461,11 @@ public class PssmShadowRenderer implements SceneProcessor {
this.lambda = lambda;
}
/**
* How far the shadows are rendered in the view
* see {@link setShadowZExtend(float zFar)}
* @return
*/
public float getShadowZExtend() {
return zFarOverride;
}
@ -463,6 +479,11 @@ public class PssmShadowRenderer implements SceneProcessor {
this.zFarOverride = zFar;
}
/**
* returns the shdaow intensity<br>
* see {@link setShadowIntensity(float shadowIntensity)}
* @return
*/
public float getShadowIntensity() {
return shadowIntensity;
}
@ -479,14 +500,22 @@ public class PssmShadowRenderer implements SceneProcessor {
postshadowMat.setFloat("ShadowIntensity", shadowIntensity);
}
/**
* returns the edges thickness <br>
* see {@link setEdgesThickness(int edgesThickness)}
* @return
*/
public int getEdgesThickness() {
return (int) (edgesThickness * 10);
}
/**
* Stes the shadow edges thickness. default is 1, setting it to lower values can help to reduce the jagged effect of the shadow edges
* @param edgesThickness
*/
public void setEdgesThickness(int edgesThickness) {
this.edgesThickness = Math.max(1, Math.min(edgesThickness,10));
this.edgesThickness = Math.max(1, Math.min(edgesThickness, 10));
this.edgesThickness *= 0.1f;
postshadowMat.setFloat("PCFEdge", edgesThickness);
}
}

@ -29,7 +29,6 @@
* 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;
import com.jme3.bounding.BoundingBox;
@ -52,18 +51,6 @@ import static java.lang.Math.*;
*/
public final class PssmShadowUtil {
public static void main(String[] args){
float[] splits = new float[5];
float[] splitsShader = new float[3];
updateFrustumSplits(splits, 1, 1000, 0.5f);
System.arraycopy(splits, 1, splitsShader, 0, splitsShader.length);
System.out.println(Arrays.toString(splitsShader));
for (int i = 0; i < splits.length-1; i++){
System.out.println(splits[i] + " - " + splits[i+1]);
}
}
/**
* Updates the frustum splits stores in <code>splits</code> using PSSM.
*/
@ -86,7 +73,7 @@ public final class PssmShadowUtil {
*/
public static float computeZFar(GeometryList occ, GeometryList recv, Camera cam) {
Matrix4f mat = cam.getViewMatrix();
BoundingBox bbOcc = ShadowUtil.computeUnionBound(occ, mat);
BoundingBox bbOcc = ShadowUtil.computeUnionBound(occ, mat);
BoundingBox bbRecv = ShadowUtil.computeUnionBound(recv, mat);
return min(max(bbOcc.getZExtent() - bbOcc.getCenter().z, bbRecv.getZExtent() - bbRecv.getCenter().z), cam.getFrustumFar());

@ -29,7 +29,6 @@
* 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;
import com.jme3.light.DirectionalLight;
@ -38,14 +37,19 @@ import com.jme3.light.PointLight;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
/**
* Creates a camera according to a light
* Handy to compute projection matrix of a light
* @author Kirill Vainer
*/
public class ShadowCamera {
private Vector3f[] points = new Vector3f[8];
private Light target;
public ShadowCamera(Light target){
public ShadowCamera(Light target) {
this.target = target;
for (int i = 0; i < points.length; i++){
for (int i = 0; i < points.length; i++) {
points[i] = new Vector3f();
}
}
@ -53,14 +57,14 @@ public class ShadowCamera {
/**
* Updates the camera view direction and position based on the light
*/
private void updateLightCamera(Camera lightCam){
if (target.getType() == Light.Type.Directional){
public void updateLightCamera(Camera lightCam) {
if (target.getType() == Light.Type.Directional) {
DirectionalLight dl = (DirectionalLight) target;
lightCam.setParallelProjection(true);
lightCam.setLocation(Vector3f.ZERO);
lightCam.lookAtDirection(dl.getDirection(), Vector3f.UNIT_Y );
lightCam.lookAtDirection(dl.getDirection(), Vector3f.UNIT_Y);
lightCam.setFrustum(-1, 1, -1, 1, 1, -1);
}else{
} else {
PointLight pl = (PointLight) target;
lightCam.setParallelProjection(false);
lightCam.setLocation(pl.getPosition());
@ -68,5 +72,4 @@ public class ShadowCamera {
lightCam.setFrustumPerspective(45, 1, 1, 300);
}
}
}

@ -29,12 +29,10 @@
* 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;
import com.jme3.bounding.BoundingBox;
import com.jme3.bounding.BoundingVolume;
import com.jme3.math.FastMath;
import com.jme3.math.Matrix4f;
import com.jme3.math.Transform;
import com.jme3.math.Vector2f;
@ -58,7 +56,12 @@ import static java.lang.Math.*;
*/
public class ShadowUtil {
public static void updateFrustumPoints2(Camera viewCam, Vector3f[] points){
/**
* Updates a points arrays with the frustum corners of the provided camera.
* @param viewCam
* @param points
*/
public static void updateFrustumPoints2(Camera viewCam, Vector3f[] points) {
int w = viewCam.getWidth();
int h = viewCam.getHeight();
float n = viewCam.getFrustumNear();
@ -68,7 +71,7 @@ public class ShadowUtil {
points[1].set(viewCam.getWorldCoordinates(new Vector2f(0, h), n));
points[2].set(viewCam.getWorldCoordinates(new Vector2f(w, h), n));
points[3].set(viewCam.getWorldCoordinates(new Vector2f(w, 0), n));
points[4].set(viewCam.getWorldCoordinates(new Vector2f(0, 0), f));
points[5].set(viewCam.getWorldCoordinates(new Vector2f(0, h), f));
points[6].set(viewCam.getWorldCoordinates(new Vector2f(w, h), f));
@ -87,10 +90,10 @@ public class ShadowUtil {
* @param farOverride
*/
public static void updateFrustumPoints(Camera viewCam,
float nearOverride,
float farOverride,
float scale,
Vector3f[] points) {
float nearOverride,
float farOverride,
float scale,
Vector3f[] points) {
Vector3f pos = viewCam.getLocation();
Vector3f dir = viewCam.getDirection();
@ -160,6 +163,12 @@ public class ShadowUtil {
}
}
/**
* Compute bounds of a geomList
* @param list
* @param transform
* @return
*/
public static BoundingBox computeUnionBound(GeometryList list, Transform transform) {
BoundingBox bbox = new BoundingBox();
for (int i = 0; i < list.size(); i++) {
@ -173,6 +182,12 @@ public class ShadowUtil {
return bbox;
}
/**
* Compute bounds of a geomList
* @param list
* @param mat
* @return
*/
public static BoundingBox computeUnionBound(GeometryList list, Matrix4f mat) {
BoundingBox bbox = new BoundingBox();
BoundingVolume store = null;
@ -187,6 +202,11 @@ public class ShadowUtil {
return bbox;
}
/**
* Computes the bounds of multiple bounding volumes
* @param bv
* @return
*/
public static BoundingBox computeUnionBound(List<BoundingVolume> bv) {
BoundingBox bbox = new BoundingBox();
for (int i = 0; i < bv.size(); i++) {
@ -196,6 +216,12 @@ public class ShadowUtil {
return bbox;
}
/**
* Compute bounds from an array of points
* @param pts
* @param transform
* @return
*/
public static BoundingBox computeBoundForPoints(Vector3f[] pts, Transform transform) {
Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
@ -211,6 +237,12 @@ public class ShadowUtil {
return new BoundingBox(center, extent.x, extent.y, extent.z);
}
/**
* Compute bounds from an array of points
* @param pts
* @param mat
* @return
*/
public static BoundingBox computeBoundForPoints(Vector3f[] pts, Matrix4f mat) {
Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
@ -228,16 +260,10 @@ public class ShadowUtil {
max.maxLocal(temp);
}
// min.x = FastMath.clamp(min.x, -1f, 1f);
// max.y = FastMath.clamp(max.y, -1f, 1f);
// min.x = FastMath.clamp(min.x, -1f, 1f);
// max.y = FastMath.clamp(max.y, -1f, 1f);
Vector3f center = min.add(max).multLocal(0.5f);
Vector3f extent = max.subtract(min).multLocal(0.5f);
//Nehon 08/18/2010 : Added an offset to the extend to avoid banding artifacts when the frustum are aligned
return new BoundingBox(center, extent.x + 2.0f, extent.y + 2.0f, extent.z +2.5f);
//return new BoundingBox(center, extent.x, extent.y, extent.z);
return new BoundingBox(center, extent.x + 2.0f, extent.y + 2.0f, extent.z + 2.5f);
}
/**
@ -248,7 +274,7 @@ public class ShadowUtil {
* @param lightCam
* @param points
*/
public static void updateShadowCamera(Camera shadowCam, Vector3f[] points){
public static void updateShadowCamera(Camera shadowCam, Vector3f[] points) {
boolean ortho = shadowCam.isParallelProjection();
shadowCam.setProjectionMatrix(null);
@ -265,7 +291,7 @@ public class ShadowUtil {
Vector3f splitMin = splitBB.getMin(null);
Vector3f splitMax = splitBB.getMax(null);
// splitMin.z = 0;
// Create the crop matrix.
@ -280,9 +306,9 @@ public class ShadowUtil {
offsetZ = -splitMin.z * scaleZ;
Matrix4f cropMatrix = new Matrix4f(scaleX, 0f, 0f, offsetX,
0f, scaleY, 0f, offsetY,
0f, 0f, scaleZ, offsetZ,
0f, 0f, 0f, 1f);
0f, scaleY, 0f, offsetY,
0f, 0f, scaleZ, offsetZ,
0f, 0f, 0f, 1f);
Matrix4f result = new Matrix4f();
@ -302,9 +328,9 @@ public class ShadowUtil {
* @param points
*/
public static void updateShadowCamera(GeometryList occluders,
GeometryList receivers,
Camera shadowCam,
Vector3f[] points){
GeometryList receivers,
Camera shadowCam,
Vector3f[] points) {
updateShadowCamera(occluders, receivers, shadowCam, points, null);
}
@ -318,51 +344,48 @@ public class ShadowUtil {
* @param points
*/
public static void updateShadowCamera(GeometryList occluders,
GeometryList receivers,
Camera shadowCam,
Vector3f[] points,
GeometryList splitOccluders){
GeometryList receivers,
Camera shadowCam,
Vector3f[] points,
GeometryList splitOccluders) {
boolean ortho = shadowCam.isParallelProjection();
shadowCam.setProjectionMatrix(null);
if (ortho){
if (ortho) {
shadowCam.setFrustum(-1, 1, -1, 1, 1, -1);
}else{
} else {
shadowCam.setFrustumPerspective(45, 1, 1, 150);
}
// create transform to rotate points to viewspace
//Transform t = new Transform(shadowCam.getRotation());
// create transform to rotate points to viewspace
Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix();
// BoundingBox casterBB = computeUnionBound(occluders, viewProjMatrix);
// BoundingBox receiverBB = computeUnionBound(receivers, viewProjMatrix);
BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);
BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);
ArrayList<BoundingVolume> visRecvList = new ArrayList<BoundingVolume>();
for (int i = 0; i < receivers.size(); i++){
for (int i = 0; i < receivers.size(); i++) {
// convert bounding box to light's viewproj space
Geometry receiver = receivers.get(i);
BoundingVolume bv = receiver.getWorldBound();
BoundingVolume recvBox = bv.transform(viewProjMatrix, null);
if (splitBB.intersects(recvBox)){
if (splitBB.intersects(recvBox)) {
visRecvList.add(recvBox);
}
}
ArrayList<BoundingVolume> visOccList = new ArrayList<BoundingVolume>();
for (int i = 0; i < occluders.size(); i++){
for (int i = 0; i < occluders.size(); i++) {
// convert bounding box to light's viewproj space
Geometry occluder = occluders.get(i);
BoundingVolume bv = occluder.getWorldBound();
BoundingVolume occBox = bv.transform(viewProjMatrix, null);
boolean intersects = splitBB.intersects(occBox);
if (!intersects && occBox instanceof BoundingBox){
BoundingBox occBB = (BoundingBox)occBox;
if (!intersects && occBox instanceof BoundingBox) {
BoundingBox occBB = (BoundingBox) occBox;
//Kirill 01/10/2011
// Extend the occluder further into the frustum
// This fixes shadow dissapearing issues when
@ -371,26 +394,26 @@ public class ShadowUtil {
// The number is in world units
occBB.setZExtent(occBB.getZExtent() + 50);
occBB.setCenter(occBB.getCenter().addLocal(0, 0, 25));
if (splitBB.intersects(occBB)){
if (splitBB.intersects(occBB)) {
// To prevent extending the depth range too much
// We return the bound to its former shape
// Before adding it
occBB.setZExtent(occBB.getZExtent() - 50);
occBB.setCenter(occBB.getCenter().subtractLocal(0, 0, 25));
visOccList.add(occBox);
if(splitOccluders != null){
if (splitOccluders != null) {
splitOccluders.add(occluder);
}
}
}else if (intersects){
} else if (intersects) {
visOccList.add(occBox);
if(splitOccluders != null){
if (splitOccluders != null) {
splitOccluders.add(occluder);
}
}
}
BoundingBox casterBB = computeUnionBound(visOccList);
BoundingBox casterBB = computeUnionBound(visOccList);
BoundingBox receiverBB = computeUnionBound(visRecvList);
//Nehon 08/18/2010 this is to avoid shadow bleeding when the ground is set to only receive shadows
@ -409,16 +432,12 @@ public class ShadowUtil {
Vector3f splitMin = splitBB.getMin(null);
Vector3f splitMax = splitBB.getMax(null);
// actualMin.x = FastMath.clamp(actualMin.x, -1, 1);
// actualMin.y = FastMath.clamp(actualMin.y, -1, 1);
// actualMax.x = FastMath.clamp(actualMax.x, -1, 1);
// actualMax.y = FastMath.clamp(actualMax.y, -1, 1);
// float far = actualMin.z + actualMax.z * 4 + 1.0f + 1.5f;
splitMin.z = 0;
if (!ortho)
if (!ortho) {
shadowCam.setFrustumPerspective(45, 1, 1, splitMax.z);
}
Matrix4f projMatrix = shadowCam.getProjectionMatrix();
Vector3f cropMin = new Vector3f();
@ -434,11 +453,6 @@ public class ShadowUtil {
cropMin.z = min(casterMin.z, splitMin.z);
cropMax.z = min(receiverMax.z, splitMax.z);
// cropMin.set(splitMin);
// cropMax.set(splitMax);
// cropMin.z = Math.min(cropMin.z, cropMax.z - cropMin.z - 1000);
// cropMin.z = Math.max(10f, cropMin.z);
// Create the crop matrix.
float scaleX, scaleY, scaleZ;
@ -446,48 +460,26 @@ public class ShadowUtil {
scaleX = (2.0f) / (cropMax.x - cropMin.x);
scaleY = (2.0f) / (cropMax.y - cropMin.y);
offsetX = -0.5f * (cropMax.x + cropMin.x) * scaleX;
offsetY = -0.5f * (cropMax.y + cropMin.y) * scaleY;
scaleZ = 1.0f / (cropMax.z - cropMin.z);
offsetZ = -cropMin.z * scaleZ;
// scaleZ = 2.0f / (cropMax.z - cropMin.z);
// offsetZ = -0.5f * (cropMax.z + cropMin.z) * scaleZ;
Matrix4f cropMatrix = new Matrix4f(scaleX, 0f, 0f, offsetX,
0f, scaleY, 0f, offsetY,
0f, 0f, scaleZ, offsetZ,
0f, 0f, 0f, 1f);
// cropMatrix.transposeLocal();
// Matrix4f cropMatrix = new Matrix4f();
// cropMatrix.setScale(new Vector3f(scaleX, scaleY, 1f));
// cropMatrix.setTranslation(offsetX, offsetY, 0);
Matrix4f cropMatrix = new Matrix4f(scaleX, 0f, 0f, offsetX,
0f, scaleY, 0f, offsetY,
0f, 0f, scaleZ, offsetZ,
0f, 0f, 0f, 1f);
Matrix4f result = new Matrix4f();
result.set(cropMatrix);
result.multLocal(projMatrix);
// result.set(projMatrix);
// result.multLocal(cropMatrix);
shadowCam.setProjectionMatrix(result);
// shadowCam.setFrustum(cropMin.z, cropMax.z, // near, far
// cropMin.x, cropMax.x, // left, right
// cropMax.y, cropMin.y); // top, bottom
// compute size and center of final frustum
//float sizeX = (max.x - min.x) / 2f;
//float sizeY = (max.y - min.y) / 2f;
//float offsetX = (max.x + min.x) / -2f;
//float offsetY = (max.y + min.y) / -2f;
// compute center for frustum
//temp.set(offsetX, offsetY, 0);
//invRot.mult(temp, temp);
shadowCam.setProjectionMatrix(result);
//shadowCam.setLocation(temp);
//shadowCam.setFrustum(min.z, max.z, -sizeX, sizeX, sizeY, -sizeY);
}
}

Loading…
Cancel
Save