TerraForge3D  2.3.1
3D Terrain And Landscape Generator
BlendNode.cpp
1#include "Generators/CPUNodeEditor/Nodes/BlendNode.h"
2#include "Base/ImGuiShapes.h"
3#include "Generators/CPUNodeEditor/CPUNodeEditor.h"
4#include <iostream>
5
6#include <mutex>
7
8static const char *items[] = { "Add", "Subtract", "Multiply", "Divide" };
9
10NodeOutput BlendNode::Evaluate(NodeInputParam input, NodeEditorPin *pin)
11{
12 float v1, v2, f;
13 v1 = v2 = f = 0;
14
15 if (inputPins[0]->IsLinked())
16 {
17 v1 = inputPins[0]->other->Evaluate(input).value;
18 }
19
20 else
21 {
22 v1 = value1;
23 }
24
25 if (inputPins[1]->IsLinked())
26 {
27 v2 = inputPins[1]->other->Evaluate(input).value;
28 }
29
30 else
31 {
32 v2 = value2;
33 }
34
35 if (inputPins[2]->IsLinked())
36 {
37 f = inputPins[2]->other->Evaluate(input).value;
38 }
39
40 else
41 {
42 f = factor;
43 }
44
45 return NodeOutput({ f * v1 + (1 - f) * v2 });
46}
47
48void BlendNode::Load(nlohmann::json data)
49{
50 value1 = data["value1"];
51 value2 = data["value2"];
52 factor = data["factor"];
53}
54
55nlohmann::json BlendNode::Save()
56{
57 nlohmann::json data;
58 data["type"] = MeshNodeEditor::MeshNodeType::Blend;
59 data["value1"] = value1;
60 data["value2"] = value2;
61 data["factor"] = factor;
62 return data;
63}
64
65void BlendNode::OnRender()
66{
67 DrawHeader("Blend");
68 inputPins[0]->Render();
69 ImGui::Text("Value A");
70
71 if (!inputPins[0]->IsLinked())
72 {
73 ImGui::PushItemWidth(100);
74 ImGui::DragFloat(("##" + std::to_string(inputPins[0]->id)).c_str(), &value1, 0.01f);
75 ImGui::PopItemWidth();
76 }
77
78 ImGui::SameLine();
79 ImGui::Text("Out");
80 outputPins[0]->Render();
81 inputPins[1]->Render();
82 ImGui::Text("Value B");
83
84 if (!inputPins[1]->IsLinked())
85 {
86 ImGui::PushItemWidth(100);
87 ImGui::DragFloat(("##" + std::to_string(inputPins[1]->id)).c_str(), &value2, 0.01f);
88 ImGui::PopItemWidth();
89 }
90
91 inputPins[2]->Render();
92 ImGui::Text("Factor");
93
94 if (!inputPins[2]->IsLinked())
95 {
96 ImGui::PushItemWidth(100);
97 ImGui::DragFloat(("##" + std::to_string(inputPins[2]->id)).c_str(), &factor, 0.01f, 0, 1);
98 ImGui::PopItemWidth();
99 }
100
101 ImGui::NewLine();
102 ImGui::Text("Blend Mode");
103}
104
105BlendNode::BlendNode()
106{
107 inputPins.push_back(new NodeEditorPin());
108 inputPins.push_back(new NodeEditorPin());
109 inputPins.push_back(new NodeEditorPin());
110 outputPins.push_back(new NodeEditorPin(NodeEditorPinType::Output));
111 headerColor = ImColor(OP_NODE_COLOR);
112 value1 = 0.0f;
113 value2 = 1.0f;
114 factor = 0.5f;
115}
a class to store JSON values
Definition: json.hpp:17860