How can I make shadows not only from a DirectionalLight but also from a SpotLight/PointLight

I have created a demoscene to learn the abilities of JME3. In scene I have added a DirectionalLight and a SpotLight (Torchlight in the camera view direction oriented). But I see only shadows from the DirectionalLight. See the screenshot:

I don’t see shadows from the SpotLight. The same situation I got with a PointLight.
To create the SpotLight I use the code (no shadows):

    rootNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);      
    torchLight = new SpotLight();
    rootNode.addLight(torchLight);
    torchLight.setPosition(new Vector3f(cam.getLocation().x, 5, cam.getLocation().z));
    torchLight.setDirection(cam.getDirection());
    torchLight.setColor(new ColorRGBA(1,1,0,1f));
    torchLight.setSpotInnerAngle((float) (Math.PI/12));
    torchLight.setSpotOuterAngle((float) (Math.PI/10));
    viewPort.setBackgroundColor(new ColorRGBA(0.3f, 0.3f, 0.9f, 1));
    SpotLightShadowRenderer spotLightShadowRenderer = new 
    SpotLightShadowRenderer(assetManager, 2048);
    spotLightShadowRenderer.setLight(torchLight);
    viewPort.addProcessor(spotLightShadowRenderer);

To create the DirectionalLight I use the next code (it works perfect):

    sun = new DirectionalLight();        
    sun.setDirection(new Vector3f(0.1f,-1,0).normalizeLocal()); 
    sun.setColor(new ColorRGBA(1f,0.8f,0.8f,0.6f));
    rootNode.addLight(sun);
    sun_renderer = new DirectionalLightShadowRenderer(assetManager, 2048, 1);
    sun_renderer.setLight(sun);        
    viewPort.addProcessor(sun_renderer);

Perhaps you should update the position and direction of the flashlight with the camera in a continuous update cycle, otherwise the light will remain in its initial configuration as the camera moves.

@capdevon It doesn’t affect on the shadows. It affects only on the position and direction of the spot light source. The light from the SpotLight makes only yellow circle (see screenshot) but no shadows, like from the DirectionalLight.

The shadows depend on the position, direction and the angles of the SpotLight. Your SpotLight configuration code snippet is not in a continuous synchronization update cycle with the camera, so I can assume that your character is far away from the light source and that’s why you don’t see the effects of his shadow on objects.

Edit:
Take a look at this demo to see if there are any errors in your configuration.

@capdevon well, I test it today but I already see that the function spot.setSpotRange(1000) is very important. I didn’t call it before.

2 Likes

Is this the shadow you’re looking for?

A synched spotlight/camera position does never cast a visible shadow.

play a bit with the offsets, camera up and camera left are your friends

A slight delay and some shaking might do the trick as well

People’s brain in general is not used to have a light source bound to its vision. Most people prefer holding the head lamp in the hand then mounting them.

2 Likes

Sorry, it was my mistake. I have tried to get shadows also in an another project where my spot light source was too far away from the nearest wall and the performance of the directional light was too high and the spotRange was not enough to shot to the nearest wall. As the result - I have not watched the shadows. In this project I have found the shadows. Thanks. And I have changed the spotRange in my another project and have received the shadows also there. Thank you very much!

3 Likes