@@ -12,7 +12,7 @@ use bevy::{
1212} ;
1313use command:: { CommandBuffer , DrawCommand } ;
1414use material:: MaterialKey ;
15- use primitive:: { TessellationMode , empty_mesh} ;
15+ use primitive:: { TessellationMode , box_mesh , empty_mesh, sphere_mesh } ;
1616use transform:: TransformStack ;
1717
1818use crate :: {
@@ -124,14 +124,8 @@ pub fn flush_draw_commands(
124124 p_geometries : Query < & Geometry > ,
125125 p_material_handles : Query < & UntypedMaterial > ,
126126) {
127- for (
128- graphics_entity,
129- mut cmd_buffer,
130- mut state,
131- render_layers,
132- projection,
133- camera_transform,
134- ) in graphics. iter_mut ( )
127+ for ( graphics_entity, mut cmd_buffer, mut state, render_layers, projection, camera_transform) in
128+ graphics. iter_mut ( )
135129 {
136130 let clip_from_view = projection. get_clip_from_view ( ) ;
137131 let view_from_world = camera_transform. to_matrix ( ) . inverse ( ) ;
@@ -162,9 +156,17 @@ pub fn flush_draw_commands(
162156 }
163157 DrawCommand :: Roughness ( r) => {
164158 state. material_key = match state. material_key {
165- MaterialKey :: Pbr { albedo, metallic, emissive, .. } => {
166- MaterialKey :: Pbr { albedo, roughness : ( r * 255.0 ) as u8 , metallic, emissive }
167- }
159+ MaterialKey :: Pbr {
160+ albedo,
161+ metallic,
162+ emissive,
163+ ..
164+ } => MaterialKey :: Pbr {
165+ albedo,
166+ roughness : ( r * 255.0 ) as u8 ,
167+ metallic,
168+ emissive,
169+ } ,
168170 _ => MaterialKey :: Pbr {
169171 albedo : [ 255 , 255 , 255 , 255 ] ,
170172 roughness : ( r * 255.0 ) as u8 ,
@@ -175,9 +177,17 @@ pub fn flush_draw_commands(
175177 }
176178 DrawCommand :: Metallic ( m) => {
177179 state. material_key = match state. material_key {
178- MaterialKey :: Pbr { albedo, roughness, emissive, .. } => {
179- MaterialKey :: Pbr { albedo, roughness, metallic : ( m * 255.0 ) as u8 , emissive }
180- }
180+ MaterialKey :: Pbr {
181+ albedo,
182+ roughness,
183+ emissive,
184+ ..
185+ } => MaterialKey :: Pbr {
186+ albedo,
187+ roughness,
188+ metallic : ( m * 255.0 ) as u8 ,
189+ emissive,
190+ } ,
181191 _ => MaterialKey :: Pbr {
182192 albedo : [ 255 , 255 , 255 , 255 ] ,
183193 roughness : 128 ,
@@ -189,9 +199,17 @@ pub fn flush_draw_commands(
189199 DrawCommand :: Emissive ( color) => {
190200 let [ r, g, b, a] = color. to_srgba ( ) . to_u8_array ( ) ;
191201 state. material_key = match state. material_key {
192- MaterialKey :: Pbr { albedo, roughness, metallic, .. } => {
193- MaterialKey :: Pbr { albedo, roughness, metallic, emissive : [ r, g, b, a] }
194- }
202+ MaterialKey :: Pbr {
203+ albedo,
204+ roughness,
205+ metallic,
206+ ..
207+ } => MaterialKey :: Pbr {
208+ albedo,
209+ roughness,
210+ metallic,
211+ emissive : [ r, g, b, a] ,
212+ } ,
195213 _ => MaterialKey :: Pbr {
196214 albedo : [ 255 , 255 , 255 , 255 ] ,
197215 roughness : 128 ,
@@ -287,8 +305,12 @@ pub fn flush_draw_commands(
287305 continue ;
288306 } ;
289307
290- let Some ( mat_handle) = p_material_handles. get ( batch. active_material ) . ok ( ) else {
291- warn ! ( "Could not find material for entity {:?}" , batch. active_material) ;
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+ ) ;
292314 continue ;
293315 } ;
294316
@@ -313,6 +335,25 @@ pub fn flush_draw_commands(
313335 batch. active_material = entity;
314336 flush_batch ( & mut res, & mut batch) ;
315337 }
338+ DrawCommand :: Box {
339+ width,
340+ height,
341+ depth,
342+ } => {
343+ add_shape3d ( & mut res, & mut batch, & state, box_mesh ( width, height, depth) ) ;
344+ }
345+ DrawCommand :: Sphere {
346+ radius,
347+ sectors,
348+ stacks,
349+ } => {
350+ add_shape3d (
351+ & mut res,
352+ & mut batch,
353+ & state,
354+ sphere_mesh ( radius, sectors, stacks) ,
355+ ) ;
356+ }
316357 }
317358 }
318359
@@ -382,11 +423,18 @@ fn start_batch(
382423
383424fn material_key_with_color ( key : & MaterialKey , color : Color ) -> MaterialKey {
384425 match key {
385- MaterialKey :: Color { background_image, .. } => MaterialKey :: Color {
426+ MaterialKey :: Color {
427+ background_image, ..
428+ } => MaterialKey :: Color {
386429 transparent : color. alpha ( ) < 1.0 ,
387430 background_image : background_image. clone ( ) ,
388431 } ,
389- MaterialKey :: Pbr { roughness, metallic, emissive, .. } => {
432+ MaterialKey :: Pbr {
433+ roughness,
434+ metallic,
435+ emissive,
436+ ..
437+ } => {
390438 let [ r, g, b, a] = color. to_srgba ( ) . to_u8_array ( ) ;
391439 MaterialKey :: Pbr {
392440 albedo : [ r, g, b, a] ,
@@ -399,6 +447,11 @@ fn material_key_with_color(key: &MaterialKey, color: Color) -> MaterialKey {
399447 }
400448}
401449
450+ fn material_key_with_fill ( state : & RenderState ) -> MaterialKey {
451+ let color = state. fill_color . unwrap_or ( Color :: WHITE ) ;
452+ material_key_with_color ( & state. material_key , color)
453+ }
454+
402455fn add_fill (
403456 res : & mut RenderResources ,
404457 batch : & mut BatchState ,
@@ -449,6 +502,28 @@ fn flush_batch(res: &mut RenderResources, batch: &mut BatchState) {
449502 batch. material_key = None ;
450503}
451504
505+ fn add_shape3d ( res : & mut RenderResources , batch : & mut BatchState , state : & RenderState , mesh : Mesh ) {
506+ flush_batch ( res, batch) ;
507+
508+ let mesh_handle = res. meshes . add ( mesh) ;
509+ let material_key = material_key_with_fill ( state) ;
510+ let material_handle = material_key. to_material ( & mut res. materials ) ;
511+
512+ let z_offset = -( batch. draw_index as f32 * 0.001 ) ;
513+ let mut transform = state. transform . to_bevy_transform ( ) ;
514+ transform. translation . z += z_offset;
515+
516+ res. commands . spawn ( (
517+ Mesh3d ( mesh_handle) ,
518+ UntypedMaterial ( material_handle) ,
519+ BelongsToGraphics ( batch. graphics_entity ) ,
520+ transform,
521+ batch. render_layers . clone ( ) ,
522+ ) ) ;
523+
524+ batch. draw_index += 1 ;
525+ }
526+
452527/// Creates a fullscreen quad by transforming NDC fullscreen by inverse of the clip-from-world matrix
453528/// so that when the vertex shader applies clip_from_world, the vertices end up correctly back in
454529/// NDC space.
0 commit comments