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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
use egui::{emath::RectTransform, NumExt as _};
use glam::{Affine3A, Quat, Vec3};
use web_time::Instant;

use re_log_types::EntityPath;
use re_math::BoundingBox;
use re_renderer::{
    view_builder::{Projection, TargetConfiguration, ViewBuilder},
    LineDrawableBuilder, Size,
};
use re_space_view::controls::{
    RuntimeModifiers, DRAG_PAN3D_BUTTON, ROLL_MOUSE, ROLL_MOUSE_ALT, ROLL_MOUSE_MODIFIER,
    ROTATE3D_BUTTON, SPEED_UP_3D_MODIFIER, TRACKED_OBJECT_RESTORE_KEY,
};
use re_types::{
    blueprint::archetypes::Background, components::ViewCoordinates, view_coordinates::SignedAxis3,
};
use re_ui::{ContextExt, ModifiersMarkdown, MouseButtonMarkdown};
use re_viewer_context::{
    gpu_bridge, Item, ItemSpaceContext, SpaceViewSystemExecutionError, ViewQuery, ViewerContext,
};
use re_viewport_blueprint::ViewProperty;

use crate::{
    scene_bounding_boxes::SceneBoundingBoxes,
    space_camera_3d::SpaceCamera3D,
    ui::{create_labels, screenshot_context_menu, SpatialSpaceViewState},
    view_kind::SpatialSpaceViewKind,
    visualizers::{collect_ui_labels, image_view_coordinates, CamerasVisualizer},
    SpatialSpaceView3D,
};

use super::eye::{Eye, ViewEye};

// ---

#[derive(Clone)]
pub struct View3DState {
    pub view_eye: Option<ViewEye>,

    /// Used to show the orbit center of the eye-camera when the user interacts.
    /// None: user has never interacted with the eye-camera.
    pub last_eye_interaction: Option<Instant>,

    /// Currently tracked entity.
    ///
    /// If this is a camera, it takes over the camera pose, otherwise follows the entity.
    pub tracked_entity: Option<EntityPath>,

    /// Eye pose just before we started following an entity [`Self::tracked_entity`].
    camera_before_tracked_entity: Option<Eye>,

    eye_interpolation: Option<EyeInterpolation>,

    /// Last known view coordinates.
    /// Used to detect changes in view coordinates, in which case we reset the camera eye.
    scene_view_coordinates: Option<ViewCoordinates>,

    // options:
    spin: bool,
    pub show_axes: bool,
    pub show_bbox: bool,
    pub show_smoothed_bbox: bool,

    eye_interact_fade_in: bool,
    eye_interact_fade_change_time: f64,
}

impl Default for View3DState {
    fn default() -> Self {
        Self {
            view_eye: Default::default(),
            last_eye_interaction: None,
            tracked_entity: None,
            camera_before_tracked_entity: None,
            eye_interpolation: Default::default(),
            scene_view_coordinates: None,
            spin: false,
            show_axes: false,
            show_bbox: false,
            show_smoothed_bbox: false,
            eye_interact_fade_in: false,
            eye_interact_fade_change_time: f64::NEG_INFINITY,
        }
    }
}

fn ease_out(t: f32) -> f32 {
    1. - (1. - t) * (1. - t)
}

impl View3DState {
    pub fn reset_camera(
        &mut self,
        scene_bbox: &SceneBoundingBoxes,
        scene_view_coordinates: Option<ViewCoordinates>,
    ) {
        // Mark as interaction since we want to stop doing any automatic interpolations,
        // even if this is caused by a full reset.
        self.last_eye_interaction = Some(Instant::now());
        self.interpolate_to_view_eye(default_eye(&scene_bbox.current, scene_view_coordinates));
        self.tracked_entity = None;
        self.camera_before_tracked_entity = None;
    }

