Shadows not aligning properly

Hello fellow developers,

I’ve come across a problem with shaders. The problem is that the shadows look very sloppy on the sword in the foreground, and they’re not aligned with the object. It seems to have a small offset:

If you look at the sword you see some weird shading, and if you look at the green blocks the shadow has some offset.

I am generating the meshes by code that is too long to post here, but the problem may be the normals. I am, however, not sure if this is true or not, as well as how to calculate the normals.

The meshes get generated by checking the nearby blocks. If, for example, top has no block it will generate 4 vertices and 2 triangles. The normals would be: {0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {0, 1}

This is the code for the DirectionalLight and the DirectionalShader:

DirectionalLight sun = new DirectionalLight();
        sun.setColor(ColorRGBA.White);
        sun.setDirection(new Vector3f(-.5f,-0.5f,-.5f).normalizeLocal());
        
        final int SHADOWMAP_SIZE=1024;
        DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE, 3);
        dlsr.setLight(sun);
        dlsr.setEnabledStabilization(false);
        viewPort.addProcessor(dlsr);
        
        DirectionalLightShadowFilter dlsf = new DirectionalLightShadowFilter(assetManager, SHADOWMAP_SIZE, 3);
        dlsf.setLight(sun);
        dlsf.setEnabled(true);
        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
        fpp.addFilter(dlsf);
        viewPort.addProcessor(fpp);

This is the code to generate the side of the mesh that is seen on the screenshot. The naming of variables may be wrong, I am not too sure about that.

Vector3f top_left = new Vector3f(x*scale, y*scale+scale, z*scale+scale);
                                Vector3f top_right = new Vector3f(x*scale, y*scale+scale, z*scale);
                                Vector3f bot_left = new Vector3f(x*scale, y*scale, z*scale+scale);
                                Vector3f bot_right = new Vector3f(x*scale, y*scale, z*scale);

                                verticesArray.add(top_left);
                                verticesArray.add(top_right);
                                verticesArray.add(bot_left);
                                verticesArray.add(bot_right);

                                if(doNormals){
                                    normalsArray.add(scale);
                                    normalsArray.add(0f);
                                    normalsArray.add(0f);

                                    normalsArray.add(scale);
                                    normalsArray.add(0f);
                                    normalsArray.add(0f);

                                    normalsArray.add(scale);
                                    normalsArray.add(0f);
                                    normalsArray.add(0f);

                                    normalsArray.add(scale);
                                    normalsArray.add(0f);
                                    normalsArray.add(0f);
                                }
                                
                                for(int i=0; i<4; i++){
                                    colorsArray.add(r);
                                    colorsArray.add(g);
                                    colorsArray.add(b);
                                    colorsArray.add(a);
                                    /*colorsArray.add(1f);
                                    colorsArray.add(1f);
                                    colorsArray.add(1f);
                                    colorsArray.add(1f);*/
                                }
                                
                                trianglesArray.add(verticesArray.size()-2);
                                trianglesArray.add(verticesArray.size()-4);
                                trianglesArray.add(verticesArray.size()-1);
                                trianglesArray.add(verticesArray.size()-1);
                                trianglesArray.add(verticesArray.size()-4);
                                trianglesArray.add(verticesArray.size()-3);

Thank you very much,
Techdaan.

For that shadow offset try to adjust the PolyOffset values in PreShadow and PostShadow techniques. Just copy the .j3md file, modify it and use in your own material files instead of the original one.

The shadow artifacts are caused by the bright ambient. Surfaces that are not lightened up by the light are darkened by default and should not be visible. I don’t know if it can be solved on the stock shaders and JME’s way of shadowcasting.

Also, they are currently working on better shadows: Improved shadow renderer - #8 by Perjin

Don’t know the current state of this project - just ask in their thread. :chimpanzee_smile:

You can work on current version and ignore the issues. Switch to new renderers when authors finish their job or learn glsl in meantime and write your own shaders, like I did.