Skip to main content

strata/runtime/
canvas3d.rs

1//! Declarative 3D Canvas Viewport extraction and DrawViewport3D command generation.
2
3use super::*;
4
5impl AppRuntime {
6    /// Extracts declarative or programmatic 3D canvas viewports when not already emitted by [`Compositor`].
7    pub fn render_canvas_3d_scenes(&mut self) {
8        if self.display_list.commands.iter().any(|cmd| {
9            matches!(
10                cmd,
11                strata_render::compositor::DrawCommand::DrawViewport3D { .. }
12            )
13        }) {
14            return;
15        }
16        Self::collect_canvas_3d_recursive(
17            &self.root_blueprint,
18            &self.layout_root,
19            &mut self.display_list.commands,
20        );
21    }
22
23    fn collect_canvas_3d_recursive(
24        blueprint: &NodeBlueprint,
25        layout_node: &LayoutNode,
26        commands: &mut Vec<strata_render::compositor::DrawCommand>,
27    ) {
28        match blueprint {
29            NodeBlueprint::Element {
30                tag,
31                attributes,
32                children,
33                ..
34            } => {
35                let has_scene_payload = attributes
36                    .iter()
37                    .any(|a| matches!(a, strata_dom::Attribute::ScenePayload(_)));
38                let is_3d_canvas = has_scene_payload
39                    || (matches!(tag, Tag::Canvas | Tag::Custom("Canvas3D"))
40                        && (attributes.iter().any(|a| {
41                            matches!(a, strata_dom::Attribute::Property { key, value } if key == "data-strata-canvas3d" && value == "true")
42                        }) || children.iter().any(|c| match c {
43                            NodeBlueprint::Element {
44                                tag: Tag::Custom(name),
45                                ..
46                            } => matches!(
47                                *name,
48                                "PerspectiveCamera"
49                                    | "OrthographicCamera"
50                                    | "AmbientLight"
51                                    | "DirectionalLight"
52                                    | "PointLight"
53                                    | "Mesh"
54                                    | "BoxGeometry"
55                                    | "SphereGeometry"
56                                    | "PlaneGeometry"
57                                    | "CylinderGeometry"
58                                    | "StandardMaterial"
59                                    | "ShaderMaterial"
60                            ),
61                            _ => false,
62                        })));
63
64                if is_3d_canvas {
65                    let scene = strata_render::scene3d::compile_scene3d_from_blueprint(blueprint);
66                    let bounds = if layout_node.geometry.content_box.width > 0.0
67                        && layout_node.geometry.content_box.height > 0.0
68                    {
69                        layout_node.geometry.content_box
70                    } else {
71                        layout_node.geometry.border_box
72                    };
73                    commands.push(strata_render::compositor::DrawCommand::DrawViewport3D {
74                        bounds,
75                        scene: std::sync::Arc::new(scene),
76                    });
77                    if matches!(tag, Tag::Canvas | Tag::Custom("Canvas3D")) {
78                        return;
79                    }
80                }
81
82                let mut flat_children = Vec::new();
83                for c in children {
84                    Self::flatten_blueprints(c, &mut flat_children);
85                }
86
87                for (child_bp, child_layout) in
88                    flat_children.iter().zip(layout_node.children.iter())
89                {
90                    Self::collect_canvas_3d_recursive(child_bp, child_layout, commands);
91                }
92            }
93            NodeBlueprint::Fragment(children) => {
94                let mut flat_children = Vec::new();
95                for c in children {
96                    Self::flatten_blueprints(c, &mut flat_children);
97                }
98                for (child_bp, child_layout) in
99                    flat_children.iter().zip(layout_node.children.iter())
100                {
101                    Self::collect_canvas_3d_recursive(child_bp, child_layout, commands);
102                }
103            }
104            _ => {}
105        }
106    }
107}