@@ -7,7 +7,7 @@ pub mod transform;
77use bevy:: {
88 camera:: visibility:: RenderLayers ,
99 ecs:: system:: SystemParam ,
10- math:: { Affine3A , Mat4 , Vec4 } ,
10+ math:: { Affine2 , Affine3A , Mat4 , Vec4 } ,
1111 prelude:: * ,
1212 render:: render_resource:: BlendState ,
1313} ;
@@ -80,6 +80,8 @@ pub struct RenderState {
8080 pub material_key : MaterialKey ,
8181 pub blend_state : Option < BlendState > ,
8282 pub transform : TransformStack ,
83+ pub tint_color : Option < Color > ,
84+ pub image_mode : ShapeMode ,
8385 pub rect_mode : ShapeMode ,
8486 pub ellipse_mode : ShapeMode ,
8587 pub shape_builder : Option < ShapeBuilder > ,
@@ -95,9 +97,12 @@ impl RenderState {
9597 material_key : MaterialKey :: Color {
9698 transparent : false ,
9799 background_image : None ,
100+ uv_transform : Affine2 :: IDENTITY ,
98101 blend_state : None ,
99102 } ,
100103 blend_state : None ,
104+ tint_color : None ,
105+ image_mode : ShapeMode :: Corner ,
101106 transform : TransformStack :: new ( ) ,
102107 rect_mode : ShapeMode :: Corner ,
103108 ellipse_mode : ShapeMode :: Center ,
@@ -113,9 +118,12 @@ impl RenderState {
113118 self . material_key = MaterialKey :: Color {
114119 transparent : false ,
115120 background_image : None ,
121+ uv_transform : Affine2 :: IDENTITY ,
116122 blend_state : None ,
117123 } ;
118124 self . blend_state = None ;
125+ self . tint_color = None ;
126+ self . image_mode = ShapeMode :: Corner ;
119127 self . transform = TransformStack :: new ( ) ;
120128 self . rect_mode = ShapeMode :: Corner ;
121129 self . ellipse_mode = ShapeMode :: Center ;
@@ -232,6 +240,7 @@ pub fn flush_draw_commands(
232240 state. material_key = MaterialKey :: Color {
233241 transparent : state. fill_is_transparent ( ) ,
234242 background_image : None ,
243+ uv_transform : Affine2 :: IDENTITY ,
235244 blend_state : None ,
236245 } ;
237246 }
@@ -755,6 +764,82 @@ pub fn flush_draw_commands(
755764 }
756765 }
757766 }
767+ DrawCommand :: Tint ( color) => {
768+ state. tint_color = Some ( color) ;
769+ }
770+ DrawCommand :: NoTint => {
771+ state. tint_color = None ;
772+ }
773+ DrawCommand :: ImageMode ( mode) => {
774+ state. image_mode = mode;
775+ }
776+ DrawCommand :: Image {
777+ entity,
778+ dx,
779+ dy,
780+ d_width,
781+ d_height,
782+ sx,
783+ sy,
784+ s_width,
785+ s_height,
786+ } => {
787+ let Some ( p_image) = p_images. get ( entity) . ok ( ) else {
788+ warn ! ( "Could not find PImage for entity {:?}" , entity) ;
789+ continue ;
790+ } ;
791+
792+ let img_w = p_image. size . width as f32 ;
793+ let img_h = p_image. size . height as f32 ;
794+ let dw = d_width. unwrap_or ( img_w) ;
795+ let dh = d_height. unwrap_or ( img_h) ;
796+ let ( x, y, w, h) = apply_shape_mode ( state. image_mode , dx, dy, dw, dh) ;
797+
798+ let uv_xform = match ( sx, sy, s_width, s_height) {
799+ ( Some ( sx) , Some ( sy) , Some ( sw) , Some ( sh) ) => {
800+ Affine2 :: from_scale_angle_translation (
801+ Vec2 :: new ( sw / img_w, sh / img_h) ,
802+ 0.0 ,
803+ Vec2 :: new ( sx / img_w, sy / img_h) ,
804+ )
805+ }
806+ _ => Affine2 :: IDENTITY ,
807+ } ;
808+
809+ let tint = state. tint_color . unwrap_or ( Color :: WHITE ) ;
810+ let material_key = MaterialKey :: Color {
811+ transparent : tint. alpha ( ) < 1.0 ,
812+ background_image : Some ( p_image. handle . clone ( ) ) ,
813+ uv_transform : uv_xform,
814+ blend_state : state. blend_state ,
815+ } ;
816+ let stroke_config = state. stroke_config ;
817+
818+ flush_batch ( & mut res, & mut batch, & p_material_handles) ;
819+ start_batch (
820+ & mut res,
821+ & mut batch,
822+ & state,
823+ material_key,
824+ & p_material_handles,
825+ ) ;
826+
827+ if let Some ( ref mut mesh) = batch. current_mesh {
828+ rect (
829+ mesh,
830+ x,
831+ y,
832+ w,
833+ h,
834+ [ 0.0 ; 4 ] ,
835+ tint,
836+ TessellationMode :: Fill ,
837+ & stroke_config,
838+ ) ;
839+ }
840+
841+ flush_batch ( & mut res, & mut batch, & p_material_handles) ;
842+ }
758843 DrawCommand :: BackgroundColor ( color) => {
759844 flush_batch ( & mut res, & mut batch, & p_material_handles) ;
760845
@@ -764,6 +849,7 @@ pub fn flush_draw_commands(
764849 let material_key = MaterialKey :: Color {
765850 transparent : color. alpha ( ) < 1.0 ,
766851 background_image : None ,
852+ uv_transform : Affine2 :: IDENTITY ,
767853 blend_state : Some ( BlendState :: REPLACE ) ,
768854 } ;
769855 let material_handle = material_key. to_material ( & mut res. materials ) ;
@@ -792,6 +878,7 @@ pub fn flush_draw_commands(
792878 let material_key = MaterialKey :: Color {
793879 transparent : false ,
794880 background_image : Some ( p_image. handle . clone ( ) ) ,
881+ uv_transform : Affine2 :: IDENTITY ,
795882 blend_state : Some ( BlendState :: REPLACE ) ,
796883 } ;
797884 let material_handle = material_key. to_material ( & mut res. materials ) ;
@@ -1111,10 +1198,13 @@ fn material_key_with_color(
11111198) -> MaterialKey {
11121199 match key {
11131200 MaterialKey :: Color {
1114- background_image, ..
1201+ background_image,
1202+ uv_transform,
1203+ ..
11151204 } => MaterialKey :: Color {
11161205 transparent : color. alpha ( ) < 1.0 ,
11171206 background_image : background_image. clone ( ) ,
1207+ uv_transform : * uv_transform,
11181208 blend_state,
11191209 } ,
11201210 MaterialKey :: Pbr { .. } => {
0 commit comments