    fn update_eye(
        &mut self,
        response: &egui::Response,
        bounding_boxes: &SceneBoundingBoxes,
        space_cameras: &[SpaceCamera3D],
        scene_view_coordinates: Option<ViewCoordinates>,
    ) -> ViewEye {
        // If the user has not interacted with the eye-camera yet, continue to
        // interpolate to the new default eye. This gives much better robustness
        // with scenes that change over time.
        if self.last_eye_interaction.is_none() {
            self.interpolate_to_view_eye(default_eye(
                &bounding_boxes.current,
                scene_view_coordinates,
            ));
        }

        // Detect live changes to view coordinates, and interpolate to the new up axis as needed.
        if scene_view_coordinates != self.scene_view_coordinates {
            self.interpolate_to_view_eye(default_eye(
                &bounding_boxes.current,
                scene_view_coordinates,
            ));
        }
        self.scene_view_coordinates = scene_view_coordinates;

        // Follow tracked object.
        if let Some(tracked_entity) = self.tracked_entity.clone() {
            if let Some(target_eye) = find_camera(space_cameras, &tracked_entity) {
                // For cameras, we want to exactly track the camera pose once we're done interpolating.
                if let Some(eye_interpolation) = &mut self.eye_interpolation {
                    eye_interpolation.target_view_eye = None;
                    eye_interpolation.target_eye = Some(target_eye);
                } else if let Some(view_eye) = &mut self.view_eye {
                    view_eye.copy_from_eye(&target_eye);
                }
            } else {
                // For other entities we keep interpolating, so when the entity jumps, we follow smoothly.
                self.interpolate_eye_to_entity(&tracked_entity, bounding_boxes, space_cameras);
            }
        }

        let view_eye = self
            .view_eye
            .get_or_insert_with(|| default_eye(&bounding_boxes.current, scene_view_coordinates));

        if self.spin {
            view_eye.rotate(egui::vec2(
                -response.ctx.input(|i| i.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(|i| i.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 = ease_out(t);

            if let Some(target_orbit) = &cam_interpolation.target_view_eye {
                *view_eye = cam_interpolation.start.lerp(target_orbit, t);
            } else if let Some(target_eye) = &cam_interpolation.target_eye {
                let camera = cam_interpolation.start.to_eye().lerp(target_eye, t);
                view_eye.copy_from_eye(&camera);
            } else {
                self.eye_interpolation = None;
            }

            if t < 1.0 {
                // There's more frames to render to finish interpolation.
                response.ctx.request_repaint();
            } else {
                // We have arrived at our target
                self.eye_interpolation = None;
            }
        }

        // If we're tracking a camera right now, we want to make it slightly sticky,
        // so that a click on some entity doesn't immediately break the tracked state.
        // (Threshold is in amount of ui points the mouse was moved.)
        let view_eye_drag_threshold = if self.tracked_entity.is_some() {
            4.0
        } else {
            0.0
        };

        if view_eye.update(response, view_eye_drag_threshold, bounding_boxes) {
            self.last_eye_interaction = Some(Instant::now());
            self.eye_interpolation = None;
            self.tracked_entity = None;
            self.camera_before_tracked_entity = None;
        }

        *view_eye
    }

    fn interpolate_to_eye(&mut self, target: Eye) {
        if let Some(start) = self.view_eye.as_mut() {
            // the user wants to move the camera somewhere, so stop spinning
            self.spin = false;

            if let Some(target_time) = EyeInterpolation::target_time(&start.to_eye(), &target) {
                self.eye_interpolation = Some(EyeInterpolation {
                    elapsed_time: 0.0,
                    target_time,
                    start: *start,
                    target_view_eye: None,
                    target_eye: Some(target),
                });
            } else {
                start.copy_from_eye(&target);
            }
        } else {
            // shouldn't really happen (`self.view_eye` is only `None` for the first frame).
        }
    }

    fn interpolate_eye_to_entity(
        &mut self,
        entity_path: &EntityPath,
        bounding_boxes: &SceneBoundingBoxes,
        space_cameras: &[SpaceCamera3D],
    ) {
        // Note that we may want to focus on an _instance_ instead in the future:
        // The problem with that is that there may be **many** instances (think point cloud)
        // and they may not be consistent over time.
        // -> we don't know the bounding box of every instance (right now)
        // -> tracking instances over time may not be desired
        //    (this can happen with entities as well, but is less likely).
        //
        // For future reference, it's also worth pointing out that for interactions in the view we
        // already have the 3D position:
        // if let Some(SelectedSpaceContext::ThreeD {
        //     pos: Some(clicked_point),
        //     ..
        // }) = ctx.selection_state().hovered_space_context()

        if let Some(tracked_camera) = find_camera(space_cameras, entity_path) {
            self.interpolate_to_eye(tracked_camera);
        } else if let Some(entity_bbox) = bounding_boxes.per_entity.get(&entity_path.hash()) {
            let Some(mut new_view_eye) = self.view_eye else {
                // Happens only the first frame when there's no eye set up yet.
                return;
            };

            let radius = entity_bbox.centered_bounding_sphere_radius() * 1.5;
            let orbit_radius = if radius < 0.0001 {
                // Handle zero-sized bounding boxes:
                (bounding_boxes.current.centered_bounding_sphere_radius() * 1.5).at_least(0.01)
            } else {
                radius
            };

            new_view_eye.set_orbit_center_and_radius(entity_bbox.center(), orbit_radius);

            self.interpolate_to_view_eye(new_view_eye);
        }
    }

    /// The taregt mode will be ignored, and the mode of the current eye will be kept unchanged.
    fn interpolate_to_view_eye(&mut self, mut target: ViewEye) {
        if let Some(view_eye) = &self.view_eye {
            target.set_mode(view_eye.mode());
        }

        // the user wants to move the camera somewhere, so stop spinning
        self.spin = false;

        if self.view_eye == Some(target) {
            return; // We're already there.
        }

        // Don't restart interpolation if we're already on it.
        if let Some(eye_interpolation) = &self.eye_interpolation {
            if eye_interpolation.target_view_eye == Some(target) {
                return;
            }
        }

        if let Some(start) = self.view_eye {
            if let Some(target_time) =
                EyeInterpolation::target_time(&start.to_eye(), &target.to_eye())
            {
                self.eye_interpolation = Some(EyeInterpolation {
                    elapsed_time: 0.0,
                    target_time,
                    start,
                    target_view_eye: Some(target),
                    target_eye: None,
                });
            } else {
                self.view_eye = Some(target);
            }
        } else {
            self.view_eye = Some(target);
        }
    }

    fn track_entity(
        &mut self,
        entity_path: &EntityPath,
        bounding_boxes: &SceneBoundingBoxes,
        space_cameras: &[SpaceCamera3D],
    ) {
        if self.tracked_entity == Some(entity_path.clone()) {
            return; // already tracking this entity.
        }

        re_log::debug!("3D view tracks now {:?}", entity_path);
        self.tracked_entity = Some(entity_path.clone());
        self.camera_before_tracked_entity = self.view_eye.map(|eye| eye.to_eye());

        self.interpolate_eye_to_entity(entity_path, bounding_boxes, space_cameras);
    }

    pub fn spin(&self) -> bool {
        self.spin
    }

    pub fn set_spin(&mut self, spin: bool) {
        if spin != self.spin {
            self.spin = spin;
            self.last_eye_interaction = Some(Instant::now());
        }
    }
}

#[derive(Clone, PartialEq)]
struct EyeInterpolation {
    elapsed_time: f32,
    target_time: f32,
    start: ViewEye,
    target_view_eye: Option<ViewEye>,
    target_eye: Option<Eye>,
}

impl EyeInterpolation {
    pub fn target_time(start: &Eye, stop: &Eye) -> Option<f32> {
        // Take more time if the rotation is big:
        let angle_difference = start
            .world_from_rub_view
            .rotation()
            .angle_between(stop.world_from_rub_view.rotation());

        // Threshold to avoid doing pointless interpolations that trigger frame requests.
        let distance = start.pos_in_world().distance(stop.pos_in_world());
        if angle_difference < 0.01 && distance < 0.0001 {
            None
        } else {
            Some(egui::remap_clamp(
                angle_difference,
                0.0..=std::f32::consts::PI,
                0.2..=0.7,
            ))
        }
    }
}

fn find_camera(space_cameras: &[SpaceCamera3D], needle: &EntityPath) -> Option<Eye> {
    let mut found_camera = None;

    for camera in space_cameras {
        if &camera.ent_path == needle {
            if found_camera.is_some() {
                return None; // More than one camera
            } else {
                found_camera = Some(camera);
            }
        }
    }

    found_camera.and_then(Eye::from_camera)
}

// ----------------------------------------------------------------------------

pub fn help_markdown(egui_ctx: &egui::Context) -> String {
    // TODO(#6876): this line was removed to from the help text, because the corresponding feature no
    // longer works. To be restored when it works again (or deleted forever).
    /* - Reset the view again with {TRACKED_OBJECT_RESTORE_KEY}.*/

    format!(
        "# 3D view

Display 3D content in the reference frame defined by the space origin.

## Navigation controls

- Click and drag the {rotate3d_button} to rotate.
- Click and drag with the {drag_pan3d_button} to pan.
- Drag with the {roll_mouse} (or the {roll_mouse_alt} + holding {roll_mouse_modifier}) to roll the view.
- Scroll or pinch to zoom.
- While hovering the 3D view, navigate with the `WASD` and `QE` keys.
- {slow_down} slows down, {speed_up_3d_modifier} speeds up.
- Double-click an object to focus the view on it.
- Double-click on an empty space to reset the view.",
        rotate3d_button = MouseButtonMarkdown(ROTATE3D_BUTTON),
        drag_pan3d_button = MouseButtonMarkdown(DRAG_PAN3D_BUTTON),
        roll_mouse = MouseButtonMarkdown(ROLL_MOUSE),
        roll_mouse_alt = MouseButtonMarkdown(ROLL_MOUSE_ALT),
        roll_mouse_modifier = ModifiersMarkdown(ROLL_MOUSE_MODIFIER, egui_ctx),
        slow_down = ModifiersMarkdown(RuntimeModifiers::slow_down(&egui_ctx.os()), egui_ctx),
        speed_up_3d_modifier = ModifiersMarkdown(SPEED_UP_3D_MODIFIER, egui_ctx),
        // TODO(#6876): see above
        /*TRACKED_OBJECT_RESTORE_KEY = KeyMarkdown(TRACKED_OBJECT_RESTORE_KEY),*/
    )
}

impl SpatialSpaceView3D {
    pub fn view_3d(
        &self,
        ctx: &ViewerContext<'_>,
        ui: &mut egui::Ui,
        state: &mut SpatialSpaceViewState,
        query: &ViewQuery<'_>,
        system_output: re_viewer_context::SystemExecutionOutput,
    ) -> Result<(), SpaceViewSystemExecutionError> {
        re_tracing::profile_function!();

        let highlights = &query.highlights;
        let space_cameras = &system_output
            .view_systems
            .get::<CamerasVisualizer>()?
            .space_cameras;
        let scene_view_coordinates = ctx
            .recording()
            // Allow logging view-coordinates to `/` and have it apply to `/world` etc.
            // See https://github.com/rerun-io/rerun/issues/3538
            .latest_at_component_at_closest_ancestor(query.space_origin, &ctx.current_query())
            .map(|(_, _index, c)| c);

        let (ui_rect, mut response) =
            ui.allocate_at_least(ui.available_size(), egui::Sense::click_and_drag());

        if !ui_rect.is_positive() {
            return Ok(()); // protect against problems with zero-sized views
        }

        let view_eye = state.state_3d.update_eye(
            &response,
            &state.bounding_boxes,
            space_cameras,
            scene_view_coordinates,
        );
        let eye = view_eye.to_eye();

        // Determine view port resolution and position.
        let resolution_in_pixel =
            gpu_bridge::viewport_resolution_in_pixels(ui_rect, ui.ctx().pixels_per_point());
        if resolution_in_pixel[0] == 0 || resolution_in_pixel[1] == 0 {
            return Ok(());
        }

        let target_config = TargetConfiguration {
            name: query.space_origin.to_string().into(),

            resolution_in_pixel,

            view_from_world: eye.world_from_rub_view.inverse(),
            projection_from_view: Projection::Perspective {
                vertical_fov: eye.fov_y.unwrap_or(Eye::DEFAULT_FOV_Y),
                near_plane_distance: eye.near(),
                aspect_ratio: resolution_in_pixel[0] as f32 / resolution_in_pixel[1] as f32,
            },
            viewport_transformation: re_renderer::RectTransform::IDENTITY,

            pixels_per_point: ui.ctx().pixels_per_point(),

            outline_config: query
                .highlights
                .any_outlines()
                .then(|| re_space_view::outline_config(ui.ctx())),
            blend_with_background: false,
        };

        let Some(render_ctx) = ctx.render_ctx else {
            return Err(SpaceViewSystemExecutionError::NoRenderContextError);
        };

        // Various ui interactions draw additional lines.
        let mut line_builder = LineDrawableBuilder::new(render_ctx);
        line_builder.radius_boost_in_ui_points_for_outlines(
            re_space_view::SIZE_BOOST_IN_POINTS_FOR_LINE_OUTLINES,
        );
        // We don't know ahead of time how many lines we need, but it's not gonna be a huge amount!
        line_builder.reserve_strips(32)?;
        line_builder.reserve_vertices(64)?;

        // Origin gizmo if requested.
        // TODO(andreas): Move this to the transform3d_arrow scene part.
        //              As of #2522 state is now longer accessible there, move the property to a context?
        if state.state_3d.show_axes {
            let axis_length = 1.0; // The axes are also a measuring stick
            crate::visualizers::add_axis_arrows(
                &mut line_builder,
                glam::Affine3A::IDENTITY,
                None,
                axis_length,
                re_renderer::OutlineMaskPreference::NONE,
            );

            // If we are showing the axes for the space, then add the space origin to the bounding box.
            state.bounding_boxes.current.extend(glam::Vec3::ZERO);
        }

        let mut view_builder = ViewBuilder::new(render_ctx, target_config);

        // Create labels now since their shapes participate are added to scene.ui for picking.
        let (label_shapes, ui_rects) = create_labels(
            collect_ui_labels(&system_output.view_systems),
            RectTransform::from_to(ui_rect, ui_rect),
            &eye,
            ui,
            highlights,
            SpatialSpaceViewKind::ThreeD,
        );

        if let Some(pointer_pos_ui) = response.hover_pos() {
            // There's no panning & zooming, so this is an identity transform.
            let ui_pan_and_zoom_from_ui = RectTransform::from_to(ui_rect, ui_rect);

            let picking_context = crate::picking::PickingContext::new(
                pointer_pos_ui,
                ui_pan_and_zoom_from_ui,
                ui.ctx().pixels_per_point(),
                &eye,
            );
            response = crate::picking_ui::picking(
                ctx,
                &picking_context,
                ui,
                response,
                &mut view_builder,
                state,
                &system_output,
                &ui_rects,
                query,
                SpatialSpaceViewKind::ThreeD,
            )?;
        } else {
            state.previous_picking_result = None;
        }

        // Track focused entity if any.
        if let Some(focused_item) = ctx.focused_item {
            let focused_entity = match focused_item {
                Item::AppId(_) | Item::DataSource(_) | Item::StoreId(_) | Item::Container(_) => {
                    None
                }

                Item::SpaceView(space_view_id) => {
                    if space_view_id == &query.space_view_id {
                        state
                            .state_3d
                            .reset_camera(&state.bounding_boxes, scene_view_coordinates);
                    }
                    None
                }

                Item::ComponentPath(component_path) => Some(&component_path.entity_path),

                Item::InstancePath(instance_path) => Some(&instance_path.entity_path),

                Item::DataResult(space_view_id, instance_path) => {
                    if *space_view_id == query.space_view_id {
                        Some(&instance_path.entity_path)
                    } else {
                        None
                    }
                }
            };
            if let Some(entity_path) = focused_entity {
                state.state_3d.last_eye_interaction = Some(Instant::now());

                // TODO(#4812): We currently only track cameras on double click since tracking arbitrary entities was deemed too surprising.
                if find_camera(space_cameras, entity_path).is_some() {
                    state
                        .state_3d
                        .track_entity(entity_path, &state.bounding_boxes, space_cameras);
                } else {
                    state.state_3d.interpolate_eye_to_entity(
                        entity_path,
                        &state.bounding_boxes,
                        space_cameras,
                    );
                }
            }

            // Make sure focus consequences happen in the next frames.
            ui.ctx().request_repaint();
        }

        // Allow to restore the camera state with escape if a camera was tracked before.
        if response.hovered() && ui.input(|i| i.key_pressed(TRACKED_OBJECT_RESTORE_KEY)) {
            if let Some(camera_before_tracked_entity) = state.state_3d.camera_before_tracked_entity
            {
                state
                    .state_3d
                    .interpolate_to_eye(camera_before_tracked_entity);
                state.state_3d.camera_before_tracked_entity = None;
                state.state_3d.tracked_entity = None;
            }
        }

        // Screenshot context menu.
        if let Some(mode) = screenshot_context_menu(ctx, &response) {
            view_builder
                .schedule_screenshot(render_ctx, query.space_view_id.gpu_readback_id(), mode)
                .ok();
        }

        for selected_context in ctx.selection_state().selection_space_contexts() {
            show_projections_from_2d_space(
                &mut line_builder,
                space_cameras,
                state,
                selected_context,
                ui.ctx().selection_stroke().color,
            );
        }
        if let Some(hovered_context) = ctx.selection_state().hovered_space_context() {
            show_projections_from_2d_space(
                &mut line_builder,
                space_cameras,
                state,
                hovered_context,
                ui.ctx().hover_stroke().color,
            );
        }

        // TODO(andreas): Make configurable. Could pick up default radius for this view?
        let box_line_radius = Size(*re_types::components::Radius::default().0);

        if state.state_3d.show_bbox {
            line_builder
                .batch("scene_bbox_current")
                .add_box_outline(&state.bounding_boxes.current)
                .map(|lines| lines.radius(box_line_radius).color(egui::Color32::WHITE));
        }
        if state.state_3d.show_smoothed_bbox {
            line_builder
                .batch("scene_bbox_smoothed")
                .add_box_outline(&state.bounding_boxes.smoothed)
                .map(|lines| {
                    lines
                        .radius(box_line_radius)
                        .color(egui::Color32::from_gray(170))
                });
        }

        show_orbit_eye_center(
            ui.ctx(),
            &mut state.state_3d,
            &mut line_builder,
            &view_eye,
            scene_view_coordinates,
        );

        for draw_data in system_output.draw_data {
            view_builder.queue_draw(draw_data);
        }

        // Commit ui induced lines.
        view_builder.queue_draw(line_builder.into_draw_data()?);

        let background = ViewProperty::from_archetype::<Background>(
            ctx.blueprint_db(),
            ctx.blueprint_query,
            query.space_view_id,
        );
        let (background_drawable, clear_color) =
            crate::configure_background(ctx, &background, render_ctx, self, state)?;
        if let Some(background_drawable) = background_drawable {
            view_builder.queue_draw(background_drawable);
        }

        ui.painter().add(gpu_bridge::new_renderer_callback(
            view_builder,
            ui_rect,
            clear_color,
        ));

        // Add egui-rendered spinners/loaders on top of re_renderer content:
        crate::ui::paint_loading_spinners(
            ui,
            RectTransform::from_to(ui_rect, ui_rect),
            &eye,
            &system_output.view_systems,
        );

        // Add egui-rendered labels on top of everything else:
        let painter = ui.painter().with_clip_rect(ui.max_rect());
        painter.extend(label_shapes);

        Ok(())
    }
}

/// Show center of orbit camera when interacting with camera (it's quite helpful).
fn show_orbit_eye_center(
    egui_ctx: &egui::Context,
    state_3d: &mut View3DState,
    line_builder: &mut LineDrawableBuilder<'_>,
    view_eye: &ViewEye,
    scene_view_coordinates: Option<ViewCoordinates>,
) {
    let Some(orbit_center) = view_eye.orbit_center() else {
        return;
    };
    let Some(orbit_radius) = view_eye.orbit_radius() else {
        return;
    };

    const FADE_DURATION: f32 = 0.1;

    let ui_time = egui_ctx.input(|i| i.time);
    let any_mouse_button_down = egui_ctx.input(|i| i.pointer.any_down());

    let should_show_center_of_orbit_camera = state_3d
        .last_eye_interaction
        .map_or(false, |d| d.elapsed().as_secs_f32() < 0.35);

    if !state_3d.eye_interact_fade_in && should_show_center_of_orbit_camera {
        // Any interaction immediately causes fade in to start if it's not already on.
        state_3d.eye_interact_fade_change_time = ui_time;
        state_3d.eye_interact_fade_in = true;
    }
    if state_3d.eye_interact_fade_in
            && !should_show_center_of_orbit_camera
            // Don't start fade-out while dragging, even if mouse is still
            && !any_mouse_button_down
    {
        state_3d.eye_interact_fade_change_time = ui_time;
        state_3d.eye_interact_fade_in = false;
    }

    pub fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
        let t = f32::clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
        t * t * (3.0 - t * 2.0)
    }

    // Compute smooth fade.
    let time_since_fade_change = (ui_time - state_3d.eye_interact_fade_change_time) as f32;
    let orbit_center_fade = if state_3d.eye_interact_fade_in {
        // Fade in.
        smoothstep(0.0, FADE_DURATION, time_since_fade_change)
    } else {
        // Fade out.
        smoothstep(FADE_DURATION, 0.0, time_since_fade_change)
    };

    if orbit_center_fade > 0.001 {
        let half_line_length = orbit_radius * 0.03;
        let half_line_length = half_line_length * orbit_center_fade;

        // We distinguish the eye up-axis from the other two axes:
        // Default to RFU
        let up = view_eye.eye_up().unwrap_or(glam::Vec3::Z);

        // For the other two axes, try to use the scene view coordinates if available:
        let right = scene_view_coordinates
            .and_then(|vc| vc.right())
            .map_or(glam::Vec3::X, Vec3::from);
        let forward = up
            .cross(right)
            .try_normalize()
            .unwrap_or_else(|| up.any_orthogonal_vector());
        let right = forward.cross(up);

        line_builder
            .batch("center orbit orientation help")
            .add_segments(
                [
                    (orbit_center, orbit_center + 0.5 * up * half_line_length),
                    (
                        orbit_center - right * half_line_length,
                        orbit_center + right * half_line_length,
                    ),
                    (
                        orbit_center - forward * half_line_length,
                        orbit_center + forward * half_line_length,
                    ),
                ]
                .into_iter(),
            )
            .radius(Size::new_ui_points(0.75))
            // TODO(andreas): Fade this out.
            .color(re_renderer::Color32::WHITE);

        // TODO(andreas): Idea for nice depth perception:
        // Render the lines once with additive blending and depth test enabled
        // and another time without depth test. In both cases it needs to be rendered last,
        // something re_renderer doesn't support yet for primitives within renderers.

        egui_ctx.request_repaint(); // show it for a bit longer.
    }
}

fn show_projections_from_2d_space(
    line_builder: &mut re_renderer::LineDrawableBuilder<'_>,
    space_cameras: &[SpaceCamera3D],
    state: &SpatialSpaceViewState,
    space_context: &ItemSpaceContext,
    ray_color: egui::Color32,
) {
    match space_context {
        ItemSpaceContext::TwoD { space_2d, pos } => {
            if let Some(cam) = space_cameras.iter().find(|cam| &cam.ent_path == space_2d) {
                if let Some(pinhole) = cam.pinhole.as_ref() {
                    // Render a thick line to the actual z value if any and a weaker one as an extension
                    // If we don't have a z value, we only render the thick one.
                    let depth = if 0.0 < pos.z && pos.z.is_finite() {
                        pos.z
                    } else {
                        cam.picture_plane_distance
                    };
                    let stop_in_image_plane = pinhole.unproject(glam::vec3(pos.x, pos.y, depth));

                    let world_from_image = glam::Affine3A::from(cam.world_from_camera)
                        * glam::Affine3A::from_mat3(
                            cam.pinhole_view_coordinates
                                .from_other(&image_view_coordinates()),
                        );
                    let stop_in_world = world_from_image.transform_point3(stop_in_image_plane);

                    let origin = cam.position();

                    if let Some(dir) = (stop_in_world - origin).try_normalize() {
                        let ray = re_math::Ray3::from_origin_dir(origin, dir);

                        let thick_ray_length = (stop_in_world - origin).length();
                        add_picking_ray(
                            line_builder,
                            ray,
                            &state.bounding_boxes.smoothed,
                            thick_ray_length,
                            ray_color,
                        );
                    }
                }
            }
        }
        ItemSpaceContext::ThreeD {
            pos: Some(pos),
            tracked_entity: Some(tracked_entity),
            ..
        } => {
            let current_tracked_entity = state.state_3d.tracked_entity.as_ref();
            if current_tracked_entity.map_or(true, |tracked| tracked != tracked_entity) {
                if let Some(tracked_camera) = space_cameras
                    .iter()
                    .find(|cam| &cam.ent_path == tracked_entity)
                {
                    let cam_to_pos = *pos - tracked_camera.position();
                    let distance = cam_to_pos.length();
                    let ray = re_math::Ray3::from_origin_dir(
                        tracked_camera.position(),
                        cam_to_pos / distance,
                    );
                    add_picking_ray(
                        line_builder,
                        ray,
                        &state.bounding_boxes.current,
                        distance,
                        ray_color,
                    );
                }
            }
        }
        ItemSpaceContext::ThreeD { .. } => {}
    }
}

fn add_picking_ray(
    line_builder: &mut re_renderer::LineDrawableBuilder<'_>,
    ray: re_math::Ray3,
    scene_bbox: &BoundingBox,
    thick_ray_length: f32,
    ray_color: egui::Color32,
) {
    let mut line_batch = line_builder.batch("picking ray");

    let origin = ray.point_along(0.0);

    // No harm in making this ray _very_ long. (Infinite messes with things though!)
    //
    // There are some degenerated cases where just taking the scene bounding box isn't enough:
    // For instance, we don't add pinholes & depth images to the bounding box since
    // the default size of a pinhole visualization itself is determined by the bounding box.
    let fallback_ray_end =
        ray.point_along((scene_bbox.size().length() * 10.0).at_least(thick_ray_length * 10.0));
    let main_ray_end = ray.point_along(thick_ray_length);

    line_batch
        .add_segment(origin, main_ray_end)
        .color(ray_color)
        .radius(Size::new_ui_points(1.0));
    line_batch
        .add_segment(main_ray_end, fallback_ray_end)
        .color(ray_color.gamma_multiply(0.7))
        // TODO(andreas): Make this dashed.
        .radius(Size::new_ui_points(0.5));
}

fn default_eye(
    bounding_box: &re_math::BoundingBox,
    scene_view_coordinates: Option<ViewCoordinates>,
) -> ViewEye {
    // Defaults to RFU.
    let scene_view_coordinates = scene_view_coordinates.unwrap_or_default();
    let scene_right = scene_view_coordinates
        .right()
        .unwrap_or(SignedAxis3::POSITIVE_X);
    let scene_forward = scene_view_coordinates
        .forward()
        .unwrap_or(SignedAxis3::POSITIVE_Y);
    let scene_up = scene_view_coordinates
        .up()
        .unwrap_or(SignedAxis3::POSITIVE_Z);

    let mut center = bounding_box.center();
    if !center.is_finite() {
        center = Vec3::ZERO;
    }

    let mut radius = 1.5 * bounding_box.half_size().length();
    if !radius.is_finite() || radius == 0.0 {
        radius = 1.0;
    }

    let eye_up: glam::Vec3 = scene_up.into();

    let eye_dir = {
        // Make sure right is to the right, and up is up:
        let right = scene_right.into();
        let fwd = eye_up.cross(right);
        0.75 * fwd + 0.25 * right - 0.25 * eye_up
    };
    let eye_dir = eye_dir.try_normalize().unwrap_or(scene_forward.into());

    let eye_pos = center - radius * eye_dir;

    ViewEye::new_orbital(
        center,
        radius,
        Quat::from_affine3(&Affine3A::look_at_rh(eye_pos, center, eye_up).inverse()),
        eye_up,
    )
}