node_engine 0.7.0

Node graph engine for Shader graph or Geometry graph.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use core::fmt;

use uuid::Uuid;

use anyhow::{Result, anyhow};

use serde::{Deserialize, Serialize, Serializer, de::Deserializer};

#[cfg(feature = "egui")]
use crate::ui::*;
use crate::*;

pub type NodeId = Uuid;

pub const NAMESPACE_NODE_IMPL: Uuid = uuid::uuid!("9dee91a8-5af8-11ee-948b-5364d73b1803");

/// This is used to resolve Dynamic Vector/Matrix inputs/outputs.
#[derive(Copy, Clone, Debug, Default)]
pub struct NodeConcreteType {
  /// The minimum size of connected Vector/Matrix.  Scalars are not counted.
  pub min: Option<DynamicSize>,
  pub scalars: usize,
  pub vectors: usize,
  pub matrixes: usize,
}

impl NodeConcreteType {
  pub fn has_dynamic(&self) -> bool {
    let count = self.scalars + self.vectors + self.matrixes;
    count > 0
  }

  pub fn data_type(&self) -> Option<DataType> {
    let count = self.scalars + self.vectors + self.matrixes;
    if count > 0 {
      match (self.min, self.scalars, self.vectors, self.matrixes) {
        (None | Some(DynamicSize::D1), s, 0, 0) if s > 0 => Some(DataType::F32),
        (Some(DynamicSize::D2), _, 0, m) if m > 0 => Some(DataType::Mat2),
        (Some(DynamicSize::D3), _, 0, m) if m > 0 => Some(DataType::Mat3),
        (Some(DynamicSize::D4), _, 0, m) if m > 0 => Some(DataType::Mat4),
        (Some(DynamicSize::D2), _, v, _) if v > 0 => Some(DataType::Vec2),
        (Some(DynamicSize::D3), _, v, _) if v > 0 => Some(DataType::Vec3),
        (Some(DynamicSize::D4), _, v, _) if v > 0 => Some(DataType::Vec4),
        _ => None,
      }
    } else {
      None
    }
  }

  pub fn convert(&self, value: &mut CompiledValue) -> Result<()> {
    if let Some(min) = self.min {
      let (vec_dt, mat_dt) = match min {
        DynamicSize::D3 => (DataType::Vec3, DataType::Mat3),
        DynamicSize::D4 => (DataType::Vec4, DataType::Mat4),
        _ => (DataType::Vec2, DataType::Mat2),
      };
      match value.dt.class() {
        DataTypeClass::Scalar => value.convert(vec_dt),
        DataTypeClass::Vector => value.convert(vec_dt),
        DataTypeClass::Matrix => value.convert(mat_dt),
        class => Err(anyhow!(
          "Unsupported data type conversion: class={class:?}, dt={:?}",
          value.dt
        )),
      }
    } else {
      Ok(())
    }
  }

  pub fn add_input_type(&mut self, dt: DataType) {
    let min = self.min.unwrap_or(DynamicSize::D4).len();
    match dt {
      DataType::I32 | DataType::U32 | DataType::F32 => {
        self.scalars += 1;
        // Don't update the `min` for Scalars.
      }
      DataType::Vec2 => {
        self.vectors += 1;
        // D2 is the smallest.
        self.min = Some(DynamicSize::D2);
      }
      DataType::Vec3 => {
        self.vectors += 1;
        if min > 3 {
          self.min = Some(DynamicSize::D3);
        }
      }
      DataType::Vec4 => {
        self.vectors += 1;
        if min > 4 {
          self.min = Some(DynamicSize::D4);
        }
      }
      DataType::Mat2 => {
        self.matrixes += 1;
        // D2 is the smallest.
        self.min = Some(DynamicSize::D2);
      }
      DataType::Mat3 => {
        self.matrixes += 1;
        if min > 3 {
          self.min = Some(DynamicSize::D3);
        }
      }
      DataType::Mat4 => {
        self.matrixes += 1;
        if min > 4 {
          self.min = Some(DynamicSize::D4);
        }
      }
      _ => (),
    }
  }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct InputId {
  pub node: NodeId,
  pub idx: u32,
}

impl InputId {
  pub fn new(node: NodeId, idx: u32) -> Self {
    Self { node, idx }
  }

  pub fn node(&self) -> NodeId {
    self.node
  }

  pub fn key(&self) -> InputKey {
    self.idx.into()
  }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct OutputId {
  pub node: NodeId,
  pub idx: u32,
}

impl OutputId {
  pub fn new(node: NodeId, idx: u32) -> Self {
    Self { node, idx }
  }

  pub fn node(&self) -> NodeId {
    self.node
  }
}

impl From<NodeId> for OutputId {
  fn from(node: NodeId) -> Self {
    Self { node, idx: 0 }
  }
}

pub trait NodeImpl: fmt::Debug + erased_serde::Serialize + Send + Sync {
  fn clone_node(&self) -> Box<dyn NodeImpl>;

  fn def(&self) -> &NodeDefinition;

  fn cache_output(&self) -> bool {
    false
  }

