@@ -12,7 +12,7 @@ use bevy::{
1212} ;
1313use command:: { CommandBuffer , DrawCommand } ;
1414use material:: MaterialKey ;
15- use primitive:: { TessellationMode , box_mesh, empty_mesh, sphere_mesh} ;
15+ use primitive:: { StrokeConfig , TessellationMode , box_mesh, empty_mesh, sphere_mesh} ;
1616use transform:: TransformStack ;
1717
1818use crate :: {
@@ -65,6 +65,7 @@ pub struct RenderState {
6565 pub fill_color : Option < Color > ,
6666 pub stroke_color : Option < Color > ,
6767 pub stroke_weight : f32 ,
68+ pub stroke_config : StrokeConfig ,
6869 pub material_key : MaterialKey ,
6970 pub transform : TransformStack ,
7071}
@@ -75,6 +76,7 @@ impl RenderState {
7576 fill_color : Some ( Color :: WHITE ) ,
7677 stroke_color : Some ( Color :: BLACK ) ,
7778 stroke_weight : 1.0 ,
79+ stroke_config : StrokeConfig :: default ( ) ,
7880 material_key : MaterialKey :: Color {
7981 transparent : false ,
8082 background_image : None ,
@@ -87,6 +89,7 @@ impl RenderState {
8789 self . fill_color = Some ( Color :: WHITE ) ;
8890 self . stroke_color = Some ( Color :: BLACK ) ;
8991 self . stroke_weight = 1.0 ;
92+ self . stroke_config = StrokeConfig :: default ( ) ;
9093 self . material_key = MaterialKey :: Color {
9194 transparent : false ,
9295 background_image : None ,
@@ -152,6 +155,12 @@ pub fn flush_draw_commands(
152155 DrawCommand :: StrokeWeight ( weight) => {
153156 state. stroke_weight = weight;
154157 }
158+ DrawCommand :: StrokeCap ( cap) => {
159+ state. stroke_config . line_cap = cap;
160+ }
161+ DrawCommand :: StrokeJoin ( join) => {
162+ state. stroke_config . line_join = join;
163+ }
155164 DrawCommand :: Roughness ( r) => {
156165 state. material_key = match state. material_key {
157166 MaterialKey :: Pbr {
@@ -223,8 +232,19 @@ pub fn flush_draw_commands(
223232 } ;
224233 }
225234 DrawCommand :: Rect { x, y, w, h, radii } => {
235+ let stroke_config = state. stroke_config ;
226236 add_fill ( & mut res, & mut batch, & state, |mesh, color| {
227- rect ( mesh, x, y, w, h, radii, color, TessellationMode :: Fill )
237+ rect (
238+ mesh,
239+ x,
240+ y,
241+ w,
242+ h,
243+ radii,
244+ color,
245+ TessellationMode :: Fill ,
246+ & stroke_config,
247+ )
228248 } ) ;
229249
230250 add_stroke ( & mut res, & mut batch, & state, |mesh, color, weight| {
@@ -237,6 +257,7 @@ pub fn flush_draw_commands(
237257 radii,
238258 color,
239259 TessellationMode :: Stroke ( weight) ,
260+ & stroke_config,
240261 )
241262 } ) ;
242263 }
@@ -511,24 +532,58 @@ fn flush_batch(res: &mut RenderResources, batch: &mut BatchState) {
511532}
512533
513534fn add_shape3d ( res : & mut RenderResources , batch : & mut BatchState , state : & RenderState , mesh : Mesh ) {
535+ use bevy:: pbr:: wireframe:: { Wireframe , WireframeColor , WireframeLineWidth , WireframeTopology } ;
536+
514537 flush_batch ( res, batch) ;
515538
516539 let mesh_handle = res. meshes . add ( mesh) ;
517- let material_key = material_key_with_fill ( state) ;
518- let material_handle = material_key. to_material ( & mut res. materials ) ;
540+ let fill_color = state. fill_color . unwrap_or ( Color :: WHITE ) ;
541+ let material_handle = match & state. material_key {
542+ MaterialKey :: Color { transparent, .. } => {
543+ let mat = StandardMaterial {
544+ base_color : fill_color,
545+ unlit : true ,
546+ cull_mode : None ,
547+ alpha_mode : if * transparent {
548+ AlphaMode :: Blend
549+ } else {
550+ AlphaMode :: Opaque
551+ } ,
552+ ..default ( )
553+ } ;
554+ res. materials . add ( mat) . untyped ( )
555+ }
556+ _ => {
557+ let key = material_key_with_fill ( state) ;
558+ key. to_material ( & mut res. materials )
559+ }
560+ } ;
519561
520562 let z_offset = -( batch. draw_index as f32 * 0.001 ) ;
521563 let mut transform = state. transform . to_bevy_transform ( ) ;
522564 transform. translation . z += z_offset;
523565
524- res. commands . spawn ( (
566+ let mut entity = res. commands . spawn ( (
525567 Mesh3d ( mesh_handle) ,
526568 UntypedMaterial ( material_handle) ,
527569 BelongsToGraphics ( batch. graphics_entity ) ,
528570 transform,
529571 batch. render_layers . clone ( ) ,
530572 ) ) ;
531573
574+ if let Some ( stroke_color) = state. stroke_color {
575+ entity. insert ( (
576+ Wireframe ,
577+ WireframeColor {
578+ color : stroke_color,
579+ } ,
580+ WireframeLineWidth {
581+ width : state. stroke_weight ,
582+ } ,
583+ WireframeTopology :: Quads ,
584+ ) ) ;
585+ }
586+
532587 batch. draw_index += 1 ;
533588}
534589
0 commit comments