forked from rerun-io/rerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiview.rs
434 lines (376 loc) · 14.4 KB
/
multiview.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Example with several independent views, using various primitives.
use std::f32::consts::TAU;
use framework::Example;
use glam::Vec3;
use itertools::Itertools;
use macaw::IsoTransform;
use rand::Rng;
use re_renderer::{
renderer::{
GenericSkyboxDrawData, LineDrawData, LineStripFlags, MeshDrawData, MeshInstance,
TestTriangleDrawData,
},
view_builder::{OrthographicCameraMode, Projection, TargetConfiguration, ViewBuilder},
Color32, GpuReadbackIdentifier, Hsva, LineDrawableBuilder, PointCloudBuilder, RenderContext,
Rgba, ScreenshotProcessor, Size,
};
use winit::{event::ElementState, keyboard};
mod framework;
fn build_mesh_instances(
re_ctx: &RenderContext,
model_mesh_instances: &[MeshInstance],
mesh_instance_positions_and_colors: &[(glam::Vec3, Color32)],
seconds_since_startup: f32,
) -> MeshDrawData {
let mesh_instances = mesh_instance_positions_and_colors
.chunks_exact(model_mesh_instances.len())
.enumerate()
.flat_map(|(i, positions_and_colors)| {
model_mesh_instances.iter().zip(positions_and_colors).map(
move |(model_mesh_instances, (p, c))| MeshInstance {
gpu_mesh: model_mesh_instances.gpu_mesh.clone(),
mesh: None,
world_from_mesh: glam::Affine3A::from_scale_rotation_translation(
glam::vec3(
2.5 + (i % 3) as f32,
2.5 + (i % 7) as f32,
2.5 + (i % 11) as f32,
) * 0.01,
glam::Quat::from_rotation_y(i as f32 + seconds_since_startup * 5.0),
*p,
) * model_mesh_instances.world_from_mesh,
additive_tint: *c,
..Default::default()
},
)
})
.collect_vec();
MeshDrawData::new(re_ctx, &mesh_instances).unwrap()
}
fn lorenz_points(seconds_since_startup: f32) -> Vec<glam::Vec3> {
// Lorenz attractor https://en.wikipedia.org/wiki/Lorenz_system
fn lorenz_integrate(cur: glam::Vec3, dt: f32) -> glam::Vec3 {
let sigma: f32 = 10.0;
let rho: f32 = 28.0;
let beta: f32 = 8.0 / 3.0;
cur + glam::vec3(
sigma * (cur.y - cur.x),
cur.x * (rho - cur.z) - cur.y,
cur.x * cur.y - beta * cur.z,
) * dt
}
// slow buildup and reset
let num_points = (((seconds_since_startup * 0.05).fract() * 10000.0) as u32).max(1);
let mut latest_point = glam::vec3(-0.1, 0.001, 0.0);
std::iter::repeat_with(move || {
latest_point = lorenz_integrate(latest_point, 0.005);
latest_point
})
// lorenz system is sensitive to start conditions (.. that's the whole point), so transform after the fact
.map(|p| (p + glam::vec3(-5.0, 0.0, -23.0)) * 0.6)
.take(num_points as _)
.collect()
}
fn build_lines(re_ctx: &RenderContext, seconds_since_startup: f32) -> LineDrawData {
// Calculate some points that look nice for an animated line.
let lorenz_points = lorenz_points(seconds_since_startup);
let mut builder = LineDrawableBuilder::new(re_ctx);
builder
.reserve_vertices(lorenz_points.len() + 4 + 1000)
.unwrap();
{
let mut batch = builder.batch("lines without transform");
// Complex orange line.
batch
.add_strip(lorenz_points.into_iter())
.color(Color32::from_rgb(255, 191, 0))
.flags(LineStripFlags::FLAG_COLOR_GRADIENT)
.radius(Size::new_points(1.0));
// Green Zig-Zag arrow
batch
.add_strip(
[
glam::vec3(0.0, -1.0, 0.0),
glam::vec3(1.0, 0.0, 0.0),
glam::vec3(2.0, -1.0, 0.0),
glam::vec3(3.0, 0.0, 0.0),
]
.into_iter(),
)
.color(Color32::GREEN)
.radius(Size::new_scene(0.05))
.flags(
LineStripFlags::FLAG_COLOR_GRADIENT
| LineStripFlags::FLAG_CAP_END_TRIANGLE
| LineStripFlags::FLAG_CAP_START_ROUND,
);
}
// Blue spiral, rotating
builder
.batch("blue spiral")
.world_from_obj(glam::Affine3A::from_rotation_x(
seconds_since_startup * 10.0,
))
.add_strip((0..1000).map(|i| {
glam::vec3(
(i as f32 * 0.01).sin() * 2.0,
i as f32 * 0.01 - 6.0,
(i as f32 * 0.01).cos() * 2.0,
)
}))
.color(Color32::BLUE)
.radius(Size::new_scene(0.1))
.flags(LineStripFlags::FLAG_CAP_END_TRIANGLE);
builder.into_draw_data().unwrap()
}
enum CameraControl {
RotateAroundCenter,
// TODO(andreas): Only pauses rotation right now. Add camera controller.
Manual,
}
struct Multiview {
perspective_projection: bool,
camera_control: CameraControl,
camera_position: glam::Vec3,
model_mesh_instances: Vec<MeshInstance>,
mesh_instance_positions_and_colors: Vec<(glam::Vec3, Color32)>,
// Want to have a large cloud of random points, but doing rng for all of them every frame is too slow
random_points_positions: Vec<glam::Vec3>,
random_points_radii: Vec<Size>,
random_points_colors: Vec<Color32>,
take_screenshot_next_frame_for_view: Option<u32>,
}
fn random_color(rnd: &mut impl rand::Rng) -> Color32 {
Hsva {
h: rnd.gen::<f32>(),
s: rnd.gen::<f32>() * 0.5 + 0.5,
v: rnd.gen::<f32>() * 0.5 + 0.5,
a: 1.0,
}
.into()
}
/// Readback identifier for screenshots.
/// Identifiers don't need to be unique and we don't have anything interesting to distinguish here!
const READBACK_IDENTIFIER: GpuReadbackIdentifier = 0;
fn handle_incoming_screenshots(re_ctx: &RenderContext) {
ScreenshotProcessor::next_readback_result(
re_ctx,
READBACK_IDENTIFIER,
|data, _extent, view_idx: u32| {
re_log::info!(
"Received screenshot for view {view_idx}. Total bytes {:?}",
data.len()
);
#[cfg(not(target_arch = "wasm32"))]
{
// Get next available file name.
let mut i = 1;
let filename = loop {
let filename = format!("screenshot_{i}.png");
if !std::path::Path::new(&filename).exists() {
break filename;
}
i += 1;
};
image::save_buffer(
filename,
data,
_extent.x,
_extent.y,
image::ColorType::Rgba8,
)
.expect("Failed to save screenshot");
}
},
);
}
impl Multiview {
fn draw_view<D: 'static + re_renderer::renderer::DrawData + Sync + Send + Clone>(
&mut self,
re_ctx: &RenderContext,
target_cfg: TargetConfiguration,
skybox: GenericSkyboxDrawData,
draw_data: D,
index: u32,
) -> (ViewBuilder, wgpu::CommandBuffer) {
let mut view_builder = ViewBuilder::new(re_ctx, target_cfg);
if self
.take_screenshot_next_frame_for_view
.map_or(false, |i| i == index)
{
view_builder
.schedule_screenshot(re_ctx, READBACK_IDENTIFIER, index)
.unwrap();
re_log::info!("Scheduled screenshot for view {}", index);
}
let command_buffer = view_builder
.queue_draw(skybox)
.queue_draw(draw_data)
.draw(re_ctx, Rgba::TRANSPARENT)
.unwrap();
(view_builder, command_buffer)
}
}
impl Example for Multiview {
fn title() -> &'static str {
"Multiple Views"
}
fn new(re_ctx: &RenderContext) -> Self {
re_log::info!("Switch between orthographic & perspective by pressing 'O'");
re_log::info!("Stop camera movement by pressing 'Space'");
let mut rnd = <rand::rngs::StdRng as rand::SeedableRng>::seed_from_u64(42);
let random_point_range = -5.0_f32..5.0_f32;
let point_count = 500000;
let random_points_positions = (0..point_count)
.map(|_| {
glam::vec3(
rnd.gen_range(random_point_range.clone()),
rnd.gen_range(random_point_range.clone()),
rnd.gen_range(random_point_range.clone()),
)
})
.collect_vec();
let random_points_radii = (0..point_count)
.map(|_| Size::new_scene(rnd.gen_range(0.005..0.05)))
.collect_vec();
let random_points_colors = (0..point_count)
.map(|_| random_color(&mut rnd))
.collect_vec();
let model_mesh_instances = crate::framework::load_rerun_mesh(re_ctx);
let mesh_instance_positions_and_colors = lorenz_points(10.0)
.iter()
.flat_map(|p| {
model_mesh_instances.iter().map(|_| {
let mut rnd = rand::thread_rng();
(*p, random_color(&mut rnd))
})
})
.collect();
Self {
perspective_projection: true,
camera_control: CameraControl::RotateAroundCenter,
camera_position: glam::Vec3::ZERO,
model_mesh_instances,
mesh_instance_positions_and_colors,
random_points_positions,
random_points_radii,
random_points_colors,
take_screenshot_next_frame_for_view: None,
}
}
fn draw(
&mut self,
re_ctx: &RenderContext,
resolution: [u32; 2],
time: &framework::Time,
pixels_from_point: f32,
) -> Vec<framework::ViewDrawResult> {
if matches!(self.camera_control, CameraControl::RotateAroundCenter) {
let seconds_since_startup = time.seconds_since_startup();
self.camera_position = Vec3::new(
seconds_since_startup.sin(),
0.5,
seconds_since_startup.cos(),
) * 10.0;
}
handle_incoming_screenshots(re_ctx);
let seconds_since_startup = time.seconds_since_startup();
let view_from_world =
IsoTransform::look_at_rh(self.camera_position, Vec3::ZERO, Vec3::Y).unwrap();
let triangle = TestTriangleDrawData::new(re_ctx);
let skybox = GenericSkyboxDrawData::new(re_ctx, Default::default());
let lines = build_lines(re_ctx, seconds_since_startup);
let mut builder = PointCloudBuilder::new(re_ctx);
builder
.batch("Random Points")
.world_from_obj(glam::Affine3A::from_rotation_x(seconds_since_startup))
.add_points(
&self.random_points_positions,
&self.random_points_radii,
&self.random_points_colors,
&[],
);
let point_cloud = builder.into_draw_data().unwrap();
let meshes = build_mesh_instances(
re_ctx,
&self.model_mesh_instances,
&self.mesh_instance_positions_and_colors,
seconds_since_startup,
);
let splits = framework::split_resolution(resolution, 2, 2).collect::<Vec<_>>();
let projection_from_view = if self.perspective_projection {
Projection::Perspective {
vertical_fov: 70.0 * TAU / 360.0,
near_plane_distance: 0.01,
aspect_ratio: resolution[0] as f32 / resolution[1] as f32,
}
} else {
Projection::Orthographic {
camera_mode: OrthographicCameraMode::NearPlaneCenter,
vertical_world_size: 15.0,
far_plane_distance: 100000.0,
}
};
// Using a macro here because `DrawData` isn't object safe and a closure cannot be
// generic over its input type.
#[rustfmt::skip]
macro_rules! draw {
($name:ident @ split #$n:expr) => {{
let (view_builder, command_buffer) = self.draw_view(re_ctx,
TargetConfiguration {
name: stringify!($name).into(),
resolution_in_pixel: splits[$n].resolution_in_pixel,
view_from_world,
projection_from_view: projection_from_view.clone(),
pixels_from_point,
..Default::default()
},
skybox.clone(),
$name,
$n,
);
framework::ViewDrawResult {
view_builder,
command_buffer,
target_location: splits[$n].target_location,
}
}};
}
let draw_results = vec![
draw!(triangle @ split #0),
draw!(lines @ split #1),
draw!(meshes @ split #2),
draw!(point_cloud @ split #3),
];
self.take_screenshot_next_frame_for_view = None;
draw_results
}
fn on_key_event(&mut self, input: winit::event::KeyEvent) {
if input.state == ElementState::Pressed {
if input.logical_key == keyboard::Key::Character("O".into()) {
self.perspective_projection = !self.perspective_projection;
}
if input.logical_key == keyboard::Key::Named(keyboard::NamedKey::Space) {
self.camera_control = match self.camera_control {
CameraControl::RotateAroundCenter => CameraControl::Manual,
CameraControl::Manual => CameraControl::RotateAroundCenter,
};
}
if input.logical_key == keyboard::Key::Character("1".into()) {
self.take_screenshot_next_frame_for_view = Some(0);
}
if input.logical_key == keyboard::Key::Character("2".into()) {
self.take_screenshot_next_frame_for_view = Some(1);
}
if input.logical_key == keyboard::Key::Character("3".into()) {
self.take_screenshot_next_frame_for_view = Some(2);
}
if input.logical_key == keyboard::Key::Character("4".into()) {
self.take_screenshot_next_frame_for_view = Some(3);
}
}
}
}
fn main() {
framework::start::<Multiview>();
}