Problems with lighting and shadows


#1

Hi!

I have an issue setting up lighting and shadows with my project. I added a simle Directional Light and then a Directional Light Shadow Renderer, but I have no shadows.

Here is my code:

    ViewPort localvp = settings.getViewPort();
    AssetManager localam = settings.getAssetManager();
    
    DirectionalLight sun = new DirectionalLight();
    
    sun.setDirection(new Vector3f(0, 1, 0).normalizeLocal());
    sun.setColor(ColorRGBA.White);
    
    settings.getNode().addLight(sun);
    
    DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(localam, 1024, 2);
    dlsr.setLight(sun);

    localvp.addProcessor(dlsr);

Mesh normals generator:

private void addSquareNormals(List<Float> normals, float normalX, float normalY, float normalZ){
    for(int i=0;i<4;i++){
        normals.add(normalX);
        normals.add(normalY);
        normals.add(normalZ);
    }
}

#2

Did you set any of your objects to cast or receive shadows?


#3

also i belive you might have not desired light direction anyway.


#4

No, how do I set this?


#5

I want a light perpendicular to X-Z plane


#6

geom.setShadowMode(ShadowMode.CastAndReceive);

or just Cast or just Receive, etc


#7

Thanks, I’ll try this in a moment


#8

I want a light perpendicular to X-Z plane

im not 100% sure, but Vector3f(0, 1, 0) is up, while usually you want light go down,
it is Vector3f(0, -1, 0)


#9

Yeah, you’re right!


#10

Yeah, but it still looks pretty ugly.

Note: I changed directional light to X: -1, Y: -1, Z:0


#11

dont realy see much with this screenshot. what this scene look like without shadow?

also i understand you still use .normalizeLocal() that is required

edit:

also try play arround config like:

    dlsf = new DirectionalLightShadowFilter(assetManager, 8192, 3);
    dlsf.setLight(sun);
    dlsf.setEnabledStabilization(true);
    dlsf.setShadowIntensity(0.4f);
    dlsf.setEdgesThickness(1);
    dlsf.setShadowCompareMode(CompareMode.Hardware);
    dlsf.setEdgeFilteringMode(EdgeFilteringMode.PCFPOISSON);

search what will give you better result.

btw. as i know default unit2meter is 1 unit = 1 meter in games. someone mentioned before.


#12

You said you wanted it perpendicular to the X-Z plane so why do you make it parallel instead?

I can’t tell if there is confusion about what “perpendicular” means or what “direction” means.

If light is overhead then 0, -1, 0 is the light pointing straight down, ie: perpendicular to the x-z plane. -1, -1, 0 is not even a direction vector until normalized but will be parallel to the x-z plane as it has no y component at all (the definition of parallel to the x-z plane.)

Edit: also the tutorials are probably useful here: https://wiki.jmonkeyengine.org/jme3/advanced/light_and_shadow.html


#13

Yes, I know that perpendicular light is X:0 Y:-1 Z:0 but I made a 45 degrees light with these parameters, right? I don’t think it’s parallel to the X-Z plane (it should be X:-1 Y:0 Z:0)


#14

The original scene:

Now i’m trying your hints.


#15

i really dont know what this scene is, but its hardly seen there is slope and looks like voxel terrain.

you use some ready2use voxel libs?


#16

It’s a flying box on a voxel terrain


#17

im not 100% sure, but if im correct white area is slope of voxel terrain, and it looks like it have broken texCoords. it might cause shadow issue too.

why cant you make it visible, its like

“guess what it is” :smiley:


#18

It’s a chunk world, so I can’t hide terrain because i’ts connected with that box. Sorry for that, but I’ve not implemented textures yet.


#19

wait, you write own voxel engine?

there are already a lot of them in JME, why not just use one.


#20

Yeah, i know. But i wanted to make one from my own, and I learned a lot from this.