TerraForge3D  2.3.1
3D Terrain And Landscape Generator
HillNode.cpp
1#include "Generators/CPUNodeEditor/Nodes/HillNode.h"
2#include "Base/ImGuiShapes.h"
3#include "Generators/CPUNodeEditor/CPUNodeEditor.h"
4#include <iostream>
5
6#include <mutex>
7#include "Base/ImGuiCurveEditor.h"
8
9#define SQUARE(x) (x) * (x)
10#define MIN(x, y) x > y ? x : y
11
12
13NodeOutput HillNode::Evaluate(NodeInputParam input, NodeEditorPin *pin)
14{
15 float xC, yC, hC;
16
17 if (inputPins[0]->IsLinked())
18 {
19 xC = inputPins[0]->other->Evaluate(input).value;
20 }
21
22 else
23 {
24 xC = pos[0];
25 }
26
27 if (inputPins[1]->IsLinked())
28 {
29 yC = inputPins[1]->other->Evaluate(input).value;
30 }
31
32 else
33 {
34 yC = pos[1];
35 }
36
37 if (inputPins[2]->IsLinked())
38 {
39 hC = inputPins[2]->other->Evaluate(input).value;
40 }
41
42 else
43 {
44 hC = height;
45 }
46
47 float xN = input.x / input.maxX;
48 float yN = input.z / input.maxZ; // TEMP
49 xN = xN * 2 - 1;
50 yN = yN * 2 - 1;
51 float h = 1 - (SQUARE((yN - yC) / radius) + SQUARE((xN - xC) / radius));
52 h *= pow(2.71828, -p * (SQUARE(xN - xC) + SQUARE(yN - yC)));
53 return NodeOutput({ h * height });
54}
55
56void HillNode::Load(nlohmann::json data)
57{
58 pos[0] = data["posX"];
59 pos[1] = data["posY"];
60 pos[2] = data["posZ"];
61 height = data["height"];
62 radius = data["radius"];
63 p = data["p"];
64}
65
66nlohmann::json HillNode::Save()
67{
68 nlohmann::json data;
69 data["type"] = MeshNodeEditor::MeshNodeType::Hill;
70 data["posX"] = pos[0];
71 data["posY"] = pos[1];
72 data["posZ"] = pos[2];
73 data["height"] = height;
74 data["radius"] = radius;
75 data["p"] = p;
76 return data;
77}
78
79void HillNode::OnRender()
80{
81 DrawHeader("Hill");
82 ImGui::Dummy(ImVec2(150, 20));
83 ImGui::SameLine();
84 ImGui::Text("Out");
85 outputPins[0]->Render();
86 ImGui::PushItemWidth(150);
87 ImGui::DragFloat2(MAKE_IMGUI_LABEL(inputPins[0]->id, "Position"), pos, 0.01f);
88 ImGui::DragFloat(MAKE_IMGUI_LABEL(inputPins[1]->id, "Height"), &height, 0.01f);
89 ImGui::DragFloat(MAKE_IMGUI_LABEL(inputPins[2]->id, "Radius"), &radius, 0.01f, 0.00001f);
90 ImGui::DragFloat(MAKE_IMGUI_LABEL(inputPins[3]->id, "Plane Factor"), &p, 0.01f, 0.00001f);
91 ImGui::PopItemWidth();
92}
93
94HillNode::HillNode()
95{
96 inputPins.push_back(new NodeEditorPin());
97 inputPins.push_back(new NodeEditorPin());
98 inputPins.push_back(new NodeEditorPin());
99 inputPins.push_back(new NodeEditorPin());
100 inputPins.push_back(new NodeEditorPin());
101 outputPins.push_back(new NodeEditorPin(NodeEditorPinType::Output));
102 headerColor = ImColor(MATH_NODE_COLOR);
103 pos[0] = pos[1] = pos[2] = 0;
104 height = 1.0f;
105 radius = 1.0f;
106 p = 1.0f;
107}
a class to store JSON values
Definition: json.hpp:17860