gloss_renderer/
viewer_dummy.rs

1cfg_if::cfg_if! {
2    if #[cfg(not(target_arch = "wasm32"))] {
3        use crate::config::Config;
4        use crate::logger::gloss_setup_logger_from_config;
5        use crate::plugin_manager::plugins::{Plugin, Plugins};
6        use crate::scene::Scene;
7        use crate::set_panic_hook;
8        use crate::{camera::Camera, scene::GLOSS_CAM_NAME};
9    }
10}
11
12use core::time::Duration;
13#[allow(unused_imports)]
14use log::{error, info, Level};
15
16#[cfg(target_arch = "wasm32")]
17#[allow(unused_imports)]
18use wasm_bindgen::prelude::*;
19use wasm_timer::Instant;
20
21#[derive(Debug)]
22#[repr(C)]
23pub struct RunnerDummy {
24    pub is_running: bool,
25    pub first_time: bool,
26    time_init: Instant, //time when the init of the viewer has finished
27}
28impl Default for RunnerDummy {
29    fn default() -> Self {
30        let time_init = Instant::now();
31        Self {
32            is_running: false,
33            first_time: true,
34            time_init,
35        }
36    }
37}
38#[allow(unused)]
39impl RunnerDummy {
40    pub fn time_since_init(&self) -> Duration {
41        if self.first_time {
42            Duration::ZERO
43        } else {
44            self.time_init.elapsed()
45        }
46    }
47}
48
49/// `ViewerDummy` is just a container for the Scene but cannot perform any rendering
50/// It's mainly used for some operations that require the existance of a scene, like exporting GLTFs
51/// In an ideal world `ViewerDummy` shouldn't exist
52#[cfg(not(target_arch = "wasm32"))] //wasm cannot compile the run_return() call so we just disable the whole
53                                    // dummy viewer
54pub struct ViewerDummy {
55    pub camera: Camera,
56    pub scene: Scene,
57    pub plugins: Plugins,
58    pub config: Config,
59    pub runner: RunnerDummy,
60}
61
62#[cfg(not(target_arch = "wasm32"))]
63impl ViewerDummy {
64    pub fn new(config_path: Option<&str>) -> Self {
65        let config = Config::new(config_path);
66        Self::new_with_config(&config)
67    }
68
69    #[allow(clippy::too_many_lines)]
70    #[allow(clippy::missing_panics_doc)]
71    pub fn new_with_config(config: &Config) -> Self {
72        set_panic_hook();
73        if config.core.auto_create_logger {
74            gloss_setup_logger_from_config(config);
75        }
76
77        //expensive but useful
78        re_memory::accounting_allocator::set_tracking_callstacks(config.core.enable_memory_profiling_callstacks);
79
80        let runner = RunnerDummy::default();
81
82        let mut scene = Scene::new();
83        let camera = Camera::new(GLOSS_CAM_NAME, &mut scene, false);
84
85        Self {
86            camera,
87            scene,
88            plugins: Plugins::new(),
89            config: config.clone(),
90            runner,
91        }
92    }
93
94    pub fn insert_plugin<T: Plugin + 'static>(&mut self, plugin: &T) {
95        self.plugins.insert_plugin(plugin);
96    }
97    #[allow(clippy::missing_panics_doc)]
98    pub fn run_manual_plugins(&mut self) {
99        self.plugins.run_logic_systems_dummy(&mut self.scene, &mut self.runner, false);
100    }
101
102    pub fn reset_for_first_time(&mut self) {
103        self.runner.first_time = true;
104    }
105}