TerraForge3D  2.3.1
3D Terrain And Landscape Generator
LayeredNoiseManager.cpp
1#include "NoiseLayers/LayeredNoiseManager.h"
2
3#include "imgui.h"
4
5#include <iostream>
6
7#define MAKE_IMGUI_ID(x) ("##LayeredNoiseManager" + std::string(x)).c_str()
8
9
10LayeredNoiseManager::LayeredNoiseManager()
11{
12 offset[0] = offset[1] = offset[2] = 0;
13 strength = 1.0f;
14 absv = false;
15 sq = false;
16 noiseLayers.push_back(new NoiseLayer());
17}
18
19void LayeredNoiseManager::Render()
20{
21 ImGui::Text("Global Offsets:");
22 ImGui::DragFloat3(MAKE_IMGUI_ID("Global Offsets"), offset, 0.1f);
23 ImGui::DragFloat("Global Strength##LayeredNoiseManager", &strength, 0.1f);
24 ImGui::Checkbox("Absolute Value##LayeredNoiseManager", &absv);
25 ImGui::Checkbox("Square Value##LayeredNoiseManager", &sq);
26 ImGui::NewLine();
27 ImGui::Text("Noise Layers");
28 std::vector<NoiseLayer *> nl = noiseLayers;
29
30 for (int i = 0; i < nl.size(); i++)
31 {
32 bool state = ImGui::CollapsingHeader((std::string("##noiseLayerName") + std::to_string(i)).c_str());
33
34 if (state)
35 {
36 nl[i]->Render(i);
37
38 if (toAdd.size() == 0)
39 {
40 if (ImGui::Button("Duplicate"))
41 {
42 nlohmann::json data = nl[i]->Save();
43 toAdd.push_back(new NoiseLayer());
44 toAdd.back()->Load(data);
45 }
46 }
47
48 if (noiseLayers.size() > 1 && toDelete.size() == 0)
49 {
50 ImGui::SameLine();
51
52 if (ImGui::Button("Delete"))
53 {
54 toDelete.push_back(i);
55 }
56 }
57 }
58
59 else
60 {
61 ImGui::SameLine();
62 ImGui::Text(nl[i]->name.data());
63 }
64 }
65
66 if (toAdd.size() == 0)
67 {
68 if (ImGui::Button("Add New Layer"))
69 {
70 toAdd.push_back(new NoiseLayer());
71 }
72 }
73}
74
75void LayeredNoiseManager::UpdateLayers()
76{
77 if (toDelete.size() > 0)
78 {
79 noiseLayers.erase(noiseLayers.begin() + toDelete[0]);
80 toDelete.clear();
81 }
82
83 if (toAdd.size() > 0)
84 {
85 noiseLayers.push_back(toAdd[0]);
86 toAdd.clear();
87 }
88}
89
90void LayeredNoiseManager::Load(nlohmann::json data)
91{
92 sq = data["sq"];
93 absv = data["absv"];
94 offset[0] = data["offsetX"];
95 offset[1] = data["offsetY"];
96 offset[2] = data["offsetZ"];
97 strength = data["strength"];
98 mutex.lock();
99
100 for (NoiseLayer *nl : noiseLayers)
101 {
102 delete nl;
103 }
104
105 noiseLayers.clear();
106
107 for (nlohmann::json tmp : data["noiseLayers"])
108 {
109 noiseLayers.push_back(new NoiseLayer());
110 noiseLayers.back()->Load(tmp);
111 }
112
113 mutex.unlock();
114}
115
116nlohmann::json LayeredNoiseManager::Save()
117{
118 nlohmann::json data, tmp, tmp2;
119 mutex.lock();
120
121 for (int i = 0; i < noiseLayers.size(); i++)
122 {
123 tmp2 = noiseLayers[i]->Save();
124 tmp2["index"] = i;
125 tmp.push_back(tmp2);
126 }
127
128 mutex.unlock();
129 data["noiseLayers"] = tmp;
130 data["sq"] = sq;
131 data["absv"] = absv;
132 data["offsetX"] = offset[0];
133 data["offsetY"] = offset[1];
134 data["offsetZ"] = offset[2];
135 data["strength"] = strength;
136 return data;
137}
138
139float LayeredNoiseManager::Evaluate(float x, float y, float z)
140{
141 std::vector<NoiseLayer *> nl = noiseLayers;
142 float noise = 0.0f;
143
144 for (NoiseLayer *n : nl)
145 noise += n->Evaluate({ x + offset[0], y + offset[1], z + offset[2] });
146 noise *= strength;
147
148 if (absv)
149 {
150 noise = abs(noise);
151 }
152
153 if (sq)
154 {
155 noise = noise * noise;
156 }
157
158 return noise;
159}
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:23148
a class to store JSON values
Definition: json.hpp:17860