Skip to content

Correct way to load new, dynamically-changing images every frame #5866

Answered by wareya
wareya asked this question in Q&A
Discussion options

You must be logged in to vote

Here's my solution: have a TextureHandle that's created dynamically but if it already exists overwritten with .set(). The texture name is just a big random number converted to string.

    if let Some(ref mut th) = layer.thumbnail
    {
        let mut th = th.to_mut::<egui::TextureHandle>().unwrap();
        th.set(thumb_img.to_egui(), egui::TextureOptions::NEAREST);
    }
    else
    {
        let ctx = ui.ctx();
        let thumb = thumb_img.to_egui();
        let tex = ctx.load_texture(format!("{}", Uuid::new_v4().as_u128()), thumb, egui::TextureOptions::NEAREST);
        layer.thumbnail = Some(Box::new(tex));
    }
    painter.image(tx.id(), rect, [(0.0, 0.0).into(), (1.0, 1.0).into()].

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@wareya
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by wareya
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants