Shadows are super awful

I followed this tutorial: . There are some nice shadows on tutorial screenshots.
These are shadows I get on the same code on a simple cube:

My shadowmap size is 1024 and the code is this: pastebin.com/e2H71Wunhub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:light_and_shadow

Wich filtering do you use?

@Empire Phoenix said: Wich filtering do you use?
Bilinear [java] final int SHADOWMAP_SIZE = 1024; DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE, 3); dlsr.setEdgeFilteringMode(EdgeFilteringMode.Bilinear); dlsr.setLight(sun); viewPort.addProcessor(dlsr); [/java]

How big is that scene? Ground and cube size…

Cube is 2 units. Floor is 20 units.
[java]
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.shadow.EdgeFilteringMode;
import com.jme3.system.AppSettings;
import com.jme3.util.TangentBinormalGenerator;

public class Main extends SimpleApplication {

public static void main(String[] args) {
    Main app = new Main();
    AppSettings setting = new AppSettings(true);
    setting.setWidth(1280);
    setting.setHeight(720);
    setting.setFrameRate(120);
    app.setSettings(setting);
    app.setShowSettings(false);
    app.start();
}

@Override
public void simpleInitApp() {
    initLight();
    
    flyCam.setMoveSpeed(5.0f);
    
    Spatial box = makeBox(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
    rootNode.attachChild(box);
    
    Spatial floor = makeBox(-10.0f, -1.05f, -10.0f, 10.0f, -1.0f, 10.0f);
    rootNode.attachChild(floor);
    
    rootNode.setShadowMode(ShadowMode.CastAndReceive);
}

protected Spatial makeBox(float x, float y, float z, float xx, float yy, float zz) {
    Box b = new Box((xx - x) / 2.0f, (yy - y) / 2.0f, (zz - z) / 2.0f);
    Geometry box = new Geometry("Box", b);

    Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    mat.setColor("Diffuse", ColorRGBA.White);
    mat.setColor("Specular", ColorRGBA.White);
    mat.setColor("Ambient", ColorRGBA.White);
    box.setMaterial(mat);
    TangentBinormalGenerator.generate(box);
    
    box.move(x + (xx - x) / 2.0f, y + (yy - y) / 2.0f, z + (zz - z) / 2.0f);
    return box;
}

protected void initLight() {
    viewPort.setBackgroundColor(new ColorRGBA(0.87890f, 0.921875f, 0.9375f, 1.0f));
    
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(1.3f));
    rootNode.addLight(al);
    
    DirectionalLight sun = new DirectionalLight();
    sun.setColor(ColorRGBA.White);
    sun.setDirection(new Vector3f(-1.0f, -2.0f, -3.0f).normalizeLocal());
    rootNode.addLight(sun);
    
    final int SHADOWMAP_SIZE = 1024;
    DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE, 3);
    dlsr.setEdgeFilteringMode(EdgeFilteringMode.Bilinear);
    dlsr.setLight(sun);
    viewPort.addProcessor(dlsr);
}

@Override
public void simpleUpdate(float tpf) {
    //TODO: add update code
}

@Override
public void simpleRender(RenderManager rm) {
    //TODO: add render code
}

}
[/java]

Is it just standing at a bad angle? What happens when you rotate it or make it 4 shadow directions (DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE, 4);)?

@normen said: Is it just standing at a bad angle? What happens when you rotate it or make it 4 shadow directions (DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE, 4);)?
On 4 shadow directions nothing changes. If I change the light direction shadow becomes smooth (as expected, but i need shadows from different angles), however this occurs:

If you want to see things go really wrong, lower your floor a bit

Spatial floor = makeBox(-10.0f, -1.15f, -10.0f, 10.0f, -1.10f, 10.0f);

and observe one of the edges below the box.

@abies said: If you want to see things go really wrong, lower your floor a bit
Spatial floor = makeBox(-10.0f, -1.15f, -10.0f, 10.0f, -1.10f, 10.0f);

and observe one of the edges below the box.


yea :D.

I don’t know how to work around that issue.
The problem is that we need a polygon offset when rendering shadows to avoid z fighting. This is what’s causing the first issue (the offsetted shadow on the box).
This issue is very obvious on cubes (or geometries with hard edges), a lot less on more rounded models. But since cubes are quite popular those days…

The second issue is standard shadow mapping…shadow map rasterization. You don’t need 3 splits for that scene, so use only one. Then use some better filtering (bilinear is pretty lame for shadows anyway).

this may also mitigate the first issue.