TerraForge3D  2.3.1
3D Terrain And Landscape Generator
NoiseValueNode.cpp
1#include "Generators/CPUNodeEditor/Nodes/NoiseValueNode.h"
2#include "FastNoiseLite/FastNoiseLite.h"
3#include "Generators/CPUNodeEditor/CPUNodeEditor.h"
4
5static const char *fractalTypes[] = { "None", "FBm", "Ridged", "PingPong" };
6
7NodeOutput NoiseValueNode::Evaluate(NodeInputParam input, NodeEditorPin *pin)
8{
9 if(!inputPins[0]->IsLinked())
10 {
11 noiseGen->SetSeed(seed);
12 }
13
14 else
15 {
16 noiseGen->SetSeed((int)inputPins[0]->other->Evaluate(input).value);
17 }
18
19 if (!inputPins[2]->IsLinked())
20 {
21 noiseGen->SetFrequency(frequency);
22 }
23
24 else
25 {
26 noiseGen->SetFrequency(inputPins[2]->other->Evaluate(input).value);
27 }
28
29 if(fractalType == 0)
30 {
31 noiseGen->SetFractalType(FastNoiseLite::FractalType::FractalType_None);
32 }
33
34 else if (fractalType == 1)
35 {
36 noiseGen->SetFractalType(FastNoiseLite::FractalType::FractalType_FBm);
37 }
38
39 else if (fractalType == 2)
40 {
41 noiseGen->SetFractalType(FastNoiseLite::FractalType::FractalType_Ridged);
42 }
43
44 else if (fractalType == 3)
45 {
46 noiseGen->SetFractalType(FastNoiseLite::FractalType::FractalType_PingPong);
47 }
48
49 else
50 {
51 noiseGen->SetFractalType(FastNoiseLite::FractalType::FractalType_None);
52 }
53
54 if (!inputPins[1]->IsLinked())
55 {
56 noiseGen->SetFractalOctaves(octaves);
57 }
58
59 else
60 {
61 noiseGen->SetFrequency(inputPins[1]->other->Evaluate(input).value);
62 }
63
64 if (!inputPins[3]->IsLinked())
65 {
66 noiseGen->SetFractalLacunarity(lacunarity);
67 }
68
69 else
70 {
71 noiseGen->SetFractalLacunarity(inputPins[3]->other->Evaluate(input).value);
72 }
73
74 if (!inputPins[4]->IsLinked())
75 {
76 noiseGen->SetFractalGain(gain);
77 }
78
79 else
80 {
81 noiseGen->SetFractalGain(inputPins[4]->other->Evaluate(input).value);
82 }
83
84 if (!inputPins[5]->IsLinked())
85 {
86 noiseGen->SetFractalWeightedStrength(weightedStrength);
87 }
88
89 else
90 {
91 noiseGen->SetFractalWeightedStrength(inputPins[5]->other->Evaluate(input).value);
92 }
93
94 if (!inputPins[6]->IsLinked())
95 {
96 noiseGen->SetFractalPingPongStrength(pingPongStrength);
97 }
98
99 else
100 {
101 noiseGen->SetFractalPingPongStrength(inputPins[6]->other->Evaluate(input).value);
102 }
103
104 float st = strength;
105
106 if (inputPins[7]->IsLinked())
107 {
108 st = inputPins[7]->other->Evaluate(input).value;
109 }
110
111 return NodeOutput({ noiseGen->GetNoise(input.x, input.y, input.z) * st });
112}
113
114void NoiseValueNode::Load(nlohmann::json data)
115{
116 frequency = data["frequency"];
117 seed = data["seed"];
118 lacunarity = data["lacunarity"];
119 weightedStrength = data["weightedStrength"];
120 octaves = data["octaves"];
121 pingPongStrength = data["pingPongStrength"];
122 gain = data["gain"];
123 strength = data["strength"];
124 fractalType = data["fractalType"];
125}
126
127nlohmann::json NoiseValueNode::Save()
128{
129 nlohmann::json data;
130 data["type"] = MeshNodeEditor::MeshNodeType::NoiseValue;
131 data["frequency"] = frequency;
132 data["seed"] = seed;
133 data["lacunarity"] = lacunarity;
134 data["weightedStrength"] = weightedStrength;
135 data["octaves"] = octaves;
136 data["pingPongStrength"] = pingPongStrength;
137 data["gain"] = gain;
138 data["strength"] = strength;
139 data["fractalType"] = fractalType;
140 return data;
141}
142
143void NoiseValueNode::OnRender()
144{
145 DrawHeader("Value Noise");
146 ImGui::Dummy(ImVec2(150, 10));
147 ImGui::SameLine();
148 ImGui::Text("Out");
149 outputPins[0]->Render();
150 inputPins[0]->Render();
151 ImGui::Text("Seed");
152
153 if (!inputPins[0]->IsLinked())
154 {
155 ImGui::Dummy(ImVec2(30, 10));
156 ImGui::SameLine();
157 ImGui::PushItemWidth(100);
158 ImGui::DragInt(MAKE_IMGUI_ID(inputPins[0]->id), &seed, 1);
159 ImGui::PopItemWidth();
160 }
161
162 else
163 {
164 ImGui::NewLine();
165 }
166
167 inputPins[1]->Render();
168 ImGui::Text("Octaves");
169
170 if (!inputPins[1]->IsLinked())
171 {
172 ImGui::Dummy(ImVec2(30, 10));
173 ImGui::SameLine();
174 ImGui::PushItemWidth(100);
175 ImGui::DragInt(MAKE_IMGUI_ID(inputPins[1]->id), &octaves, 1);
176 ImGui::PopItemWidth();
177 }
178
179 else
180 {
181 ImGui::NewLine();
182 }
183
184 inputPins[2]->Render();
185 ImGui::Text("Frequency");
186
187 if (!inputPins[2]->IsLinked())
188 {
189 ImGui::Dummy(ImVec2(30, 10));
190 ImGui::SameLine();
191 ImGui::PushItemWidth(100);
192 ImGui::DragFloat(MAKE_IMGUI_ID(inputPins[2]->id), &frequency, 0.001f);
193 ImGui::PopItemWidth();
194 }
195
196 else
197 {
198 ImGui::NewLine();
199 }
200
201 inputPins[3]->Render();
202 ImGui::Text("Lacunarity");
203
204 if (!inputPins[3]->IsLinked())
205 {
206 ImGui::Dummy(ImVec2(30, 10));
207 ImGui::SameLine();
208 ImGui::PushItemWidth(100);
209 ImGui::DragFloat(MAKE_IMGUI_ID(inputPins[3]->id), &lacunarity, 0.01f);
210 ImGui::PopItemWidth();
211 }
212
213 else
214 {
215 ImGui::NewLine();
216 }
217
218 inputPins[4]->Render();
219 ImGui::Text("Gain");
220
221 if (!inputPins[4]->IsLinked())
222 {
223 ImGui::Dummy(ImVec2(30, 10));
224 ImGui::SameLine();
225 ImGui::PushItemWidth(100);
226 ImGui::DragFloat(MAKE_IMGUI_ID(inputPins[4]->id), &gain, 0.01f);
227 ImGui::PopItemWidth();
228 }
229
230 else
231 {
232 ImGui::NewLine();
233 }
234
235 inputPins[5]->Render();
236 ImGui::Text("Weighted Strength");
237
238 if (!inputPins[5]->IsLinked())
239 {
240 ImGui::Dummy(ImVec2(30, 10));
241 ImGui::SameLine();
242 ImGui::PushItemWidth(100);
243 ImGui::DragFloat(MAKE_IMGUI_ID(inputPins[5]->id), &weightedStrength, 0.01f, 0, 1);
244 ImGui::PopItemWidth();
245 }
246
247 else
248 {
249 ImGui::NewLine();
250 }
251
252 inputPins[6]->Render();
253 ImGui::Text("Ping Pong Strength");
254
255 if (!inputPins[6]->IsLinked())
256 {
257 ImGui::Dummy(ImVec2(30, 10));
258 ImGui::SameLine();
259 ImGui::PushItemWidth(100);
260 ImGui::DragFloat(MAKE_IMGUI_ID(inputPins[6]->id), &pingPongStrength, 0.01f);
261 ImGui::PopItemWidth();
262 }
263
264 else
265 {
266 ImGui::NewLine();
267 }
268
269 inputPins[7]->Render();
270 ImGui::Text("Strength");
271
272 if (!inputPins[7]->IsLinked())
273 {
274 ImGui::Dummy(ImVec2(30, 10));
275 ImGui::SameLine();
276 ImGui::PushItemWidth(100);
277 ImGui::DragFloat(MAKE_IMGUI_ID(inputPins[7]->id), &strength, 0.01f);
278 ImGui::PopItemWidth();
279 }
280
281 else
282 {
283 ImGui::NewLine();
284 }
285
286 ImGui::NewLine();
287 ImGui::Text("Current Fractal Type : ");
288 ImGui::SameLine();
289 ImGui::Text(fractalTypes[fractalType]);
290
291 if (ImGui::Button(MAKE_IMGUI_LABEL(id, "Change Fractal Type")))
292 {
293 fractalType++;
294
295 if (fractalType == 4)
296 {
297 fractalType = 0;
298 }
299 }
300}
301
302NoiseValueNode::NoiseValueNode()
303{
304 headerColor = ImColor(NOISE_NODE_COLOR);
305 inputPins.push_back(new NodeEditorPin());
306 inputPins.push_back(new NodeEditorPin());
307 inputPins.push_back(new NodeEditorPin());
308 inputPins.push_back(new NodeEditorPin());
309 inputPins.push_back(new NodeEditorPin());
310 inputPins.push_back(new NodeEditorPin());
311 inputPins.push_back(new NodeEditorPin());
312 inputPins.push_back(new NodeEditorPin());
313 inputPins.push_back(new NodeEditorPin());
314 outputPins.push_back(new NodeEditorPin(NodeEditorPinType::Output));
315 seed = 42;
316 frequency = 0.01f;
317 fractalType = 0;
318 octaves = 3;
319 lacunarity = 2.0f;
320 gain = 0.5f;
321 weightedStrength = 0.0f; // should be within 0 to 1
322 pingPongStrength = 2.0f;
323 strength = 1.0f;
324 noiseGen = new FastNoiseLite();
325 noiseGen->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Value);
326}
327
328NoiseValueNode::~NoiseValueNode()
329{
330 delete noiseGen;
331}
a class to store JSON values
Definition: json.hpp:17860