@@ -370,7 +370,7 @@ pub fn destroy(
370370}
371371
372372pub fn begin (
373- In ( graphics_entity ) : In < Entity > ,
373+ In ( entity ) : In < Entity > ,
374374 mut commands : Commands ,
375375 mut state_query : Query < & mut RenderState > ,
376376 mut meshes : ResMut < Assets < Mesh > > ,
@@ -392,24 +392,90 @@ pub fn begin(
392392 let handle = meshes. add ( mesh) ;
393393
394394 let mut state = state_query
395- . get_mut ( graphics_entity )
395+ . get_mut ( entity )
396396 . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
397397
398398 state. running_geometry = Some ( Geometry :: new ( handle, layout_entity) ) ;
399399 Ok ( ( ) )
400400}
401401
402402pub fn end (
403- In ( graphics_entity ) : In < Entity > ,
403+ In ( entity ) : In < Entity > ,
404404 mut commands : Commands ,
405405 mut state_query : Query < & mut RenderState > ,
406406) -> Result < Entity > {
407407 let geometry = state_query
408- . get_mut ( graphics_entity )
408+ . get_mut ( entity )
409409 . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?
410410 . running_geometry
411411 . take ( )
412412 . ok_or ( ProcessingError :: GeometryNotFound ) ?;
413413
414414 Ok ( commands. spawn ( geometry) . id ( ) )
415415}
416+
417+ pub fn sphere (
418+ In ( ( entity, radius) ) : In < ( Entity , f32 ) > ,
419+ state_query : Query < & mut RenderState > ,
420+ mut meshes : ResMut < Assets < Mesh > > ,
421+ ) -> Result < ( ) > {
422+ let geometry = state_query
423+ . get ( entity)
424+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?
425+ . running_geometry
426+ . as_ref ( )
427+ . ok_or ( ProcessingError :: GeometryNotFound ) ?;
428+
429+ let mesh = meshes
430+ . get_mut ( & geometry. handle )
431+ . ok_or ( ProcessingError :: GeometryNotFound ) ?;
432+
433+ let base_index = mesh. count_vertices ( ) as u32 ;
434+ let sphere_mesh = Sphere :: new ( radius) . mesh ( ) . build ( ) ;
435+
436+ // Append positions
437+ if let Some ( VertexAttributeValues :: Float32x3 ( new_pos) ) =
438+ sphere_mesh. attribute ( Mesh :: ATTRIBUTE_POSITION )
439+ && let Some ( VertexAttributeValues :: Float32x3 ( positions) ) =
440+ mesh. attribute_mut ( Mesh :: ATTRIBUTE_POSITION )
441+ {
442+ positions. extend_from_slice ( new_pos) ;
443+ }
444+
445+ // Append normals
446+ if let Some ( VertexAttributeValues :: Float32x3 ( new_normals) ) =
447+ sphere_mesh. attribute ( Mesh :: ATTRIBUTE_NORMAL )
448+ && let Some ( VertexAttributeValues :: Float32x3 ( normals) ) =
449+ mesh. attribute_mut ( Mesh :: ATTRIBUTE_NORMAL )
450+ {
451+ normals. extend_from_slice ( new_normals) ;
452+ }
453+
454+ // Append UVs
455+ if let Some ( VertexAttributeValues :: Float32x2 ( new_uvs) ) =
456+ sphere_mesh. attribute ( Mesh :: ATTRIBUTE_UV_0 )
457+ && let Some ( VertexAttributeValues :: Float32x2 ( uvs) ) =
458+ mesh. attribute_mut ( Mesh :: ATTRIBUTE_UV_0 )
459+ {
460+ uvs. extend_from_slice ( new_uvs) ;
461+ }
462+
463+ // Append indices with offset
464+ let new_indices: Vec < u32 > = match sphere_mesh. indices ( ) {
465+ Some ( Indices :: U16 ( vec) ) => vec. iter ( ) . map ( |& i| i as u32 + base_index) . collect ( ) ,
466+ Some ( Indices :: U32 ( vec) ) => vec. iter ( ) . map ( |& i| i + base_index) . collect ( ) ,
467+ None => Vec :: new ( ) ,
468+ } ;
469+
470+ match mesh. indices_mut ( ) {
471+ Some ( Indices :: U32 ( indices) ) => indices. extend ( new_indices) ,
472+ Some ( Indices :: U16 ( indices) ) => {
473+ indices. extend ( new_indices. iter ( ) . map ( |& i| i as u16 ) ) ;
474+ }
475+ None => {
476+ mesh. insert_indices ( Indices :: U32 ( new_indices) ) ;
477+ }
478+ }
479+
480+ Ok ( ( ) )
481+ }
0 commit comments