Shadows

Hi all.

I have a problem with the shadows.

I leave a test and some screenshots.

[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.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer;
import com.jme3.shadow.PssmShadowRenderer;
import com.jme3.system.AppSettings;
import com.jme3.util.BufferUtils;
import jme3tools.optimize.GeometryBatchFactory;

public class Sombras extends SimpleApplication {

public static void main(String[] args) {
    Sombras app = new Sombras();
    app.setShowSettings(false);
    
    AppSettings settings = new AppSettings(true);
    settings.put("Width", 1024);
    settings.put("Height", 576);
    settings.put("VSync", true);
    //Anti-Aliasing
    settings.put("Samples", 0);

    app.setSettings(settings);
    
    app.start();
}

@Override
public void simpleInitApp() {
    flyCam.setMoveSpeed(20f);  
    
    // We add light so we see the scene
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(0.6f));
    rootNode.addLight(al);

    DirectionalLight dl = new DirectionalLight();
    dl.setColor(ColorRGBA.White.mult(0.5f));
    dl.setDirection(new Vector3f(-.5f,-.5f,-.5f).normalizeLocal());
    rootNode.addLight(dl);
    
    PssmShadowRenderer pssmRenderer = new PssmShadowRenderer(assetManager, 1024, 3);
    pssmRenderer.setDirection(new Vector3f(-.5f,-.5f,-.5f).normalizeLocal()); // light direction
    pssmRenderer.setShadowIntensity(0.8f);
    pssmRenderer.setEdgesThickness(1);
    viewPort.addProcessor(pssmRenderer);

    
    Node bloque = makeQuad(6);
    
    Node terreno = new Node("terreno");
    
    Node optimizar = new Node("optimizar");
    
    
    for (int x=0;x<6;x++){
        for (int z=0;z<6;z++){
            Spatial bloqueClonado = bloque.clone();
            bloqueClonado.move(x*6,-6,z*6);
            optimizar.attachChild(bloqueClonado);
        }
    }
    
    Spatial bloqueClonado = bloque.clone();
    bloqueClonado.move(18,0,18);
    optimizar.attachChild(bloqueClonado);
    
    bloqueClonado = bloque.clone();
    bloqueClonado.move(12,0,18);
    optimizar.attachChild(bloqueClonado);
    
    bloqueClonado = bloque.clone();
    bloqueClonado.move(12,6,18);
    optimizar.attachChild(bloqueClonado);
    
    Spatial optimizadoFinal = GeometryBatchFactory.optimize(optimizar);
    
    terreno.attachChild(optimizadoFinal);
    
    terreno.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
    
    rootNode.attachChild(terreno);
}

public Node makeQuad(int tamano) {        
    Node bloque = new Node("bloque");
    
    for (int i = 0; i<6; i++){
        Mesh m = new Mesh();

        //VERTICES -  posiciones en el espacio
        Vector3f [] vertices = new Vector3f[4];
        vertices[0] = new Vector3f(0,0,0);
        vertices[1] = new Vector3f(tamano,0,0);
        vertices[2] = new Vector3f(0,tamano,0);
        vertices[3] = new Vector3f(tamano,tamano,0);

        //TEXTURAS - Posiciones de las texturas
        Vector2f [] texCoord = new Vector2f[4];
        texCoord[0] = new Vector2f(0,0);
        texCoord[1] = new Vector2f(1,0);
        texCoord[2] = new Vector2f(0,1);
        texCoord[3] = new Vector2f(1,1);

        //NORMALES - para la iluminacion
        float[] normals = new float[]{0,0,1, 0,0,1, 0,0,1, 0,0,1};
        
        //INDICES -  Indices de los vertices, en que orden se construyen
        int [] indexes = {2,0,1,1,3,2};
        
        // Setting buffers
        m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
        m.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
        m.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(indexes));
        m.setBuffer(VertexBuffer.Type.Normal, 3, BufferUtils.createFloatBuffer(normals));
        m.updateBound();
        m.setStatic();

        //Geometry cara = new Geometry("Cara-"+String.valueOf(i)+"-Bloque-x-y", m);   
        Geometry cara = new Geometry("Cara-"+String.valueOf(i), m);   

        //colocamos las caras en su sitio
        switch(i){
            case 0: //cara 1
                //esta ya va bien de serie
            break;
            case 1: //cara 2
               cara.move(tamano,0,0); 
               cara.setLocalRotation(new Quaternion().fromAngleAxis(90*FastMath.DEG_TO_RAD, new Vector3f(0,tamano,0)));
            break;
            case 2: //cara 3
               cara.move(tamano,0,-tamano); 
               cara.setLocalRotation(new Quaternion().fromAngleAxis(180*FastMath.DEG_TO_RAD, new Vector3f(0,tamano,0)));
            break;
            case 3: //cara 4
               cara.move(0,0,-tamano); 
               cara.setLocalRotation(new Quaternion().fromAngleAxis(-90*FastMath.DEG_TO_RAD, new Vector3f(0,tamano,0)));
            break;
            case 4: //cara 5
                cara.move(0,tamano,0); 
                cara.setLocalRotation(new Quaternion().fromAngleAxis(-90*FastMath.DEG_TO_RAD, new Vector3f(tamano,0,0)));
            break;
            case 5: //cara 6
                cara.move(0,0,-tamano); 
                cara.setLocalRotation(new Quaternion().fromAngleAxis(90*FastMath.DEG_TO_RAD, new Vector3f(tamano,0,0)));
            break;
            
        }
        
        bloque.attachChild(cara); 
    }
    
    Material mat1 = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); 
    //jm3.testdata
    mat1.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));   

    //luces
    mat1.setBoolean("UseMaterialColors", true);
    mat1.setColor("Ambient",  ColorRGBA.White);
    mat1.setColor("Diffuse",  ColorRGBA.White);
    mat1.setColor("Specular", ColorRGBA.White);
    mat1.setFloat("Shininess", 1f);

    bloque.setMaterial(mat1);
    
    return bloque;
}

}
[/java]

Any suggestions?

Thanks and regards.

Turning on smoothing on the shadows will help a lot, what you are seeing is unavoidable artifacts of the way shadows work though. All you can do is find the right settings to mitigate them.

what @zarch said, and the first artifact will be a lot less noticeable if you make your ambient light and your shadow intensity match a little better.
The shadows are too dark and the ambient light is too bright.