@@ -64,6 +64,10 @@ pub struct Wireframe;
64
64
/// it will still affect the color of the wireframe when [`WireframeConfig::global`] is set to true.
65
65
///
66
66
/// 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.
67
71
#[ derive( Component , Debug , Clone , Default , Reflect ) ]
68
72
#[ reflect( Component , Default ) ]
69
73
pub struct WireframeColor {
@@ -155,19 +159,12 @@ fn apply_wireframe_material(
155
159
}
156
160
}
157
161
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) ) ;
169
166
}
170
- commands. insert_or_spawn_batch ( wireframes_to_spawn ) ;
167
+ commands. insert_or_spawn_batch ( material_to_spawn ) ;
171
168
}
172
169
173
170
type WireframeFilter = ( With < Handle < Mesh > > , Without < Wireframe > , Without < NoWireframe > ) ;
@@ -176,16 +173,21 @@ type WireframeFilter = (With<Handle<Mesh>>, Without<Wireframe>, Without<NoWirefr
176
173
fn apply_global_wireframe_material (
177
174
mut commands : Commands ,
178
175
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
+ > ,
180
180
meshes_with_global_material : Query < Entity , ( WireframeFilter , With < Handle < WireframeMaterial > > ) > ,
181
181
global_material : Res < GlobalWireframeMaterial > ,
182
+ mut materials : ResMut < Assets < WireframeMaterial > > ,
182
183
) {
183
184
if config. global {
184
185
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) ;
186
188
// We only add the material handle but not the Wireframe component
187
189
// 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 ) ) ;
189
191
}
190
192
commands. insert_or_spawn_batch ( material_to_spawn) ;
191
193
} else {
@@ -195,6 +197,22 @@ fn apply_global_wireframe_material(
195
197
}
196
198
}
197
199
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
+
198
216
#[ derive( Default , AsBindGroup , TypePath , Debug , Clone , Asset ) ]
199
217
pub struct WireframeMaterial {
200
218
#[ uniform( 0 ) ]
@@ -213,7 +231,9 @@ impl Material for WireframeMaterial {
213
231
_key : MaterialPipelineKey < Self > ,
214
232
) -> Result < ( ) , SpecializedMeshPipelineError > {
215
233
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
+ }
217
237
Ok ( ( ) )
218
238
}
219
239
}
0 commit comments