  fn get_input_idx(&self, key: &InputKey) -> Result<u32> {
    match key {
      InputKey::Idx(idx) => Ok(*idx),
      key => self.def().get_input_idx(key),
    }
  }

  fn get_node_input(&self, _idx: &InputKey) -> Result<Input> {
    Err(anyhow!("This node doesn't support `get_input`"))
  }

  fn set_node_input(&mut self, _idx: &InputKey, _value: Input) -> Result<Option<OutputId>> {
    Err(anyhow!("This node doesn't support `set_input`"))
  }

  fn get_param(&self, _name: &str) -> Result<ParameterValue> {
    Err(anyhow!("This node doesn't support `get_param`"))
  }

  fn set_param(&mut self, _name: &str, _value: ParameterValue) -> Result<()> {
    Err(anyhow!("This node doesn't support `get_param`"))
  }

  fn eval(
    &self,
    _graph: &NodeGraph,
    _execution: &mut NodeGraphExecution,
    _id: NodeId,
  ) -> Result<Value> {
    Err(anyhow!("This node doesn't support `eval`."))
  }

  fn compile(
    &self,
    _graph: &NodeGraph,
    _compile: &mut NodeGraphCompile,
    _id: NodeId,
  ) -> Result<()> {
    Err(anyhow!("This node doesn't support `compile`."))
  }

  #[cfg(feature = "egui")]
  fn details_ui(&mut self, ui: &mut egui::Ui, id: NodeId) -> bool {
    self.ui(ui, id, true)
  }

  #[cfg(feature = "egui")]
  fn node_ui(&mut self, ui: &mut egui::Ui, id: NodeId) -> bool {
    self.ui(ui, id, false)
  }

  #[cfg(feature = "egui")]
  fn ui(&mut self, ui: &mut egui::Ui, id: NodeId, details: bool) -> bool {
    let def = self.def();
    let input_count = def.inputs.len();
    let param_count = def.parameters.len();
    let node_style = NodeStyle::get(ui);
    let zoom = node_style.zoom;
    let mut concrete_type = NodeConcreteType::default();
    let mut updated = false;
    if details {
      if self.inputs_ui(&mut concrete_type, ui, id, details) {
        updated = true;
      }
      if param_count > 0 {
        if input_count > 0 {
          ui.separator();
        }
        if self.parameters_ui(&mut concrete_type, ui, id, details) {
          updated = true;
        }
      }
    } else {
      let output_count = def.outputs.len();
      ui.vertical(|ui| {
        ui.horizontal(|ui| {
          if input_count > 0 {
            ui.vertical(|ui| {
              if self.inputs_ui(&mut concrete_type, ui, id, details) {
                updated = true;
              }
            });
          }
          if output_count > 0 {
            if input_count > 0 {
              ui.separator();
            }
            ui.vertical(|ui| {
              ui.set_min_width(50.0 * zoom);
              if self.outputs_ui(&mut concrete_type, ui, id, details) {
                updated = true;
              }
            });
          }
        });
        if param_count > 0 {
          ui.separator();
          if self.parameters_ui(&mut concrete_type, ui, id, details) {
            updated = true;
          }
        }
      });
    }
    updated
  }

  #[cfg(feature = "egui")]
  fn inputs_ui(
    &mut self,
    concrete_type: &mut NodeConcreteType,
    ui: &mut egui::Ui,
    id: NodeId,
    details: bool,
  ) -> bool;

  #[cfg(feature = "egui")]
  fn parameters_ui(
    &mut self,
    concrete_type: &mut NodeConcreteType,
    ui: &mut egui::Ui,
    _id: NodeId,
    details: bool,
  ) -> bool;

  #[cfg(feature = "egui")]
  fn outputs_ui(
    &mut self,
    concrete_type: &mut NodeConcreteType,
    ui: &mut egui::Ui,
    id: NodeId,
    details: bool,
  ) -> bool;
}

impl Serialize for dyn NodeImpl {
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  where
    S: Serializer,
  {
    erased_serde::serialize(self, serializer)
  }
}

/// Define some generic helper methods for Nodes.
///
/// Boxed trait objects can't have generic methods.
pub trait NodeInterface: NodeImpl {
  fn get_input<I: Into<InputKey>>(&self, idx: I) -> Result<Input>;
  fn set_input<I: Into<InputKey>>(&mut self, idx: I, value: Input) -> Result<Option<OutputId>>;
}

impl<T: ?Sized + NodeImpl> NodeInterface for T {
  fn get_input<I: Into<InputKey>>(&self, idx: I) -> Result<Input> {
    self.get_node_input(&idx.into())
  }

