bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.58k stars 3.52k forks source link

2D Motion Blur #14781

Open victorb opened 1 month ago

victorb commented 1 month ago

What problem does this solve or what need does it fill?

Motion blur has been implemented for 3D, would be great to have it for 2D as well :)

What solution would you like?

Like the 3D example but for 2D things.

Additional context

Ideally, we should be able to do something like this and see motion blur:

use bevy::{
    core_pipeline::motion_blur::{MotionBlur, MotionBlurBundle},
    prelude::*,
};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(FixedUpdate, move_sprite)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn((
        Camera2dBundle::default(),
        MotionBlurBundle {
            motion_blur: MotionBlur {
                shutter_angle: 1.0,
                samples: 4,
            },
            ..default()
        },
    ));
    commands.spawn(SpriteBundle {
        texture: asset_server.load("branding/bevy_bird_dark.png"),
        ..default()
    });
}

fn move_sprite(
    time: Res<Time>,
    mut query: Query<&mut Transform, With<Sprite>>,
    mut move_left: Local<bool>,
) {
    let mut transform = query.get_single_mut().unwrap();
    if *move_left {
        transform.translation.x -= 512.0 * time.delta_seconds();
        if transform.translation.x < -100.0 {
            *move_left = false;
        }
    } else {
        transform.translation.x += 512.0 * time.delta_seconds();
        if transform.translation.x > 100.0 {
            *move_left = true;
        }
    }
}

Previous motion blur for 3D was submitted in this PR: https://github.com/bevyengine/bevy/pull/9924

aevyrie commented 2 weeks ago

All motion blur requires is writing to depth and motion IIRC. My guess is that all that is needed is 2d rendering needs updates to write motion vectors. This would also enable the use of TAA and other temporal effects.