euv-example 0.49.1

Cross-platform native showcase application demonstrating euv fine-grained reactivity, multi-mode rendering, window controls, and euv-ui components.
Documentation
use super::*;

/// Mounts the 3D Open-World RT Engine studio with a wide BVH viewport beside the graphics quality, lighting, and live model-import inspector rail.
///
/// # Arguments
///
/// - `&ShowcaseState` - Reactive showcase state.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted 3D engine showcase page handle.
pub fn mount_game3d_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
    let host: GameEngineHost = state.get_game3d_host().clone();
    let quality_sig: Signal<RenderQualityConfig> = state.get_engine3d_quality();
    let preset_idx: Signal<usize> = state.get_engine3d_preset_idx();
    let rt_idx: Signal<usize> = state.get_engine3d_rt_idx();
    let res_scale: Signal<f32> = state.get_engine3d_res_scale();
    let time_sig: Signal<f32> = state.get_engine3d_time_of_day();
    let import_sig: Signal<u64> = state.get_engine3d_import_queue();
    let status_sig: Signal<String> = state.get_engine3d_import_status();
    let cam_mode_sig: Signal<u8> = state.get_engine3d_camera_mode();
    let tsr_idx: Signal<usize> = state.get_engine3d_tsr_idx();
    let atmo_idx: Signal<usize> = state.get_engine3d_atmosphere_idx();
    let viewport: NodeId = host.mount_viewport(style! {
        Width(Length::Percent(100.0))
        Height(Length::Percent(100.0))
    });
    let vp_sig: Signal<Size> = use_viewport_size();
    let vp_frame_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let height_val: f32 = adaptive_viewport_height(vp_sig.get().get_height());
        style! {
            FlexDirection::Column
            Width(Length::Percent(100.0))
            Height(Length::Px(height_val))
            Overflow::Hidden
            Border::new(live.get_geometry().get_border_width(), live.get_palette().get_border())
            Radius(live.get_geometry().get_radius_md())
        }
    };
    let viewport_frame: NodeId = view! {
        column {
            bind_style: vp_frame_style
            { viewport }
        }
    };
    let fs_3d_open: Signal<bool> = Signal::new(false);
    let fs_3d_portal: NodeId = mount_game_fullscreen_portal(
        fs_3d_open,
        &host,
        viewport,
        Some(cam_mode_sig),
        GAME3D_CARD_TITLE,
        tokens,
    );
    let res_slider: NodeId = view! {
        EuvSlider {
            value: res_scale
            min: 40.0
            max: 125.0
            tokens: tokens
        }
    };
    let sun_slider: NodeId = view! {
        EuvSlider {
            value: time_sig
            min: 6.0
            max: 23.0
            tokens: tokens
        }
    };
    let ctx: Context = current_context();
    let (sync_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        Effect::new(move || {
            let p_idx: usize = preset_idx.get();
            let preset: QualityPreset = match p_idx {
                0 => QualityPreset::Low,
                1 => QualityPreset::Medium,
                3 => QualityPreset::Ultra,
                4 => QualityPreset::Cinematic,
                _ => QualityPreset::High,
            };
            let runtime_ref: RayTracingMode = match rt_idx.get() {
                0 => RayTracingMode::Off,
                1 => RayTracingMode::ShadowsOnly,
                3 => RayTracingMode::FullPathTracedGi,
                _ => RayTracingMode::ShadowsAndReflections,
            };
            let scale_pct: f32 = res_scale.get();
            let t_idx: usize = tsr_idx.get();
            let mut config_spec: RenderQualityConfig = RenderQualityConfig::from_preset(preset);
            config_spec.set_ray_tracing(runtime_ref);
            match t_idx {
                1 => {
                    config_spec.set_anti_aliasing(AntiAliasingMode::Tsr);
                    config_spec.set_resolution_scale(0.67);
                }
                2 => {
                    config_spec.set_anti_aliasing(AntiAliasingMode::Tsr);
                    config_spec.set_resolution_scale(0.50);
                }
                _ => {
                    config_spec.set_resolution_scale((scale_pct / 100.0).clamp(0.40, 1.25));
                }
            }
            let _: bool = quality_sig.set_if_changed(config_spec);
        })
    });
    let on_switch_cam = move |_: &UiEvent| {
        let next: u8 = (cam_mode_sig.get_untracked() + 1) % 4;
        let _: bool = cam_mode_sig.set_if_changed(next);
    };
    let on_open_fs = move |_: &UiEvent| {
        let _: bool = fs_3d_open.set_if_changed(true);
    };
    let on_import_obj = move |_: &UiEvent| {
        let cur: u64 = import_sig.get_untracked();
        let next: u64 = if cur.is_multiple_of(2) {
            cur + 1
        } else {
            cur + 2
        };
        let _: bool = import_sig.set_if_changed(next);
    };
    let on_import_gltf = move |_: &UiEvent| {
        let cur: u64 = import_sig.get_untracked();
        let next: u64 = if !cur.is_multiple_of(2) {
            cur + 1
        } else {
            cur + 2
        };
        let _: bool = import_sig.set_if_changed(next);
    };
    let status_text = move || -> String { status_sig.get() };
    let status_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        style! {
            Width(Length::Percent(100.0))
            FontSize(12.5)
            FontFamily::Monospace
            TextColor(live.get_palette().get_muted_foreground())
        }
    };
    let ctrl_left: NodeId =
        mount_game_control_button(BTN_GAME3D_LEFT, &host, KeyCode::ArrowLeft, tokens);
    let ctrl_fwd: NodeId =
        mount_game_control_button(BTN_GAME3D_FWD, &host, KeyCode::ArrowUp, tokens);
    let ctrl_rev: NodeId =
        mount_game_control_button(BTN_GAME3D_REV, &host, KeyCode::ArrowDown, tokens);
    let ctrl_right: NodeId =
        mount_game_control_button(BTN_GAME3D_RIGHT, &host, KeyCode::ArrowRight, tokens);
    let mut sync_host_id: NodeId = NodeId::default();
    let column_secondary_style: Style = style! {
        Width(Length::Percent(100.0))
        Gap(SPACE_MD)
    };
    let main_card: NodeId = view! {
        EuvCard {
            title: GAME3D_CARD_TITLE.to_string()
            tokens: tokens
            subtitle: GAME3D_CARD_SUB
            column {
                node_ref: sync_host_id
                style: column_secondary_style
                { viewport_frame }
                EuvActionRow {
                    { ctrl_left }
                    { ctrl_fwd }
                    { ctrl_rev }
                    { ctrl_right }
                    EuvButton {
                        label: BTN_GAME3D_CAMERA.to_string()
                        tokens: tokens
                        outline()
                        small()
                        on_click: on_switch_cam
                    }
                    EuvButton {
                        label: BTN_GAME3D_FULLSCREEN.to_string()
                        tokens: tokens
                        small()
                        on_click: on_open_fs
                    }
                }
                text {
                    bind_style: status_style
                    { status_text }
                }
            }
        }
    };
    let graphics_card: NodeId = view! {
        EuvCard {
            title: GAME3D_GRAPHICS_TITLE.to_string()
            tokens: tokens
            subtitle: GAME3D_GRAPHICS_SUB
            EuvSegmented {
                options: vec![
                    GAME3D_PRESET_LOW.to_string(),
                    GAME3D_PRESET_MED.to_string(),
                    GAME3D_PRESET_HIGH.to_string(),
                    GAME3D_PRESET_ULTRA.to_string(),
                    GAME3D_PRESET_CINEMA.to_string(),
                ]
                selected: preset_idx
                tokens: tokens
            }
            EuvSegmented {
                options: vec![
                    GAME3D_RT_OFF.to_string(),
                    GAME3D_RT_SHADOWS.to_string(),
                    GAME3D_RT_REFL.to_string(),
                    GAME3D_RT_FULL_GI.to_string(),
                ]
                selected: rt_idx
                tokens: tokens
            }
            EuvSegmented {
                options: vec![
                    GAME3D_TSR_NATIVE.to_string(),
                    GAME3D_TSR_QUALITY.to_string(),
                    GAME3D_TSR_PERF.to_string(),
                ]
                selected: tsr_idx
                tokens: tokens
            }
            EuvSegmented {
                options: vec![
                    GAME3D_ATMO_SUNSET.to_string(),
                    GAME3D_ATMO_RAIN.to_string(),
                    GAME3D_ATMO_NOON.to_string(),
                    GAME3D_ATMO_MIDNIGHT.to_string(),
                ]
                selected: atmo_idx
                tokens: tokens
            }
            EuvField {
                label: GAME3D_FIELD_RES.to_string()
                control: res_slider
                tokens: tokens
            }
            EuvField {
                label: GAME3D_FIELD_SUN.to_string()
                control: sun_slider
                tokens: tokens
            }
        }
    };
    let import_card: NodeId = view! {
        EuvCard {
            title: GAME3D_IMPORT_TITLE.to_string()
            tokens: tokens
            subtitle: GAME3D_IMPORT_SUB
            EuvActionRow {
                EuvButton {
                    label: BTN_GAME3D_IMPORT_OBJ.to_string()
                    tokens: tokens
                    outline()
                    small()
                    on_click: on_import_obj
                }
                EuvButton {
                    label: BTN_GAME3D_IMPORT_GLTF.to_string()
                    tokens: tokens
                    outline()
                    small()
                    on_click: on_import_gltf
                }
            }
        }
    };
    let pso_sig: Signal<Option<ShaderPrecompileReport>> = Signal::new(shader_precompile_status());
    let on_precompile_3d = move |_: &UiEvent| {
        let report: ShaderPrecompileReport = precompile_shaders(default_native_gpu_backend());
        let _: bool = pso_sig.set(Some(report));
    };
    let pso_status_text = move || -> String {
        if let Some(report) = pso_sig.get()
            && let Some(entry) = report.entry_for(ShaderProgramId::Engine3dPbr)
        {
            let compile_ms: f64 = (entry.get_compile_time_us() as f64) / 1000.0;
            let hit_tag: &str = if report.get_cache_hit() {
                "Warm Hit"
            } else {
                entry.get_state().label()
            };
            format!(
                "3D PBR [{}] · {hit_tag} · {compile_ms:.2}ms · PSO {}/{}",
                entry.get_language(),
                report.get_ready_programs(),
                report.get_total_programs(),
            )
        } else {
            format!(
                "3D PBR [{}] · Idle — warm pipeline cache below",
                ShaderProgramId::language_for_backend(default_native_gpu_backend()),
            )
        }
    };
    let pso_text_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        style! {
            Width(Length::Percent(100.0))
            FontSize(12.0)
            FontFamily::Monospace
            TextColor(live.get_palette().get_primary())
        }
    };
    let pso_col_style: Style = style! {
        Width(Length::Percent(100.0))
        Gap(SPACE_SM)
    };
    let pso_card: NodeId = view! {
        EuvCard {
            title: GAME3D_PSO_CARD_TITLE.to_string()
            tokens: tokens
            subtitle: GAME3D_PSO_CARD_SUB
            column {
                style: pso_col_style
                text {
                    bind_style: pso_text_style
                    { pso_status_text }
                }
                EuvActionRow {
                    EuvButton {
                        label: BTN_GAME3D_PRECOMPILE_PSO.to_string()
                        tokens: tokens
                        outline()
                        small()
                        on_click: on_precompile_3d
                    }
                }
            }
        }
    };
    let column_style: Style = style! {
        Width(Length::Percent(100.0))
        Gap(SPACE_MD)
    };
    let rail: NodeId = view! {
        column {
            style: column_style
            { graphics_card }
            { import_card }
            { pso_card }
        }
    };
    let studio: NodeId = mount_studio_layout(main_card, rail);
    let page: NodeId = view! {
        EuvPage {
            title: GAME3D_TITLE.to_string()
            subtitle: GAME3D_SUBTITLE.to_string()
            tokens: tokens
            { studio }
            { fs_3d_portal }
        }
    };
    ctx.get_scene().with_write(
        |tree: &mut SceneTree| {
            tree.attach_scope(sync_host_id, sync_scope);
        },
        (),
    );
    page
}