Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving Spotlight with shadows_enabled not shining #7152

Closed
Rust-Ninja-Sabi opened this issue Jan 10, 2023 · 1 comment
Closed

Moving Spotlight with shadows_enabled not shining #7152

Rust-Ninja-Sabi opened this issue Jan 10, 2023 · 1 comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior

Comments

@Rust-Ninja-Sabi
Copy link

Bevy version

0.9.1

`AdapterInfo { name: "Apple M1", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }`

What you did

I need a moving Spotlight.

What went wrong

If shadows_enabled == false, it works. I see the light moving.
else, no light

Example:


use bevy::prelude::*;

const GUARD_SPEED:f32 = 1.0;

#[derive(Component)]
struct PathFollower{
    path : Vec<Vec3>,
    next_pathpoint: usize,
    last_pathpoint: usize
}

fn main() {
    App::new()
        //add config resources
        .insert_resource(Msaa {samples: 4})
        .insert_resource(ClearColor(Color::BLACK))
        //bevy itself
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            window: WindowDescriptor {
                title: "bevy spotlight".to_string(),
                width: 920.0,
                height: 640.0,
                ..default()
            },
            ..default()
        }))
        .add_startup_system(setup_camera)
        .add_startup_system(setup)
        .add_system(move_path_follower)
        .run();
}


fn setup_camera(
    mut commands: Commands
) {
    commands.
        spawn(Camera3dBundle {
            transform: Transform {
                translation: Vec3::new(-0.05, 23.68, 14.94),
                rotation: Quat:: from_euler(EulerRot::XYZ, -1.0,0.0,0.0),
                ..default()
            },
            ..Default::default()
        })
        .insert(Name::new("MainCamera"));
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    //light
    commands.spawn(DirectionalLightBundle {
        directional_light: DirectionalLight {
            illuminance: 10000.0,
            shadows_enabled: true,
            ..default()
        },
        transform: Transform {
            translation: Vec3::new(0.0, 11.6, -15.1),
            rotation: Quat::from_rotation_x(-std::f32::consts::FRAC_PI_4),
            ..default()
        },
        ..default()
    });

    // ambient light
    commands.insert_resource(AmbientLight {
        color: Color::WHITE,
        brightness: 0.1,
    });

    //platform
    commands
        .spawn(PbrBundle {
            mesh: meshes.add(Mesh::from(shape::Plane { size: 12.0 })),
            material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
            ..default()
        })
        .insert(Name::new("Ground"));

    //splotlight
    commands.spawn(SpotLightBundle {
        spot_light: SpotLight {
            color: Color::WHITE,
            intensity: 10000.0,
            inner_angle: 0.7,
            shadows_enabled: true,   //<--
            ..default()
        },
        transform: Transform::from_xyz(-5.0,0.5,0.0),
        ..default()
    })
        .insert(PathFollower{
            path:vec!(Vec3::new(-5.0,0.5,0.0),
                      Vec3::new(5.0,0.5,0.0)),
            next_pathpoint:1,
            last_pathpoint:0
        })
        .insert(Name::new("Spotlight"));
}

const MIN_DISTANCE:f32 = 0.1;
fn move_path_follower(
    time: Res<Time>,
    mut query: Query<(&mut Transform, &mut PathFollower)>
)
{
    for (mut transform, mut path_follower) in query.iter_mut(){
        if transform.translation.distance(path_follower.path[path_follower.next_pathpoint]).abs() <= MIN_DISTANCE {
            path_follower.last_pathpoint = path_follower.next_pathpoint;
            if path_follower.next_pathpoint < path_follower.path.len() - 1 {
                path_follower.next_pathpoint += 1;
            } else {
                path_follower.next_pathpoint = 0;
            }
        }
        let velocity =  (path_follower.path[path_follower.next_pathpoint] -
            path_follower.path[path_follower.last_pathpoint]).normalize()*GUARD_SPEED;
        transform.translation = transform.translation + velocity * time.delta_seconds();
        let t = transform.clone();
        *transform = t.looking_at(path_follower.path[path_follower.next_pathpoint], Vec3::Y);

    }
}

@Rust-Ninja-Sabi Rust-Ninja-Sabi added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jan 10, 2023
@rparrett rparrett added A-Rendering Drawing game state to the screen and removed S-Needs-Triage This issue needs to be labelled labels Jan 10, 2023
@rparrett
Copy link
Contributor

rparrett commented Jan 10, 2023

I can reproduce this on bevy main. This seems to be related to rotation rather than any movement. If you modify the code above to remove the looking_at line, it seems to work.

PointLight is not affected.

Here's a more minimal reproduction:

use bevy::prelude::*;

// NOT BROKEN
//const ROTATION_Y: f32 = 0.0001;

// BROKEN
const ROTATION_Y: f32 = 0.001;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup_camera)
        .add_startup_system(setup)
        .run();
}

fn setup_camera(mut commands: Commands) {
    commands.spawn(Camera3dBundle {
        transform: Transform {
            translation: Vec3::new(-0.05, 23.68, 14.94),
            rotation: Quat::from_euler(EulerRot::XYZ, -1.0, 0.0, 0.0),
            ..default()
        },
        ..Default::default()
    });
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    commands.spawn(PbrBundle {
        mesh: meshes.add(Mesh::from(shape::Plane { size: 12.0 })),
        material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
        ..default()
    });

    commands.spawn(SpotLightBundle {
        spot_light: SpotLight {
            shadows_enabled: true,
            ..default()
        },
        transform: Transform::from_xyz(0.0, 0.5, 0.0)
            .with_rotation(Quat::from_rotation_y(ROTATION_Y)),
        ..default()
    });
}

bors bot pushed a commit that referenced this issue Jan 13, 2023
# Objective

fix error with shadow shader's spotlight direction calculation when direction.y ~= 0
fixes #7152

## Solution

same as #6167: in shadows.wgsl, clamp 1-x^2-z^2 to >= 0 so that we can safely sqrt it
bors bot pushed a commit that referenced this issue Jan 13, 2023
# Objective

fix error with shadow shader's spotlight direction calculation when direction.y ~= 0
fixes #7152

## Solution

same as #6167: in shadows.wgsl, clamp 1-x^2-z^2 to >= 0 so that we can safely sqrt it
bors bot pushed a commit that referenced this issue Jan 13, 2023
# Objective

fix error with shadow shader's spotlight direction calculation when direction.y ~= 0
fixes #7152

## Solution

same as #6167: in shadows.wgsl, clamp 1-x^2-z^2 to >= 0 so that we can safely sqrt it
bors bot pushed a commit that referenced this issue Jan 13, 2023
# Objective

fix error with shadow shader's spotlight direction calculation when direction.y ~= 0
fixes #7152

## Solution

same as #6167: in shadows.wgsl, clamp 1-x^2-z^2 to >= 0 so that we can safely sqrt it
@bors bors bot closed this as completed in 0af8e1c Jan 13, 2023
alradish pushed a commit to alradish/bevy that referenced this issue Jan 22, 2023
# Objective

fix error with shadow shader's spotlight direction calculation when direction.y ~= 0
fixes bevyengine#7152

## Solution

same as bevyengine#6167: in shadows.wgsl, clamp 1-x^2-z^2 to >= 0 so that we can safely sqrt it
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
# Objective

fix error with shadow shader's spotlight direction calculation when direction.y ~= 0
fixes bevyengine#7152

## Solution

same as bevyengine#6167: in shadows.wgsl, clamp 1-x^2-z^2 to >= 0 so that we can safely sqrt it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants