Skip to content

Commit e5d805a

Browse files
committed
Filled scene to generate nonblack image
1 parent 0475ec9 commit e5d805a

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

examples/app/headless_renderer.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ fn main() {
8080

8181
fn setup(
8282
mut commands: Commands,
83+
mut meshes: ResMut<Assets<Mesh>>,
84+
mut materials: ResMut<Assets<StandardMaterial>>,
8385
mut images: ResMut<Assets<Image>>,
8486
mut scene_controller: ResMut<frame_capture::scene::SceneController>,
8587
render_device: Res<RenderDevice>,
@@ -89,16 +91,49 @@ fn setup(
8991
&mut images,
9092
&render_device,
9193
&mut scene_controller,
92-
15,
94+
// pre_roll_frames should be big enought for full scene render,
95+
// but the bigger it is, the longer example will run.
96+
// To visualize stages of scene rendering change this param to 0
97+
// and change AppConfig::single_image to false in main
98+
// Stages are:
99+
// 1. Transparent image
100+
// 2. Few black box images
101+
// 3. Fully rendered scene images
102+
// Exact number depends on device speed, device load and scene size
103+
30,
93104
"main_scene".into(),
94105
);
95106

96-
// Scene is empty, but you can add any mesh to generate non black box picture
107+
// Scene example for non black box picture
108+
// circular base
109+
commands.spawn(PbrBundle {
110+
mesh: meshes.add(Circle::new(4.0)),
111+
material: materials.add(Color::WHITE),
112+
transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
113+
..default()
114+
});
115+
// cube
116+
commands.spawn(PbrBundle {
117+
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
118+
material: materials.add(Color::srgb_u8(124, 144, 255)),
119+
transform: Transform::from_xyz(0.0, 0.5, 0.0),
120+
..default()
121+
});
122+
// light
123+
commands.spawn(PointLightBundle {
124+
point_light: PointLight {
125+
shadows_enabled: true,
126+
..default()
127+
},
128+
transform: Transform::from_xyz(4.0, 8.0, 4.0),
129+
..default()
130+
});
97131

98132
commands.spawn(Camera3dBundle {
99-
transform: Transform::from_xyz(0.0, 6., 12.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y),
133+
transform: Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
100134
tonemapping: Tonemapping::None,
101135
camera: Camera {
136+
// render to image
102137
target: render_target,
103138
..default()
104139
},
@@ -469,10 +504,16 @@ mod frame_capture {
469504

470505
// We don't want to block the main world on this,
471506
// so we use try_recv which attempts to receive without blocking
507+
let mut image_data = Vec::new();
472508
while let Ok(data) = receiver.try_recv() {
509+
// image generation could be faster than saving to fs,
510+
// that's why use only last of them
511+
image_data = data;
512+
}
513+
if !image_data.is_empty() {
473514
for image in images_to_save.iter() {
474515
let img_bytes = images.get_mut(image.id()).unwrap();
475-
img_bytes.data = data.clone();
516+
img_bytes.data = image_data.clone();
476517

477518
let img = match img_bytes.clone().try_into_dynamic() {
478519
Ok(img) => img.to_rgba8(),
@@ -492,7 +533,6 @@ mod frame_capture {
492533
}
493534
if scene_controller.single_image {
494535
app_exit_writer.send(AppExit::Success);
495-
break;
496536
}
497537
}
498538
} else {

0 commit comments

Comments
 (0)