Mutate multiple assets in parallel #8338
-
I am trying to make a custom material with custom shader. The shader gets as input multiple textures (stored as Image assets) and displays them in a certain way. The asset system lets me mutably borrow one of the images at a time but not more which makes this awkward to use: I cant really write a wrapper structure that neatly hides away the implementation detail of using 2 or more textures under the hood and given a user easy access to modify the necessary data. Is there some nice way to (temprarily, within the scope of a system) get mutable references to multiple |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You have two options
trait ModifyImage {
fn modify_1(&mut self, image: &mut Image);
fn modify_2(&mut self, image: &mut Image);
}
fn modificator(
assets: &mut Assets<Image>,
handle1: &Handle<Image>,
handle2: &Handle<Image>,
modificator: &mut impl ModifyImage,
) {
let Some(image1) = assets.get_mut(handle1) else { return; };
modificator.modify_1(image1);
let Some(image2) = assets.get_mut(handle2) else { return; };
modificator.modify_2(image2);
}
fn with_remove(
assets: &mut Assets<Image>,
handle1: &Handle<Image>,
handle2: &Handle<Image>,
) {
let Some(mut image1) = assets.remove(handle1) else { return; };
let Some(mut image2) = assets.remove(handle2) else {
assets.set(handle1, image1);
return;
};
some_function_to_modify_two_images(&mut image1, &mut image2);
assets.set(handle1, image1);
assets.set(handle2, image2);
} |
Beta Was this translation helpful? Give feedback.
You have two options
Assets::remove
to get ownership of the image asset temporarily, then useAssets::set
to reinsert it back.