Skip to main content

spawn_objects

Function spawn_objects 

Source
pub fn spawn_objects(
    world: &mut World,
    object: Object,
    positions: &[Vec3],
) -> Vec<Entity>
Expand description

Spawns one Object at each position, all sharing a single registered material, which is what writing it longhand for a crowd looks like. A thousand entities through this hold one material entry, not a thousand.

Examples found in repository?
examples/parity.rs (lines 13-20)
7fn main() {
8    let mut app = open();
9    show_grid(&mut app.world, false);
10    orbit_camera(&mut app.world, vec3(0.0, 0.5, 0.0), 45.0);
11
12    let positions = grid_positions();
13    let mut entities = spawn_objects(
14        &mut app.world,
15        Object {
16            color: TEAL,
17            ..Object::default()
18        },
19        &positions,
20    );
21
22    let label = spawn_text(
23        &mut app.world,
24        "phase 1: api spelling",
25        ScreenAnchor::TopLeft,
26    );
27
28    let mut frame_index = 0u32;
29    let mut api_total = Duration::ZERO;
30    let mut api_frames = 0u32;
31    let mut longhand_total = Duration::ZERO;
32    let mut longhand_frames = 0u32;
33
34    while frame(&mut app) {
35        let step = delta_time(&app.world);
36        if frame_index < PHASE_FRAMES {
37            let start = Instant::now();
38            for &entity in &entities {
39                rotate(&mut app.world, entity, Vec3::y(), step);
40            }
41            api_total += start.elapsed();
42            api_frames += 1;
43            if frame_index + 1 == PHASE_FRAMES {
44                for &entity in &entities {
45                    despawn(&mut app.world, entity);
46                }
47                entities = longhand::spawn_grid(&mut app.world, &positions, ORANGE);
48                app.world.resources.mesh_render_state.request_full_rebuild();
49                set_text(&mut app.world, label, "phase 2: longhand spelling");
50            }
51        } else {
52            let start = Instant::now();
53            for &entity in &entities {
54                longhand::rotate(&mut app.world, entity, Vec3::y(), step);
55            }
56            longhand_total += start.elapsed();
57            longhand_frames += 1;
58            if frame_index + 1 == PHASE_FRAMES * 2 {
59                break;
60            }
61        }
62        frame_index += 1;
63    }
64
65    report("api", api_total, api_frames);
66    report("longhand", longhand_total, longhand_frames);
67}