@@ -40,19 +40,17 @@ pub struct RenderResources<'w, 's> {
4040struct BatchState {
4141 current_mesh : Option < Mesh > ,
4242 material_key : Option < MaterialKey > ,
43- active_material : Entity ,
4443 transform : Affine3A ,
4544 draw_index : u32 ,
4645 render_layers : RenderLayers ,
4746 graphics_entity : Entity ,
4847}
4948
5049impl BatchState {
51- fn new ( graphics_entity : Entity , render_layers : RenderLayers , active_material : Entity ) -> Self {
50+ fn new ( graphics_entity : Entity , render_layers : RenderLayers ) -> Self {
5251 Self {
5352 current_mesh : None ,
5453 material_key : None ,
55- active_material,
5654 transform : Affine3A :: IDENTITY ,
5755 draw_index : 0 ,
5856 render_layers,
@@ -68,11 +66,10 @@ pub struct RenderState {
6866 pub stroke_weight : f32 ,
6967 pub material_key : MaterialKey ,
7068 pub transform : TransformStack ,
71- pub active_material : Entity ,
7269}
7370
7471impl RenderState {
75- pub fn new ( default_material : Entity ) -> Self {
72+ pub fn new ( ) -> Self {
7673 Self {
7774 fill_color : Some ( Color :: WHITE ) ,
7875 stroke_color : Some ( Color :: BLACK ) ,
@@ -82,11 +79,10 @@ impl RenderState {
8279 background_image : None ,
8380 } ,
8481 transform : TransformStack :: new ( ) ,
85- active_material : default_material,
8682 }
8783 }
8884
89- pub fn reset ( & mut self , default_material : Entity ) {
85+ pub fn reset ( & mut self ) {
9086 self . fill_color = Some ( Color :: WHITE ) ;
9187 self . stroke_color = Some ( Color :: BLACK ) ;
9288 self . stroke_weight = 1.0 ;
@@ -95,7 +91,6 @@ impl RenderState {
9591 background_image : None ,
9692 } ;
9793 self . transform = TransformStack :: new ( ) ;
98- self . active_material = default_material;
9994 }
10095
10196 pub fn fill_is_transparent ( & self ) -> bool {
@@ -131,11 +126,7 @@ pub fn flush_draw_commands(
131126 let view_from_world = camera_transform. to_matrix ( ) . inverse ( ) ;
132127 let world_from_clip = ( clip_from_view * view_from_world) . inverse ( ) ;
133128 let draw_commands = std:: mem:: take ( & mut cmd_buffer. commands ) ;
134- let mut batch = BatchState :: new (
135- graphics_entity,
136- render_layers. clone ( ) ,
137- state. active_material ,
138- ) ;
129+ let mut batch = BatchState :: new ( graphics_entity, render_layers. clone ( ) ) ;
139130
140131 for cmd in draw_commands {
141132 match cmd {
@@ -305,13 +296,16 @@ pub fn flush_draw_commands(
305296 continue ;
306297 } ;
307298
308- let Some ( mat_handle) = p_material_handles. get ( batch. active_material ) . ok ( )
309- else {
310- warn ! (
311- "Could not find material for entity {:?}" ,
312- batch. active_material
313- ) ;
314- continue ;
299+ let material_key = material_key_with_fill ( & state) ;
300+ let material_handle = match & material_key {
301+ MaterialKey :: Custom ( mat_entity) => {
302+ let Some ( handle) = p_material_handles. get ( * mat_entity) . ok ( ) else {
303+ warn ! ( "Could not find material for entity {:?}" , mat_entity) ;
304+ continue ;
305+ } ;
306+ handle. 0 . clone ( )
307+ }
308+ _ => material_key. to_material ( & mut res. materials ) ,
315309 } ;
316310
317311 flush_batch ( & mut res, & mut batch) ;
@@ -322,7 +316,7 @@ pub fn flush_draw_commands(
322316
323317 res. commands . spawn ( (
324318 Mesh3d ( geometry. handle . clone ( ) ) ,
325- UntypedMaterial ( mat_handle . 0 . clone ( ) ) ,
319+ UntypedMaterial ( material_handle ) ,
326320 BelongsToGraphics ( batch. graphics_entity ) ,
327321 transform,
328322 batch. render_layers . clone ( ) ,
@@ -331,9 +325,7 @@ pub fn flush_draw_commands(
331325 batch. draw_index += 1 ;
332326 }
333327 DrawCommand :: Material ( entity) => {
334- state. active_material = entity;
335- batch. active_material = entity;
336- flush_batch ( & mut res, & mut batch) ;
328+ state. material_key = MaterialKey :: Custom ( entity) ;
337329 }
338330 DrawCommand :: Box {
339331 width,
0 commit comments