@@ -29,6 +29,7 @@ struct EntityData {
2929pub struct WebSimulation {
3030 simulation : simulation:: Simulation ,
3131 config : config:: SimulationConfig ,
32+ entity_buffer : Vec < f32 > , // Reusable buffer for entity data
3233}
3334
3435#[ wasm_bindgen]
@@ -40,7 +41,11 @@ impl WebSimulation {
4041
4142 let simulation = simulation:: Simulation :: new_with_config ( world_size, config. clone ( ) ) ;
4243
43- Ok ( WebSimulation { simulation, config } )
44+ Ok ( WebSimulation {
45+ simulation,
46+ config,
47+ entity_buffer : Vec :: with_capacity ( 60000 ) , // 10000 entities * 6 floats
48+ } )
4449 }
4550
4651 pub fn update ( & mut self ) {
@@ -63,6 +68,27 @@ impl WebSimulation {
6368 serde_wasm_bindgen:: to_value ( & entities) . unwrap_or ( JsValue :: NULL )
6469 }
6570
71+ /// Update entity buffer and return pointer for WebGPU renderer
72+ pub fn update_entity_buffer ( & mut self ) -> * const f32 {
73+ let entity_tuples = self . simulation . get_entities ( ) ;
74+ self . entity_buffer . clear ( ) ;
75+
76+ for ( x, y, radius, r, g, b) in entity_tuples {
77+ self . entity_buffer . push ( x) ;
78+ self . entity_buffer . push ( y) ;
79+ self . entity_buffer . push ( radius) ;
80+ self . entity_buffer . push ( r) ;
81+ self . entity_buffer . push ( g) ;
82+ self . entity_buffer . push ( b) ;
83+ }
84+
85+ self . entity_buffer . as_ptr ( )
86+ }
87+
88+ pub fn entity_count ( & self ) -> u32 {
89+ ( self . entity_buffer . len ( ) / 6 ) as u32
90+ }
91+
6692 pub fn get_stats ( & self ) -> JsValue {
6793 let stats = stats:: SimulationStats :: from_world (
6894 self . simulation . world ( ) ,
0 commit comments