webizen_studio/render/
graph.rs1use super::mesh::{Mesh, Transform};
12use super::scene::{Camera, Vec3};
13use super::{Renderer, ScreenPoint};
14
15#[derive(Clone, Debug, PartialEq)]
18pub struct Style {
19 pub stroke: Option<String>,
21 pub fill: Option<String>,
23 pub point: Option<String>,
25 pub alpha: f64,
26 pub width: f64,
27 pub point_radius: f64,
28}
29
30impl Default for Style {
31 fn default() -> Self {
32 Self {
33 stroke: Some("var(--qualia-accent)".to_string()),
34 fill: None,
35 point: None,
36 alpha: 1.0,
37 width: 1.0,
38 point_radius: 2.5,
39 }
40 }
41}
42
43impl Style {
44 pub fn wire(color: impl Into<String>) -> Self {
46 Self {
47 stroke: Some(color.into()),
48 ..Default::default()
49 }
50 }
51 pub fn solid(fill: impl Into<String>) -> Self {
53 Self {
54 stroke: None,
55 fill: Some(fill.into()),
56 ..Default::default()
57 }
58 }
59 pub fn with_alpha(mut self, a: f64) -> Self {
61 self.alpha = a;
62 self
63 }
64}
65
66#[derive(Clone, Debug, PartialEq)]
68pub struct Node {
69 pub label: String,
70 pub transform: Transform,
71 pub mesh: Option<Mesh>,
72 pub style: Style,
73 pub children: Vec<Node>,
74 pub entity_id: u64,
76 pub affordance_bits: u8,
78}
79
80impl Default for Node {
81 fn default() -> Self {
82 Self {
83 label: String::new(),
84 transform: Transform::default(),
85 mesh: None,
86 style: Style::default(),
87 children: Vec::new(),
88 entity_id: 0,
89 affordance_bits: 0,
90 }
91 }
92}
93
94impl Node {
95 pub fn new(label: impl Into<String>) -> Self {
97 Self {
98 label: label.into(),
99 ..Default::default()
100 }
101 }
102
103 pub fn with_entity_id(mut self, entity_id: u64) -> Self {
105 self.entity_id = entity_id;
106 self
107 }
108
109 pub fn with_affordance_bits(mut self, bits: u8) -> Self {
111 self.affordance_bits = bits;
112 self
113 }
114 pub fn with_mesh(mut self, mesh: Mesh) -> Self {
116 self.mesh = Some(mesh);
117 self
118 }
119 pub fn with_style(mut self, style: Style) -> Self {
121 self.style = style;
122 self
123 }
124 pub fn with_transform(mut self, t: Transform) -> Self {
126 self.transform = t;
127 self
128 }
129 pub fn at(mut self, position: Vec3) -> Self {
131 self.transform.position = position;
132 self
133 }
134 pub fn child(mut self, node: Node) -> Self {
136 self.children.push(node);
137 self
138 }
139}
140
141#[derive(Clone, Debug, PartialEq)]
143pub struct Scene {
144 pub background: String,
145 pub camera: Camera,
146 pub roots: Vec<Node>,
147}
148
149impl Default for Scene {
150 fn default() -> Self {
151 Self {
152 background: "var(--qualia-bg)".to_string(),
153 camera: Camera::default(),
154 roots: Vec::new(),
155 }
156 }
157}
158
159impl Scene {
160 pub fn new(camera: Camera) -> Self {
162 Self {
163 camera,
164 ..Default::default()
165 }
166 }
167 pub fn with_background(mut self, bg: impl Into<String>) -> Self {
169 self.background = bg.into();
170 self
171 }
172 pub fn add(mut self, node: Node) -> Self {
174 self.roots.push(node);
175 self
176 }
177
178 pub fn render<R: Renderer>(&self, r: &mut R) {
180 r.set_camera(self.camera);
181 r.clear(&self.background);
182 for node in &self.roots {
183 render_node(node, &|v| v, r);
184 }
185 }
186}
187
188fn render_node<R: Renderer>(node: &Node, world: &dyn Fn(Vec3) -> Vec3, r: &mut R) {
192 let to_world = |v: Vec3| world(node.transform.apply(v));
193
194 if let Some(mesh) = &node.mesh {
195 if let Some(fill) = &node.style.fill {
197 for face in &mesh.faces {
198 let pts: Option<Vec<ScreenPoint>> = face
199 .iter()
200 .map(|&i| r.project(to_world(mesh.vertices[i])))
201 .collect();
202 if let Some(pts) = pts {
203 r.fill_polygon(&pts, fill, node.style.alpha);
204 }
205 }
206 }
207 if let Some(stroke) = &node.style.stroke {
209 for &(a, b) in &mesh.edges {
210 if let (Some(pa), Some(pb)) = (
211 r.project(to_world(mesh.vertices[a])),
212 r.project(to_world(mesh.vertices[b])),
213 ) {
214 r.line(pa, pb, stroke, node.style.alpha, node.style.width);
215 }
216 }
217 }
218 if let Some(point) = &node.style.point {
220 for v in &mesh.vertices {
221 if let Some(p) = r.project(to_world(*v)) {
222 r.point(p, node.style.point_radius, point, node.style.alpha);
223 }
224 }
225 }
226 }
227
228 for child in &node.children {
229 render_node(child, &to_world, r);
230 }
231}
232
233#[cfg(test)]
234mod tests {
235 use super::*;
236
237 #[test]
238 fn scene_builder_nesting() {
239 let scene = Scene::new(Camera::default()).add(
240 Node::new("parent")
241 .with_mesh(Mesh::cube(1.0))
242 .child(Node::new("child").at(Vec3::new(2.0, 0.0, 0.0))),
243 );
244 assert_eq!(scene.roots.len(), 1);
245 assert_eq!(scene.roots[0].children.len(), 1);
246 assert_eq!(scene.roots[0].children[0].transform.position.x, 2.0);
247 }
248}