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