Skip to main content

webizen_studio/render/
mod.rs

1//! Backend-agnostic immediate-mode 3D line/point rendering.
2//!
3//! The browser demo currently renders 3D on the CPU into a Canvas 2D context
4//! (`Canvas2dRenderer()`). The native runtime has real `wgpu` compute, and a
5//! future `WgpuRenderer` (WebGPU/WebGL) can implement this same [`Renderer`]
6//! trait so call sites such as the physics surface stay backend-agnostic and the
7//! browser demo can eventually benchmark genuine GPU work.
8//!
9//! Geometry is submitted in **world space**; each backend is responsible for
10//! projection. The CPU backend projects via [`Camera::project`]; a GPU backend
11//! would project in a vertex shader.
12
13pub mod graph;
14pub mod mesh;
15pub mod motion;
16pub mod motion_loop;
17pub mod qualia;
18pub mod scene;
19
20pub mod command_stack;
21pub mod gizmo_3d;
22pub mod inspector_10d;
23pub mod node_graph;
24pub mod q_viewport;
25pub mod spatial_bridge;
26pub mod tensor_buffer;
27
28#[cfg(not(target_arch = "wasm32"))]
29pub mod scene_to_contract;
30
31#[cfg(not(target_arch = "wasm32"))]
32pub mod native;
33
34#[cfg(not(target_arch = "wasm32"))]
35pub mod studio_preview;
36
37#[cfg(target_arch = "wasm32")]
38pub mod canvas2d;
39
40pub use command_stack::{StudioCommand, StudioCommandStack};
41pub use gizmo_3d::{
42    GizmoDragDelta, GizmoHandle, GizmoMode, GizmoPickHit, Motor as StudioPgaMotor, MotorGizmo3D,
43};
44pub use graph::{Node, Scene, Style};
45pub use inspector_10d::{
46    AbsmLayerInspectorState, AffordanceBinaryRecord, DevelopmentalKeyframeRecord,
47    EntityOntologyMiniHeader, Epistemic10DSliceParams, ProceduralSynthesisHeader,
48    StudioNodeInspector10D, Tensor10D as StudioTensor10D, MAX_INSPECTOR_ABSM_LAYERS,
49    MAX_INSPECTOR_AFFORDANCES, MAX_INSPECTOR_KEYFRAMES,
50};
51pub use mesh::{Mesh, Transform};
52pub use qualia::{build_scene, item_color, ItemState, SceneItem, SceneSource, SemanticScene};
53pub use scene::{Camera, ScreenPoint, Vec3};
54
55#[cfg(target_arch = "wasm32")]
56pub use canvas2d::Canvas2dRenderer;
57
58/// Ergonomic single-import surface for building scenes:
59/// `use crate::render::prelude::*;`
60pub mod prelude {
61    #[cfg(target_arch = "wasm32")]
62    pub use super::canvas2d::Canvas2dRenderer;
63    pub use super::graph::{Node, Scene, Style};
64    pub use super::mesh::{Mesh, Transform};
65
66    pub use super::qualia::{build_scene, item_color, ItemState, SceneItem, SemanticScene};
67    pub use super::scene::{Camera, ScreenPoint, Vec3};
68}
69
70/// Touch the render stack so graph/mesh/qualia/scene exports stay wired (lib + bin).
71pub fn render_stack_revision() -> usize {
72    #[cfg(not(target_arch = "wasm32"))]
73    let panes: &[crate::canvas_model::PanePlacement] = &[];
74    let _transform = Transform::default();
75    let _scene_item = SceneItem {
76        id: "render-stack-wire".to_string(),
77        state: ItemState::Active,
78        intensity: 0.5,
79        provenance: None,
80        reasons: Vec::new(),
81    };
82    let _scene_source = SemanticScene::default().semantic_scene();
83    {
84        use prelude::*;
85        let _prelude_scene = Scene::new(Camera::default()).add(
86            Node::new("prelude-origin")
87                .with_mesh(Mesh::line(Vec3::default(), Vec3::new(0.0, 1.0, 0.0)))
88                .with_style(Style::wire("var(--qualia-text-muted)")),
89        );
90        let _ = build_scene(&SemanticScene::default(), Camera::default(), |_| {
91            None::<(Vec3, Mesh)>
92        });
93        let _ = item_color(ItemState::Active, 0.25);
94        let _prelude_item = SceneItem {
95            id: "prelude-item".to_string(),
96            state: ItemState::Default,
97            intensity: 0.0,
98            provenance: None,
99            reasons: Vec::new(),
100        };
101        let _screen = ScreenPoint {
102            x: 0.0,
103            y: 0.0,
104            depth: 1.0,
105        };
106        let _xf = Transform::default();
107    }
108    let _scene = Scene::new(Camera::default()).add(
109        Node::new("origin")
110            .with_mesh(Mesh::line(Vec3::default(), Vec3::new(1.0, 0.0, 0.0)))
111            .with_style(Style::wire("var(--qualia-accent)")),
112    );
113    let _ = item_color(ItemState::Active, 0.5);
114    let _ = build_scene(&SemanticScene::default(), Camera::default(), |_| {
115        None::<(Vec3, Mesh)>
116    });
117    #[cfg(not(target_arch = "wasm32"))]
118    {
119        let _ = workspace_scene_from_panes(panes);
120        let _ = workspace_scene_from_source(&SemanticScene::default(), panes);
121        let _ = semantic_scene_from_panes(panes);
122        let _ = render_contract_from_panes(panes);
123        let _ = rasterize_scene_draw_count(panes);
124        let _ = preview_item_swatch(ItemState::Active, 0.5);
125        let _ = tensor_buffer_digest(&[]);
126        let mut cam = Camera::default();
127        orbit_preview_camera(&mut cam, 0.1, 0.2);
128        let mut spring = motion::Spring::new(0.0);
129        let theme =
130            crate::theme_engine::resolve_theme(None, &crate::theme_engine::builtin_theme_catalog());
131        let _ = theme_motion_timeline(&theme, 0.016);
132        let _ = step_native_selection_pulse(&mut spring, true, &theme);
133        let binding = crate::theme_engine::ThemeBinding::default();
134        let _ = motion_class_from_binding(&binding, &crate::theme_engine::builtin_theme_catalog());
135        let _ = empty_workspace_fallback_scene();
136    }
137    1
138}
139
140/// Re-export the native studio preview pipeline for canvas + theme wiring.
141#[cfg(not(target_arch = "wasm32"))]
142pub use studio_preview::{
143    empty_workspace_fallback_scene, motion_class_from_binding, native_headless_png_byte_len,
144    orbit_preview_camera, preview_item_swatch, rasterize_scene_draw_count,
145    render_contract_from_panes, semantic_scene_from_panes, step_native_selection_pulse,
146    tensor_buffer_digest, theme_motion_timeline, workspace_scene_from_panes,
147    workspace_scene_from_source,
148};
149
150/// An immediate-mode renderer for line/point 3D scenes.
151///
152/// Colors are CSS color strings (e.g. `"#67e8f9"`, `"rgba(...)"`) so the CPU
153/// backend can pass them straight to Canvas 2D; a GPU backend parses them.
154pub trait Renderer {
155    /// Current drawable size in physical pixels.
156    fn viewport(&self) -> (f64, f64);
157
158    /// Set the active camera used for [`Renderer::project`].
159    fn set_camera(&mut self, camera: Camera);
160
161    /// Clear the frame to a solid background color.
162    fn clear(&self, color: &str);
163
164    /// Project a world-space point to screen space (CPU-side cull/fade helper).
165    fn project(&self, world: Vec3) -> Option<ScreenPoint>;
166
167    /// Draw a screen-space line segment.
168    fn line(&self, a: ScreenPoint, b: ScreenPoint, color: &str, alpha: f64, width: f64);
169
170    /// Draw a filled screen-space disc (billboarded point).
171    fn point(&self, p: ScreenPoint, radius: f64, color: &str, alpha: f64);
172
173    /// Fill a screen-space polygon (e.g. a depth-shaded quad).
174    fn fill_polygon(&self, points: &[ScreenPoint], color: &str, alpha: f64);
175}