@@ -64,6 +64,10 @@ pub struct Wireframe;
6464/// it will still affect the color of the wireframe when [`WireframeConfig::global`] is set to true.
6565///
6666/// This overrides the [`WireframeConfig::default_color`].
67+ //
68+ // TODO: consider caching materials based on this color.
69+ // This could blow up in size if people use random colored wireframes for each mesh.
70+ // It will also be important to remove unused materials from the cache.
6771#[ derive( Component , Debug , Clone , Default , Reflect ) ]
6872#[ reflect( Component , Default ) ]
6973pub struct WireframeColor {
@@ -155,19 +159,12 @@ fn apply_wireframe_material(
155159 }
156160 }
157161
158- let mut wireframes_to_spawn = vec ! [ ] ;
159- for ( e, wireframe_color) in & wireframes {
160- let material = if let Some ( wireframe_color) = wireframe_color {
161- materials. add ( WireframeMaterial {
162- color : wireframe_color. color . into ( ) ,
163- } )
164- } else {
165- // If there's no color specified we can use the global material since it's already set to use the default_color
166- global_material. handle . clone ( )
167- } ;
168- wireframes_to_spawn. push ( ( e, material) ) ;
162+ let mut material_to_spawn = vec ! [ ] ;
163+ for ( e, maybe_color) in & wireframes {
164+ let material = get_wireframe_material ( maybe_color, & mut materials, & global_material) ;
165+ material_to_spawn. push ( ( e, material) ) ;
169166 }
170- commands. insert_or_spawn_batch ( wireframes_to_spawn ) ;
167+ commands. insert_or_spawn_batch ( material_to_spawn ) ;
171168}
172169
173170type WireframeFilter = ( With < Handle < Mesh > > , Without < Wireframe > , Without < NoWireframe > ) ;
@@ -176,16 +173,21 @@ type WireframeFilter = (With<Handle<Mesh>>, Without<Wireframe>, Without<NoWirefr
176173fn apply_global_wireframe_material (
177174 mut commands : Commands ,
178175 config : Res < WireframeConfig > ,
179- meshes_without_material : Query < Entity , ( WireframeFilter , Without < Handle < WireframeMaterial > > ) > ,
176+ meshes_without_material : Query <
177+ ( Entity , Option < & WireframeColor > ) ,
178+ ( WireframeFilter , Without < Handle < WireframeMaterial > > ) ,
179+ > ,
180180 meshes_with_global_material : Query < Entity , ( WireframeFilter , With < Handle < WireframeMaterial > > ) > ,
181181 global_material : Res < GlobalWireframeMaterial > ,
182+ mut materials : ResMut < Assets < WireframeMaterial > > ,
182183) {
183184 if config. global {
184185 let mut material_to_spawn = vec ! [ ] ;
185- for e in & meshes_without_material {
186+ for ( e, maybe_color) in & meshes_without_material {
187+ let material = get_wireframe_material ( maybe_color, & mut materials, & global_material) ;
186188 // We only add the material handle but not the Wireframe component
187189 // This makes it easy to detect which mesh is using the global material and which ones are user specified
188- material_to_spawn. push ( ( e, global_material . handle . clone ( ) ) ) ;
190+ material_to_spawn. push ( ( e, material ) ) ;
189191 }
190192 commands. insert_or_spawn_batch ( material_to_spawn) ;
191193 } else {
@@ -195,6 +197,22 @@ fn apply_global_wireframe_material(
195197 }
196198}
197199
200+ /// Gets an handle to a wireframe material with a fallback on the default material
201+ fn get_wireframe_material (
202+ maybe_color : Option < & WireframeColor > ,
203+ wireframe_materials : & mut Assets < WireframeMaterial > ,
204+ global_material : & GlobalWireframeMaterial ,
205+ ) -> Handle < WireframeMaterial > {
206+ if let Some ( wireframe_color) = maybe_color {
207+ wireframe_materials. add ( WireframeMaterial {
208+ color : wireframe_color. color . into ( ) ,
209+ } )
210+ } else {
211+ // If there's no color specified we can use the global material since it's already set to use the default_color
212+ global_material. handle . clone ( )
213+ }
214+ }
215+
198216#[ derive( Default , AsBindGroup , TypePath , Debug , Clone , Asset ) ]
199217pub struct WireframeMaterial {
200218 #[ uniform( 0 ) ]
@@ -213,7 +231,9 @@ impl Material for WireframeMaterial {
213231 _key : MaterialPipelineKey < Self > ,
214232 ) -> Result < ( ) , SpecializedMeshPipelineError > {
215233 descriptor. primitive . polygon_mode = PolygonMode :: Line ;
216- descriptor. depth_stencil . as_mut ( ) . unwrap ( ) . bias . slope_scale = 1.0 ;
234+ if let Some ( depth_stencil) = descriptor. depth_stencil . as_mut ( ) {
235+ depth_stencil. bias . slope_scale = 1.0 ;
236+ }
217237 Ok ( ( ) )
218238 }
219239}
0 commit comments