#Directional Light - shadow cut-off

3 messages · Page 1 of 1 (latest)

tardy chasm
#

I have a very basic scene containing a plane, two cubes, a directional light and a camera.

The problem is that shadow projected onto the plane is cut-off at estimately z < 0.0.

Bevy version: 0.16.1

I tried a bunch of things:

  • changing light angles
  • splitting the plane into multiple smaller planes
  • setting CascadeShadowConfig

But i couldn't resolve the problem with the cast shadow.

Here is the code for the scene setup:

fn test_scene(
  mut commands: Commands,
  mut meshes: ResMut<Assets<Mesh>>,
  mut materials: ResMut<Assets<StandardMaterial>>,
) {
    commands.spawn((
        Mesh3d(meshes.add(Rectangle::new(100.0, 100.0))),
        MeshMaterial3d(materials.add(StandardMaterial {
            base_color: Color::srgb(0.2, 0.7, 0.3),
            ..default()
        })),
        Transform::from_xyz(0.0, -8.0, 0.0).with_rotation(Quat::from_rotation_x(-FRAC_PI_2)),
    ));

    let cube = meshes.add(Cuboid::from_size(Vec3::splat(16.0)));
    let quat = Quat::from_rotation_y(FRAC_PI_4);
    commands.spawn((
        Mesh3d(cube.clone()),
        MeshMaterial3d(materials.add(StandardMaterial {
            base_color: Color::srgb(0.2, 0.2, 0.8),
            ..default()
        })),
        Transform::from_xyz(-20.0, 0.0, 20.0).with_rotation(quat),
    ));
    commands.spawn((
        Mesh3d(cube),
        MeshMaterial3d(materials.add(StandardMaterial {
            base_color: Color::srgb(0.8, 0.2, 0.2),
            ..default()
        })),
        Transform::from_xyz(15.0, 0.0, -6.0).with_rotation(quat),
    ));

    commands.spawn((
        DirectionalLight {
            illuminance: 1_000.0,
            shadows_enabled: true,
            ..default()
        },
        Transform::from_rotation(Quat::from_rotation_arc(
            Vec3::NEG_Z,
            Vec3::new(1.0, -0.9, -0.1).normalize(),
        )),
    ));

    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(0.0, 100.0, 100.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));
}
arctic spear
tardy chasm
#

Thanks for the response. Sadly this only helps with resolutions of the resulting shadow maps.

But i just found the problem. The thing is that the camera is at location (0.0, 100.0, 100.0) so it looks at the plane from a distance of ~150.0 units.
Unfortunately that's the default maximum_range for CascadeShadowConfigBuilder. When i tried different configurations i was pretty planless and unsystematic. After a some deeper research into cascaded shadow maps i understood it better. A simplistic solution:

commands.spawn((
        DirectionalLight {
            illuminance: 1_000.0,
            shadows_enabled: true,
            ..default()
        },
        Transform::from_rotation(Quat::from_rotation_arc(
            Vec3::NEG_Z,
            Vec3::new(1.0, -0.9, -0.1).normalize(),
        )).with_translation(Vec3 { x: 0.0, y: 20.0, z: 0.0 }),
        CascadeShadowConfigBuilder {
            maximum_distance: 200.0,  // <-- increasing the maximum distance
            ..default()
        }.build(),
    ));

Remark: Directional light sources have more to them than i initially thought. For everyone having issues with those, this source helped me on my specific problem: https://developer.download.nvidia.com/SDK/10.5/opengl/src/cascaded_shadow_maps/doc/cascaded_shadow_maps.pdf