@@ -358,12 +358,16 @@ pub fn flush_draw_commands(
358358
359359 let material_key = material_key_with_fill ( & state) ;
360360 let material_handle = match & material_key {
361- MaterialKey :: Custom { entity : mat_entity, .. } => {
362- let Some ( handle ) = p_material_handles. get ( * mat_entity) . ok ( ) else {
361+ MaterialKey :: Custom { entity : mat_entity, blend_state } => {
362+ let Some ( untyped ) = p_material_handles. get ( * mat_entity) . ok ( ) else {
363363 warn ! ( "Could not find material for entity {:?}" , mat_entity) ;
364364 continue ;
365365 } ;
366- handle. 0 . clone ( )
366+ clone_custom_material_with_blend (
367+ & mut res. custom_materials ,
368+ & untyped. 0 ,
369+ * blend_state,
370+ )
367371 }
368372 _ => material_key. to_material ( & mut res. materials ) ,
369373 } ;
@@ -478,24 +482,11 @@ fn spawn_mesh(
478482 warn ! ( "Custom material entity {:?} not found" , entity) ;
479483 return ;
480484 } ;
481- match * blend_state {
482- // No blend override — use the original handle
483- None => untyped. 0 . clone ( ) ,
484- // Blend override — clone the custom material with the blend state
485- Some ( bs) => {
486- if let Ok ( handle) = untyped. 0 . clone ( ) . try_typed :: < CustomMaterial > ( ) {
487- if let Some ( original) = res. custom_materials . get ( & handle) {
488- let mut variant = original. clone ( ) ;
489- variant. blend_state = Some ( bs) ;
490- res. custom_materials . add ( variant) . untyped ( )
491- } else {
492- untyped. 0 . clone ( )
493- }
494- } else {
495- untyped. 0 . clone ( )
496- }
497- }
498- }
485+ clone_custom_material_with_blend (
486+ & mut res. custom_materials ,
487+ & untyped. 0 ,
488+ * blend_state,
489+ )
499490 }
500491 _ => key. to_material ( & mut res. materials ) ,
501492 } ;
@@ -512,8 +503,6 @@ fn spawn_mesh(
512503fn needs_batch ( batch : & BatchState , state : & RenderState , material_key : & MaterialKey ) -> bool {
513504 let material_changed = batch. material_key . as_ref ( ) != Some ( material_key) ;
514505 let transform_changed = batch. transform != state. transform . current ( ) ;
515- // When a custom blend mode is active, each shape needs its own draw call
516- // so that shapes composite against each other in draw order.
517506 let requires_separate_draws = state. blend_state . is_some ( ) ;
518507 material_changed || transform_changed || requires_separate_draws
519508}
@@ -531,6 +520,27 @@ fn start_batch(
531520 batch. current_mesh = Some ( empty_mesh ( ) ) ;
532521}
533522
523+ fn clone_custom_material_with_blend (
524+ custom_materials : & mut Assets < CustomMaterial > ,
525+ original : & UntypedHandle ,
526+ blend_state : Option < BlendState > ,
527+ ) -> UntypedHandle {
528+ match blend_state {
529+ None => original. clone ( ) ,
530+ Some ( bs) => {
531+ let Ok ( handle) = original. clone ( ) . try_typed :: < CustomMaterial > ( ) else {
532+ return original. clone ( ) ;
533+ } ;
534+ let Some ( original_mat) = custom_materials. get ( & handle) else {
535+ return original. clone ( ) ;
536+ } ;
537+ let mut variant = original_mat. clone ( ) ;
538+ variant. blend_state = Some ( bs) ;
539+ custom_materials. add ( variant) . untyped ( )
540+ }
541+ }
542+ }
543+
534544fn material_key_with_color (
535545 key : & MaterialKey ,
536546 color : Color ,
@@ -641,13 +651,17 @@ fn add_shape3d(
641651 let mesh_handle = res. meshes . add ( mesh) ;
642652 let fill_color = state. fill_color . unwrap_or ( Color :: WHITE ) ;
643653 let material_handle = match & state. material_key {
644- MaterialKey :: Custom { entity, .. } => match material_handles. get ( * entity) {
645- Ok ( handle) => handle. 0 . clone ( ) ,
646- Err ( _) => {
654+ MaterialKey :: Custom { entity, .. } => {
655+ let Some ( untyped) = material_handles. get ( * entity) . ok ( ) else {
647656 warn ! ( "Custom material entity {:?} not found" , entity) ;
648657 return ;
649- }
650- } ,
658+ } ;
659+ clone_custom_material_with_blend (
660+ & mut res. custom_materials ,
661+ & untyped. 0 ,
662+ state. blend_state ,
663+ )
664+ }
651665 // TODO: in 2d, we use vertex colors. `to_material` becomes complicated if we also encode
652666 // a base color in the material, so for simplicity we just create a new material here
653667 // that is unlit and uses the fill color as the base color
0 commit comments