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.