  fn set_input<I: Into<InputKey>>(&mut self, idx: I, value: Input) -> Result<Option<OutputId>> {
    self.set_node_input(&idx.into(), value)
  }
}

impl Clone for Box<dyn NodeImpl> {
  fn clone(&self) -> Self {
    self.clone_node()
  }
}

#[derive(serde::Deserialize)]
pub struct LoadNodeState {
  pub id: NodeId,
  pub group_id: NodeGroupId,
  pub name: String,
  pub node_type: Uuid,
  pub node: serde_json::Value,
  pub area: emath::Rect,
}

#[derive(Clone, Debug, serde::Serialize)]
pub struct Node {
  pub id: NodeId,
  pub group_id: NodeGroupId,
  pub name: String,
  node_type: Uuid,
  node: Box<dyn NodeImpl>,
  pub area: emath::Rect,
  #[serde(skip)]
  pub updated: bool,
}

impl GetId for Node {
  fn id(&self) -> Uuid {
    self.id
  }
}

impl<'de> Deserialize<'de> for Node {
  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  where
    D: Deserializer<'de>,
  {
    let node = LoadNodeState::deserialize(deserializer)?;
    NODE_REGISTRY
      .load_node(node)
      .map_err(serde::de::Error::custom)
  }
}

impl Node {
  pub fn new(def: &NodeDefinition) -> Result<Self> {
    Ok(Self {
      id: Uuid::new_v4(),
      group_id: Uuid::nil(),
      name: def.name.clone(),
      node_type: def.id,
      node: def.new_node()?,
      area: emath::Rect::from_min_size([0., 0.].into(), [10., 10.].into()),
      updated: true,
    })
  }

  pub fn load(def: &NodeDefinition, data: LoadNodeState) -> Result<Self> {
    Ok(Self {
      id: data.id,
      group_id: data.group_id,
      name: data.name,
      node_type: data.node_type,
      node: def.load_node(data.node)?,
      area: data.area,
      updated: true,
    })
  }

  pub fn set_position(&mut self, position: emath::Vec2) {
    self.area = emath::Rect::from_min_size(position.to_pos2(), self.area.size());
  }

  pub(crate) fn new_id(&mut self) {
    self.id = Uuid::new_v4();
  }

  /// Clone node with a new uuid.
  pub fn duplicate(&self) -> Self {
    let mut node = self.clone();
    node.new_id();
    node
  }

  pub fn eval(
    &self,
    graph: &NodeGraph,
    execution: &mut NodeGraphExecution,
    id: NodeId,
  ) -> Result<Value> {
    self.node.eval(graph, execution, id)
  }

  pub fn compile(
    &self,
    graph: &NodeGraph,
    compile: &mut NodeGraphCompile,
    id: NodeId,
  ) -> Result<()> {
    self.node.compile(graph, compile, id)
  }

  pub fn cache_output(&self) -> bool {
    self.node.cache_output()
  }

  pub fn get_input_idx(&self, idx: &InputKey) -> Result<u32> {
    self.node.get_input_idx(idx)
  }

  pub fn get_input<I: Into<InputKey>>(&self, idx: I) -> Result<Input> {
    self.node.get_node_input(&idx.into())
  }

  pub fn set_input<I: Into<InputKey>>(&mut self, idx: I, value: Input) -> Result<Option<OutputId>> {
    self.updated = true;
    self.node.set_node_input(&idx.into(), value)
  }

  #[cfg(feature = "egui")]
  pub fn details_ui(&mut self, ui: &mut egui::Ui, id: NodeId) -> bool {
    self.node.details_ui(ui, id)
  }
}

#[cfg(feature = "egui")]
impl NodeFrame for Node {
  fn title(&self) -> &str {
    &self.name
  }

  fn set_title(&mut self, title: String) {
    self.name = title;
  }

  fn take_updated(&mut self, state: &mut NodeFrameState) -> bool {
    let updated = self.updated | state.take_updated();
    self.updated = false;
    updated
  }

  fn rect(&self) -> emath::Rect {
    self.area
  }

  fn set_rect(&mut self, rect: emath::Rect) {
    self.area = rect;
  }

  fn auto_size(&self) -> bool {
    true
  }

  fn resizable(&self) -> bool {
    false
  }

  fn contents_ui(&mut self, ui: &mut egui::Ui, node_style: NodeStyle) {
    egui::Frame::new()
      .fill(egui::Color32::from_gray(63))
      .show(ui, |ui| {
        ui.set_min_width(node_style.node_min_size.x);
        if self.node.node_ui(ui, self.id) {
          self.updated = true;
        }
      });
  }

  /// Handle events and context menu.
  fn handle_resp(
    &mut self,
    _ui: &mut egui::Ui,
    resp: egui::Response,
    _graph: &NodeGraphMeta,
    frame: &mut NodeFrameState,
  ) -> Option<NodeAction> {
    let mut action = None;
    if resp.clicked() {
      action = Some(NodeAction::Clicked);
    } else if resp.dragged() {
      if frame.is_dragging() {
        action = Some(NodeAction::Dragged(resp.drag_delta()));
      } else {
        action = Some(NodeAction::Resize);
      }
    }
    resp.context_menu(|ui| {
      if ui.button("Delete").clicked() {
        action = Some(NodeAction::Delete(false));
        ui.close_kind(egui::UiKind::Menu);
      }
      if !self.group_id.is_nil() {
        if ui.button("Remove from group").clicked() {
          action = Some(NodeAction::LeaveGroup(self.group_id));
          self.group_id = Uuid::nil();
          ui.close_kind(egui::UiKind::Menu);
        }
      }
    });
    action
  }
}