@@ -385,10 +385,16 @@ pub fn begin(
385385 ] ) )
386386 . id ( ) ;
387387
388- let mesh = Mesh :: new (
388+ let mut mesh = Mesh :: new (
389389 Topology :: TriangleList . to_primitive_topology ( ) ,
390390 RenderAssetUsages :: default ( ) ,
391391 ) ;
392+
393+ mesh. insert_attribute ( Mesh :: ATTRIBUTE_POSITION , Vec :: < [ f32 ; 3 ] > :: new ( ) ) ;
394+ mesh. insert_attribute ( Mesh :: ATTRIBUTE_NORMAL , Vec :: < [ f32 ; 3 ] > :: new ( ) ) ;
395+ mesh. insert_attribute ( Mesh :: ATTRIBUTE_COLOR , Vec :: < [ f32 ; 4 ] > :: new ( ) ) ;
396+ mesh. insert_attribute ( Mesh :: ATTRIBUTE_UV_0 , Vec :: < [ f32 ; 2 ] > :: new ( ) ) ;
397+
392398 let handle = meshes. add ( mesh) ;
393399
394400 let mut state = state_query
@@ -403,6 +409,7 @@ pub fn end(
403409 In ( entity) : In < Entity > ,
404410 mut commands : Commands ,
405411 mut state_query : Query < & mut RenderState > ,
412+ meshes : Res < Assets < Mesh > > ,
406413) -> Result < Entity > {
407414 let geometry = state_query
408415 . get_mut ( entity)
@@ -411,53 +418,94 @@ pub fn end(
411418 . take ( )
412419 . ok_or ( ProcessingError :: GeometryNotFound ) ?;
413420
421+ if let Some ( mesh) = meshes. get ( & geometry. handle ) {
422+ println ! ( "End geometry: {} vertices, {} indices" ,
423+ mesh. count_vertices( ) ,
424+ mesh. indices( ) . map( |i| i. len( ) ) . unwrap_or( 0 )
425+ ) ;
426+ }
427+
414428 Ok ( commands. spawn ( geometry) . id ( ) )
415429}
416430
417431pub fn sphere (
418432 In ( ( entity, radius) ) : In < ( Entity , f32 ) > ,
419433 state_query : Query < & mut RenderState > ,
434+ layouts : Query < & VertexLayout > ,
435+ builtins : Res < BuiltinAttributes > ,
420436 mut meshes : ResMut < Assets < Mesh > > ,
421437) -> Result < ( ) > {
422- let geometry = state_query
438+ let state = state_query
423439 . get ( entity)
424- . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?
440+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
441+
442+ let geometry = state
425443 . running_geometry
426444 . as_ref ( )
427445 . ok_or ( ProcessingError :: GeometryNotFound ) ?;
428446
447+ let layout = layouts
448+ . get ( geometry. layout )
449+ . map_err ( |_| ProcessingError :: LayoutNotFound ) ?;
450+
429451 let mesh = meshes
430452 . get_mut ( & geometry. handle )
431453 . ok_or ( ProcessingError :: GeometryNotFound ) ?;
432454
433455 let base_index = mesh. count_vertices ( ) as u32 ;
434456 let sphere_mesh = Sphere :: new ( radius) . mesh ( ) . build ( ) ;
457+ let vertex_count = sphere_mesh. count_vertices ( ) ;
458+
459+ // Get current transformation matrix from render state
460+ let transform = state. transform . current ( ) ;
435461
436- // Append positions
462+ // Append positions with transformation applied
437463 if let Some ( VertexAttributeValues :: Float32x3 ( new_pos) ) =
438464 sphere_mesh. attribute ( Mesh :: ATTRIBUTE_POSITION )
439465 && let Some ( VertexAttributeValues :: Float32x3 ( positions) ) =
440466 mesh. attribute_mut ( Mesh :: ATTRIBUTE_POSITION )
441467 {
442- positions. extend_from_slice ( new_pos) ;
468+ for pos in new_pos {
469+ let transformed = transform. transform_point3 ( Vec3 :: from_array ( * pos) ) ;
470+ positions. push ( transformed. to_array ( ) ) ;
471+ }
443472 }
444473
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) ;
474+ // Append normals with rotation applied (no translation/scale)
475+ if layout. has_attribute ( builtins. normal ) {
476+ if let Some ( VertexAttributeValues :: Float32x3 ( new_normals) ) =
477+ sphere_mesh. attribute ( Mesh :: ATTRIBUTE_NORMAL )
478+ && let Some ( VertexAttributeValues :: Float32x3 ( normals) ) =
479+ mesh. attribute_mut ( Mesh :: ATTRIBUTE_NORMAL )
480+ {
481+ let normal_matrix = transform. matrix3 ;
482+ for normal in new_normals {
483+ let transformed = normal_matrix. mul_vec3 ( Vec3 :: from_array ( * normal) ) . normalize ( ) ;
484+ normals. push ( transformed. to_array ( ) ) ;
485+ }
486+ }
452487 }
453488
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) ;
489+ // Append colors if in layout - fill with current color
490+ if layout. has_attribute ( builtins. color ) {
491+ if let Some ( VertexAttributeValues :: Float32x4 ( colors) ) =
492+ mesh. attribute_mut ( Mesh :: ATTRIBUTE_COLOR )
493+ {
494+ for _ in 0 ..vertex_count {
495+ colors. push ( geometry. current_color ) ;
496+ }
497+ }
498+ }
499+
500+ // Append UVs if in layout
501+ if layout. has_attribute ( builtins. uv ) {
502+ if let Some ( VertexAttributeValues :: Float32x2 ( new_uvs) ) =
503+ sphere_mesh. attribute ( Mesh :: ATTRIBUTE_UV_0 )
504+ && let Some ( VertexAttributeValues :: Float32x2 ( uvs) ) =
505+ mesh. attribute_mut ( Mesh :: ATTRIBUTE_UV_0 )
506+ {
507+ uvs. extend_from_slice ( new_uvs) ;
508+ }
461509 }
462510
463511 // Append indices with offset
@@ -467,15 +515,27 @@ pub fn sphere(
467515 None => Vec :: new ( ) ,
468516 } ;
469517
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) ) ;
518+ if !new_indices. is_empty ( ) {
519+ match mesh. indices_mut ( ) {
520+ Some ( Indices :: U32 ( indices) ) => {
521+ indices. extend ( new_indices) ;
522+ }
523+ None => {
524+ mesh. insert_indices ( Indices :: U32 ( new_indices) ) ;
525+ }
526+ Some ( Indices :: U16 ( indices) ) => {
527+ if base_index + vertex_count as u32 > u16:: MAX as u32 {
528+ // Need to upgrade to U32
529+ let existing: Vec < u32 > = indices. iter ( ) . map ( |& i| i as u32 ) . collect ( ) ;
530+ mesh. insert_indices ( Indices :: U32 (
531+ existing. into_iter ( ) . chain ( new_indices) . collect ( )
532+ ) ) ;
533+ } else {
534+ indices. extend ( new_indices. iter ( ) . map ( |& i| i as u16 ) ) ;
535+ }
536+ }
477537 }
478538 }
479539
480540 Ok ( ( ) )
481- }
541+ }
0 commit comments