diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 40985ad237f0f..a0a332bb61747 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -732,6 +732,7 @@ pub fn queue_uinodes( transparent_phase .items .reserve(extracted_uinodes.uinodes.len()); + for (entity, extracted_uinode) in extracted_uinodes.uinodes.iter() { transparent_phase.add(TransparentUi { draw_function, @@ -781,11 +782,6 @@ pub fn prepare_uinodes( }; } - #[inline] - fn is_textured(image: AssetId) -> bool { - image != AssetId::default() - } - if let Some(view_binding) = view_uniforms.uniforms.binding() { let mut batches: Vec<(Entity, UiBatch)> = Vec::with_capacity(*previous_len); @@ -798,7 +794,6 @@ pub fn prepare_uinodes( // Vertex buffer index let mut index = 0; - for mut ui_phase in &mut phases { let mut batch_item_index = 0; let mut batch_image_handle = AssetId::invalid(); @@ -806,11 +801,14 @@ pub fn prepare_uinodes( for item_index in 0..ui_phase.items.len() { let item = &mut ui_phase.items[item_index]; if let Some(extracted_uinode) = extracted_uinodes.uinodes.get(&item.entity) { - let mut existing_batch = batches - .last_mut() - .filter(|_| batch_image_handle == extracted_uinode.image); - - if existing_batch.is_none() { + let mut existing_batch = batches.last_mut(); + + if batch_image_handle == AssetId::invalid() + || existing_batch.is_none() + || (batch_image_handle != AssetId::default() + && extracted_uinode.image != AssetId::default() + && batch_image_handle != extracted_uinode.image) + { if let Some(gpu_image) = gpu_images.get(extracted_uinode.image) { batch_item_index = item_index; batch_image_handle = extracted_uinode.image; @@ -840,9 +838,32 @@ pub fn prepare_uinodes( } else { continue; } + } else if batch_image_handle == AssetId::default() + && extracted_uinode.image != AssetId::default() + { + if let Some(gpu_image) = gpu_images.get(extracted_uinode.image) { + batch_image_handle = extracted_uinode.image; + existing_batch.as_mut().unwrap().1.image = extracted_uinode.image; + + image_bind_groups + .values + .entry(batch_image_handle) + .or_insert_with(|| { + render_device.create_bind_group( + "ui_material_bind_group", + &ui_pipeline.image_layout, + &BindGroupEntries::sequential(( + &gpu_image.texture_view, + &gpu_image.sampler, + )), + ) + }); + } else { + continue; + } } - let mode = if is_textured(extracted_uinode.image) { + let mode = if extracted_uinode.image != AssetId::default() { TEXTURED_QUAD } else { UNTEXTURED_QUAD