-
Notifications
You must be signed in to change notification settings - Fork 251
/
view3d.rs
595 lines (520 loc) · 19.3 KB
/
view3d.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
mod eye;
mod mesh_cache;
mod rendering;
mod scene;
pub use mesh_cache::CpuMeshCache;
use eye::*;
use re_data_store::{InstanceId, InstanceIdHash};
use rendering::*;
use scene::*;
use egui::NumExt as _;
use glam::Affine3A;
use macaw::{vec3, Quat, Ray3, Vec3};
use re_log_types::ObjPath;
use crate::{
misc::{HoveredSpace, Selection},
ViewerContext,
};
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub(crate) struct State3D {
orbit_eye: Option<OrbitEye>,
#[serde(skip)]
eye_interpolation: Option<EyeInterpolation>,
/// What the mouse is hovering (from previous frame)
#[serde(skip)]
hovered_instance: Option<InstanceId>,
/// Where in world space the mouse is hovering (from previous frame)
#[serde(skip)]
hovered_point: Option<glam::Vec3>,
/// Estimate of the the bounding box of all data. Accumulated.
#[serde(skip)]
scene_bbox: macaw::BoundingBox,
// options:
spin: bool,
show_axes: bool,
last_eye_interact_time: f64,
}
impl Default for State3D {
fn default() -> Self {
Self {
orbit_eye: Default::default(),
eye_interpolation: Default::default(),
hovered_instance: Default::default(),
hovered_point: Default::default(),
scene_bbox: macaw::BoundingBox::nothing(),
spin: false,
show_axes: false,
last_eye_interact_time: f64::NEG_INFINITY,
}
}
}
impl State3D {
fn update_eye(
&mut self,
ctx: &mut ViewerContext<'_>,
tracking_camera: Option<Eye>,
response: &egui::Response,
space_specs: &SpaceSpecs,
) -> &mut OrbitEye {
if response.double_clicked() {
// Reset camera
if tracking_camera.is_some() {
ctx.rec_cfg.selection = Selection::None;
}
self.interpolate_to_orbit_eye(default_eye(&self.scene_bbox, space_specs));
}
if let Some(tracking_camera) = tracking_camera {
if let Some(cam_interpolation) = &mut self.eye_interpolation {
// Update interpolation target:
cam_interpolation.target_orbit = None;
if cam_interpolation.target_eye != Some(tracking_camera) {
cam_interpolation.target_eye = Some(tracking_camera);
response.ctx.request_repaint();
}
} else {
self.interpolate_to_eye(tracking_camera);
}
}
let orbit_camera = self
.orbit_eye
.get_or_insert_with(|| default_eye(&self.scene_bbox, space_specs));
if self.spin {
orbit_camera.rotate(egui::vec2(
-response.ctx.input().stable_dt.at_most(0.1) * 150.0,
0.0,
));
response.ctx.request_repaint();
}
if let Some(cam_interpolation) = &mut self.eye_interpolation {
cam_interpolation.elapsed_time += response.ctx.input().stable_dt.at_most(0.1);
let t = cam_interpolation.elapsed_time / cam_interpolation.target_time;
let t = t.clamp(0.0, 1.0);
let t = crate::math::ease_out(t);
if t < 1.0 {
response.ctx.request_repaint();
}
if let Some(target_orbit) = &cam_interpolation.target_orbit {
*orbit_camera = cam_interpolation.start.lerp(target_orbit, t);
} else if let Some(target_camera) = &cam_interpolation.target_eye {
let camera = cam_interpolation.start.to_eye().lerp(target_camera, t);
orbit_camera.copy_from_eye(&camera);
} else {
self.eye_interpolation = None;
}
}
orbit_camera
}
fn interpolate_to_eye(&mut self, target: Eye) {
if let Some(start) = self.orbit_eye {
let target_time = EyeInterpolation::target_time(&start.to_eye(), &target);
self.eye_interpolation = Some(EyeInterpolation {
elapsed_time: 0.0,
target_time,
start,
target_orbit: None,
target_eye: Some(target),
});
} else {
// shouldn't really happen (`self.orbit_eye` is only `None` for the first frame).
}
}
fn interpolate_to_orbit_eye(&mut self, target: OrbitEye) {
if let Some(start) = self.orbit_eye {
let target_time = EyeInterpolation::target_time(&start.to_eye(), &target.to_eye());
self.eye_interpolation = Some(EyeInterpolation {
elapsed_time: 0.0,
target_time,
start,
target_orbit: Some(target),
target_eye: None,
});
} else {
self.orbit_eye = Some(target);
}
}
}
struct EyeInterpolation {
elapsed_time: f32,
target_time: f32,
start: OrbitEye,
target_orbit: Option<OrbitEye>,
target_eye: Option<Eye>,
}
impl EyeInterpolation {
pub fn target_time(start: &Eye, stop: &Eye) -> f32 {
// Take more time if the rotation is big:
let angle_difference = start
.world_from_view
.rotation()
.angle_between(stop.world_from_view.rotation());
egui::remap_clamp(angle_difference, 0.0..=std::f32::consts::PI, 0.2..=0.7)
}
}
fn show_settings_ui(
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut State3D,
space_specs: &SpaceSpecs,
) {
ui.horizontal(|ui| {
{
let up = space_specs.up.normalize_or_zero();
let up_response = if up == Vec3::X {
ui.label("Up: +X")
} else if up == -Vec3::X {
ui.label("Up: -X")
} else if up == Vec3::Y {
ui.label("Up: +Y")
} else if up == -Vec3::Y {
ui.label("Up: -Y")
} else if up == Vec3::Z {
ui.label("Up: +Z")
} else if up == -Vec3::Z {
ui.label("Up: -Z")
} else if up != Vec3::ZERO {
ui.label(format!("Up: [{:.3} {:.3} {:.3}]", up.x, up.y, up.z))
} else {
ui.label("Up: —")
};
up_response.on_hover_ui(|ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("Set with ");
ui.code("rerun.set_space_up");
ui.label(".");
});
});
}
if ui
.button("Reset view")
.on_hover_text("You can also double-click the 3D view")
.clicked()
{
state.orbit_eye = Some(default_eye(&state.scene_bbox, space_specs));
state.eye_interpolation = None;
// TODO(emilk): reset tracking camera too
}
// TODO(emilk): only show if there is a camera om scene.
ui.toggle_value(&mut ctx.options.show_camera_mesh_in_3d, "📷")
.on_hover_text("Show camera mesh");
ui.toggle_value(&mut state.spin, "Spin")
.on_hover_text("Spin view");
ui.toggle_value(&mut state.show_axes, "Axes")
.on_hover_text("Show X-Y-Z axes");
crate::misc::help_hover_button(ui).on_hover_text(
"Drag to rotate.\n\
Drag with secondary mouse button to pan.\n\
Drag with middle mouse button to roll the view.\n\
Scroll to zoom.\n\
\n\
While hovering the 3D view, navigate with WSAD and QE.\n\
CTRL slows down, SHIFT speeds up.\n\
\n\
Click on a object to focus the view on it.\n\
\n\
Double-click anywhere to reset the view.",
);
});
}
#[derive(Default)]
struct SpaceSpecs {
/// ZERO = unset
up: glam::Vec3,
}
impl SpaceSpecs {
fn from_objects(space: Option<&ObjPath>, objects: &re_data_store::Objects<'_>) -> Self {
if let Some(space) = space {
if let Some(space) = objects.space.get(&space) {
return SpaceSpecs {
up: Vec3::from(*space.up).normalize_or_zero(),
};
}
}
Default::default()
}
}
/// If the path to a camera is selected, we follow that camera.
fn tracking_camera(ctx: &ViewerContext<'_>, objects: &re_data_store::Objects<'_>) -> Option<Eye> {
if let Selection::Instance(selected) = &ctx.rec_cfg.selection {
find_camera(objects, selected)
} else {
None
}
}
fn find_camera(objects: &re_data_store::Objects<'_>, needle: &InstanceId) -> Option<Eye> {
let mut found_camera = None;
for (props, camera) in objects.camera.iter() {
if needle.is_instance(props) {
if found_camera.is_some() {
return None; // More than one camera
} else {
found_camera = Some(camera.camera);
}
}
}
found_camera.map(Eye::from_camera)
}
fn click_object(
ctx: &mut ViewerContext<'_>,
objects: &re_data_store::Objects<'_>,
state: &mut State3D,
instance_id: &InstanceId,
) {
ctx.rec_cfg.selection = crate::Selection::Instance(instance_id.clone());
if let Some(camera) = find_camera(objects, instance_id) {
state.interpolate_to_eye(camera);
} else if let Some(clicked_point) = state.hovered_point {
// center camera on what we click on
if let Some(mut new_orbit_eye) = state.orbit_eye {
new_orbit_eye.orbit_radius = new_orbit_eye.position().distance(clicked_point);
new_orbit_eye.orbit_center = clicked_point;
state.interpolate_to_orbit_eye(new_orbit_eye);
}
}
}
pub(crate) fn view_3d(
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut State3D,
space: Option<&ObjPath>,
objects: &re_data_store::Objects<'_>,
) -> egui::Response {
crate::profile_function!();
state.scene_bbox = state.scene_bbox.union(crate::misc::calc_bbox_3d(objects));
let space_specs = SpaceSpecs::from_objects(space, objects);
// TODO(emilk): show settings on top of 3D view.
// Requires some egui work to handle interaction of overlapping widgets.
show_settings_ui(ctx, ui, state, &space_specs);
let (rect, response) = ui.allocate_at_least(ui.available_size(), egui::Sense::click_and_drag());
let tracking_camera = tracking_camera(ctx, objects);
let orbit_eye = state.update_eye(ctx, tracking_camera, &response, &space_specs);
let did_interact_wth_eye = orbit_eye.interact(&response);
let orbit_eye = *orbit_eye;
let eye = orbit_eye.to_eye();
if did_interact_wth_eye {
state.last_eye_interact_time = ui.input().time;
state.eye_interpolation = None;
if tracking_camera.is_some() {
ctx.rec_cfg.selection = Selection::None;
}
}
let mut hovered_instance = state.hovered_instance.clone();
if ui.input().pointer.any_click() {
if let Some(hovered_instance) = &hovered_instance {
click_object(ctx, objects, state, hovered_instance);
}
} else if ui.input().pointer.any_down() {
hovered_instance = None;
}
if let Some(instance_id) = &hovered_instance {
egui::containers::popup::show_tooltip_at_pointer(
ui.ctx(),
egui::Id::new("3d_tooltip"),
|ui| {
ctx.instance_id_button(ui, instance_id);
crate::ui::data_ui::view_instance(ctx, ui, instance_id, crate::ui::Preview::Medium);
},
);
}
let mut scene = Scene::from_objects(
ctx,
&state.scene_bbox,
rect.size(),
&eye,
hovered_instance.as_ref(),
objects,
);
let hovered = response
.hover_pos()
.and_then(|pointer_pos| scene.picking(pointer_pos, &rect, &eye));
state.hovered_instance = None;
state.hovered_point = None;
if let Some((instance_id, point)) = hovered {
if let Some(instance_id) = instance_id.resolve(&ctx.log_db.data_store) {
state.hovered_instance = Some(instance_id);
state.hovered_point = Some(point);
}
}
project_onto_other_spaces(ctx, state, space, &response, orbit_eye, objects);
show_projections_from_2d_space(ctx, objects, state, orbit_eye, &mut scene);
{
let orbit_center_alpha = egui::remap_clamp(
ui.input().time - state.last_eye_interact_time,
0.0..=0.4,
0.7..=0.0,
) as f32;
if orbit_center_alpha > 0.0 {
// Show center of orbit camera when interacting with camera (it's quite helpful).
scene.points.push(Point {
instance_id: InstanceIdHash::NONE,
pos: orbit_eye.orbit_center.to_array(),
radius: orbit_eye.orbit_radius * 0.01,
color: [255, 0, 255, (orbit_center_alpha * 255.0) as u8],
});
ui.ctx().request_repaint(); // let it fade out
}
}
let dark_mode = ui.visuals().dark_mode;
let show_axes = state.show_axes;
let callback = egui::PaintCallback {
rect,
callback: std::sync::Arc::new(egui_glow::CallbackFn::new(move |info, painter| {
with_three_d_context(painter.gl(), |rendering| {
paint_with_three_d(
rendering, &eye, &info, &scene, dark_mode, show_axes, painter,
);
});
})),
};
ui.painter().add(callback);
response
}
fn show_projections_from_2d_space(
ctx: &mut ViewerContext<'_>,
objects: &re_data_store::Objects<'_>,
state: &mut State3D,
orbit_eye: OrbitEye,
scene: &mut Scene,
) {
let eye_pos = orbit_eye.position();
if let HoveredSpace::TwoD { space_2d, pos } = &ctx.rec_cfg.hovered_space {
for (_, cam) in objects.camera.iter() {
let cam = cam.camera;
if &cam.target_space == space_2d {
if let Some(ray) = crate::misc::cam::unproject_as_ray(cam, glam::vec2(pos.x, pos.y))
{
// TODO(emilk): better visualization of a ray
let mut hit_pos = None;
if pos.z.is_finite() && pos.z > 0.0 {
if let Some(world_from_image) = crate::misc::cam::world_from_image(cam) {
let pos = world_from_image
.transform_point3(glam::vec3(pos.x, pos.y, 1.0) * pos.z);
hit_pos = Some(pos);
}
}
let length = if let Some(hit_pos) = hit_pos {
hit_pos.distance(Vec3::from_slice(&cam.position))
} else {
4.0 * state.scene_bbox.half_size().length() // should be long enough
};
let origin = ray.point_along(0.0);
let end = ray.point_along(length);
let distance =
crate::math::line_segment_distance_to_point_3d([origin, end], eye_pos);
let radius = 2.0 * scene.line_radius_from_distance * distance;
scene.line_segments.push(LineSegments {
instance_id: InstanceIdHash::NONE,
segments: vec![[origin.into(), end.into()]],
radius,
color: [255; 4],
});
if let Some(pos) = hit_pos {
// Show where the ray hits the depth map:
scene.points.push(Point {
instance_id: InstanceIdHash::NONE,
pos: pos.into(),
radius: radius * 3.0,
color: [255; 4],
});
}
}
}
}
}
}
fn project_onto_other_spaces(
ctx: &mut ViewerContext<'_>,
state: &mut State3D,
space: Option<&ObjPath>,
response: &egui::Response,
orbit_eye: OrbitEye,
objects: &re_data_store::Objects<'_>,
) {
if let Some(pos_in_ui) = response.hover_pos() {
let ray_in_world = {
let eye = orbit_eye.to_eye();
let world_from_ui = eye.world_from_ui(&response.rect);
let ray_origin = eye.pos_in_world();
let ray_dir = world_from_ui.project_point3(glam::vec3(pos_in_ui.x, pos_in_ui.y, 1.0))
- ray_origin;
Ray3::from_origin_dir(ray_origin, ray_dir.normalize())
};
let mut target_spaces = vec![];
for (_, cam) in objects.camera.iter() {
let cam = cam.camera;
if let Some(target_space) = cam.target_space.clone() {
let ray_in_2d = crate::misc::cam::image_from_world(cam)
.map(|image_from_world| (image_from_world * ray_in_world).normalize());
let point_in_2d = state.hovered_point.and_then(|hovered_point| {
crate::misc::cam::project_onto_2d(cam, hovered_point)
});
target_spaces.push((target_space, ray_in_2d, point_in_2d));
}
}
ctx.rec_cfg.hovered_space = HoveredSpace::ThreeD {
space_3d: space.cloned(),
target_spaces,
}
}
}
fn default_eye(scene_bbox: &macaw::BoundingBox, space_spects: &SpaceSpecs) -> OrbitEye {
let mut center = scene_bbox.center();
if !center.is_finite() {
center = Vec3::ZERO;
}
let mut radius = 2.0 * scene_bbox.half_size().length();
if !radius.is_finite() || radius == 0.0 {
radius = 1.0;
}
let look_up = if space_spects.up == Vec3::ZERO {
Vec3::Z
} else {
space_spects.up.normalize()
};
// Look along the cardinal directions:
let look_dir = vec3(1.0, 1.0, 1.0);
// Make sure the eye is looking down, but just slightly:
let look_dir = look_dir + look_up * (-0.5 - look_dir.dot(look_up));
let look_dir = look_dir.normalize();
let eye_pos = center - radius * look_dir;
OrbitEye {
orbit_center: center,
orbit_radius: radius,
world_from_view_rot: Quat::from_affine3(
&Affine3A::look_at_rh(eye_pos, center, look_up).inverse(),
),
fov_y: eye::DEFAULT_FOV_Y,
up: space_spects.up,
velocity: Vec3::ZERO,
}
}
/// We get a [`glow::Context`] from `eframe`, but we want a [`three_d::Context`].
///
/// Sadly we can't just create and store a [`three_d::Context`] in the app and pass it
/// to the [`egui::PaintCallback`] because [`three_d::Context`] isn't `Send+Sync`, which
/// [`egui::PaintCallback`] is.
fn with_three_d_context<R>(
gl: &std::sync::Arc<glow::Context>,
f: impl FnOnce(&mut RenderingContext) -> R,
) -> R {
use std::cell::RefCell;
thread_local! {
static THREE_D: RefCell<Option<RenderingContext>> = RefCell::new(None);
}
#[allow(unsafe_code)]
// SAFETY: we should have a valid glow context here, and we _should_ be in the correct thread.
unsafe {
use glow::HasContext as _;
gl.enable(glow::DEPTH_TEST);
if !cfg!(target_arch = "wasm32") {
gl.disable(glow::FRAMEBUFFER_SRGB);
}
gl.clear(glow::DEPTH_BUFFER_BIT);
}
THREE_D.with(|three_d| {
let mut three_d = three_d.borrow_mut();
let three_d = three_d.get_or_insert_with(|| RenderingContext::new(gl).unwrap());
f(three_d)
})
}