emilk / egui

egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native
https://www.egui.rs/
Apache License 2.0
21.63k stars 1.56k forks source link

Is it possible to render .gif images? #1857

Closed ocean-beach closed 2 weeks ago

ocean-beach commented 2 years ago

Hi. I'm using bevy_egui https://github.com/mvlabat/bevy_egui that provides a Egui integration for the bevy game engine.

I'm trying to render simple Window with .gif animation.

fn load_assets(mut commands: Commands, assets: Res<AssetServer>) {
    commands.insert_resource(Images {
        example_img: assets.load("images/example.gif"),
        // example_img: assets.load("images/example.png") <---- png  images work as expected, but not .gif
    });
}
fn render(
    mut egui_context: ResMut<EguiContext>,
    images: Res<Images>,
) {
    let bevy_texture_id = egui_context.add_image(images.example_img.clone_weak());
    egui::Window::new("dialog")
        .resizable(false)
        .collapsible(false)
        .title_bar(false)
        .show(egui_context.ctx_mut(), |ui| {
            ui.add(egui::widgets::Image::new(bevy_texture_id, [300.0, 300.0]));
        });
}

It it possible to render .gif animation with Egui?

emilk commented 2 years ago

It is possible, but you will have to run the animation yourself.

Load all frames, assign them all their own texture, then flip between them at the right interval (remember to use Context::request_repaint)

Gui-Yom commented 2 years ago

@ocean-beach I have this working in one of my projects here : https://github.com/Gui-Yom/vibin/blob/26e1a89a193d16754a1e33bd495aa51cd9b886a1/src/main.rs

In this code, I schedule a repaint from another thread so I can get the timings right without impacting the UI itself. Though I'm not sure how you could integrate this with bevy.

ocean-beach commented 2 years ago

Thanks a lot @emilk and @Gui-Yom

zdimension commented 1 month ago

Support for animated GIFs has been added in https://github.com/emilk/egui/pull/4620.