1- use std:: { ops:: Deref , sync:: Arc } ;
1+ use std:: { marker :: PhantomData , ops:: Deref , sync:: Arc } ;
22
33use crate :: {
44 core:: debug:: {
@@ -15,11 +15,15 @@ use super::{
1515 field_boundary:: FieldBoundary ,
1616 scan_grid:: { FieldColorApproximate , ScanGrid } ,
1717} ;
18- use bevy:: prelude:: * ;
18+ use bevy:: {
19+ prelude:: * ,
20+ tasks:: { AsyncComputeTaskPool , Task } ,
21+ } ;
1922
2023use heimdall:: { Bottom , CameraLocation , CameraPosition , Top , YuvPixel , YuyvImage } ;
2124use nalgebra:: Point2 ;
2225use serde:: { Deserialize , Serialize } ;
26+ use tasks:: { CommandsExt , conditions:: task_finished} ;
2327use yggdrasil_rerun_comms:: protocol:: control:: FieldColorConfig ;
2428
2529#[ derive( Resource , Debug , Clone , Serialize , Deserialize ) ]
@@ -100,12 +104,14 @@ impl Plugin for ScanLinesPlugin {
100104 . add_systems (
101105 Update ,
102106 (
103- update_scan_lines :: < Top >
107+ spawn_scan_lines_task :: < Top >
104108 . after ( super :: scan_grid:: update_top_scan_grid)
105- . run_if ( resource_exists_and_changed :: < ScanGrid < Top > > ) ,
106- update_scan_lines :: < Bottom >
109+ . run_if ( resource_exists_and_changed :: < ScanGrid < Top > > )
110+ . run_if ( task_finished :: < ScanLines < Top > > ) ,
111+ spawn_scan_lines_task :: < Bottom >
107112 . after ( super :: scan_grid:: update_bottom_scan_grid)
108- . run_if ( resource_exists_and_changed :: < ScanGrid < Bottom > > ) ,
113+ . run_if ( resource_exists_and_changed :: < ScanGrid < Bottom > > )
114+ . run_if ( task_finished :: < ScanLines < Bottom > > ) ,
109115 ) ,
110116 )
111117 . add_named_debug_systems (
@@ -174,6 +180,29 @@ impl<T: CameraLocation> ScanLines<T> {
174180 }
175181}
176182
183+ fn spawn_scan_lines_task < T : CameraLocation > (
184+ mut commands : Commands ,
185+ cfg : Res < ScanLinesConfig > ,
186+ image : Res < Image < T > > ,
187+ grid : Res < ScanGrid < T > > ,
188+ field_boundary : Res < FieldBoundary > ,
189+ ) {
190+ commands
191+ . prepare_task ( tasks:: TaskPool :: AsyncCompute )
192+ . to_resource ( )
193+ . spawn ( {
194+ let cfg = cfg. clone ( ) ;
195+ let image = image. clone ( ) ;
196+ let grid = grid. clone ( ) ;
197+ let field_boundary = field_boundary. clone ( ) ;
198+
199+ async move {
200+ // Run the scan lines generation in a separate task
201+ Some ( get_scan_lines ( & cfg, image, & grid, & field_boundary) )
202+ }
203+ } ) ;
204+ }
205+
177206/// A set of classified scanline regions.
178207#[ derive( Debug , Default ) ]
179208pub struct ScanLine {
@@ -640,15 +669,15 @@ pub fn init_scan_lines<T: CameraLocation>(mut commands: Commands, image: Res<Ima
640669 commands. insert_resource ( scan_lines) ;
641670}
642671
643- pub fn update_scan_lines < T : CameraLocation > (
644- config : Res < ScanLinesConfig > ,
645- mut scan_lines : ResMut < ScanLines < T > > ,
646- image : Res < Image < T > > ,
647- scan_grid : Res < ScanGrid < T > > ,
648- field_boundary : Res < FieldBoundary > ,
649- ) {
650- * scan_lines = get_scan_lines ( & config, image. clone ( ) , & scan_grid, & field_boundary) ;
651- }
672+ // pub fn update_scan_lines<T: CameraLocation>(
673+ // config: Res<ScanLinesConfig>,
674+ // mut scan_lines: ResMut<ScanLines<T>>,
675+ // image: Res<Image<T>>,
676+ // scan_grid: Res<ScanGrid<T>>,
677+ // field_boundary: Res<FieldBoundary>,
678+ // ) {
679+ // *scan_lines = get_scan_lines(&config, image.clone(), &scan_grid, &field_boundary);
680+ // }
652681
653682/// Find the edge of a region in a scanline.
654683///
0 commit comments