1#include "Generators/CPUNodeEditor/Nodes/HeightmapNode.h"
2#include "Utils/Utils.h"
3#include "Data/ProjectData.h"
4#include "Base/Heightmap.h"
5#include "Base/ImGuiShapes.h"
6#include "Generators/CPUNodeEditor/CPUNodeEditor.h"
11#include "Base/ImGuiCurveEditor.h"
14static float fract(
float v)
24 res = sc = x = y = 0.0f;
27 if (inputPins[0]->IsLinked())
29 x = inputPins[0]->other->Evaluate(input).value;
37 if (inputPins[1]->IsLinked())
39 y = inputPins[1]->other->Evaluate(input).value;
47 if (inputPins[2]->IsLinked())
49 sc = inputPins[2]->other->Evaluate(input).value;
57 x = (x * 2.0f - 1.0f) * sc - posi[0];
58 y = (y * 2.0f - 1.0f) * sc - posi[1];
59 float cr = cos(rota * 3.1415926535f / 180.0f);
60 float sr = sin(rota * 3.1415926535f / 180.0f);
63 x = tx * cr - ty * sr;
64 y = tx * sr + ty * cr;
69 int xC = (int)(x * (heightmap->GetWidth()-1));
70 int yC = (int)(y * (heightmap->GetHeight()-1));
75 res = heightmap->Sample(x, y, interpolated) / 65536.0;
78 if(x > numTiles || y > numTiles || x < 0 || y < 0)
84 res = res * 2.0f - 1.0f;
98 scale = data[
"scale"];
100 if (isDefault && data[
"isDefault"])
105 isDefault = data[
"isDefault"];
106 interpolated = data[
"interpolated"];
107 npScale = data[
"npsc"];
109 autoTiled = data[
"autoTiled"];
110 numTiles = data[
"numTiles"];
111 posi[0] = data[
"posiX"];
112 posi[1] = data[
"posiY"];
118 heightmap =
new Heightmap(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"heightmaps" PATH_SEPARATOR
"flat.png");
123 std::string
hash = data[
"heightmap"];
125 if (!ProjectManager::Get()->AssetExists(hash))
127 ShowMessageBox(
"Failed to Load Heightmap : " + hash,
"Error");
134 heightmap =
new Heightmap(ProjectManager::Get()->GetResourcePath() + PATH_SEPARATOR + ProjectManager::Get()->GetAsset(hash));
135 Log(
"Loaded Cached Heightmap : " + hash);
143 data[
"type"] = MeshNodeEditor::MeshNodeType::Heightmap;
144 data[
"scale"] = scale;
145 data[
"isDefault"] = isDefault;
146 data[
"interpolated"] = interpolated;
148 data[
"npsc"] = npScale;
149 data[
"autoTiled"] = autoTiled;
150 data[
"numTiles"] = numTiles;
151 data[
"posiX"] = posi[0];
152 data[
"posiY"] = posi[1];
157 std::string
hash = MD5File(heightmap->GetPath()).ToString();
158 data[
"heightmap"] =
hash;
160 if (!ProjectManager::Get()->AssetExists(hash))
162 ProjectManager::Get()->SaveResource(
"heightmaps", heightmap->GetPath());
163 Log(
"Cached " + heightmap->GetPath());
168 Log(
"Heightmap already cached : " + hash);
175void HeightmapNode::OnRender()
177 DrawHeader(
"Heightmap");
178 inputPins[0]->Render();
181 ImGui::Dummy(ImVec2(150, 10));
182 ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
183 ImGui::Text(
"Height");
184 ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
185 outputPins[0]->Render();
186 inputPins[1]->Render();
188 inputPins[2]->Render();
190 if (inputPins[2]->IsLinked())
192 ImGui::Text(
"Scale");
197 ImGui::PushItemWidth(100);
198 ImGui::DragFloat((
"Scale##" + std::to_string(inputPins[1]->
id)).c_str(), &scale, 0.01f);
199 ImGui::PopItemWidth();
202 ImGui::Checkbox((
"Auto Tiled##tild" + std::to_string(
id)).c_str(), &autoTiled);
203 ImGui::Checkbox((
"Interpolated##intrp" + std::to_string(
id)).c_str(), &interpolated);
204 ImGui::Checkbox((
"Inverted##tinv" + std::to_string(
id)).c_str(), &inv);
205 ImGui::Checkbox((
"Scale -1 To 1##tnpsc" + std::to_string(
id)).c_str(), &npScale);
209 ImGui::PushItemWidth(100);
210 ImGui::DragFloat((
"Num Tiles##nmtl" + std::to_string(
id)).c_str(), &numTiles, 0.01f);
211 ImGui::PopItemWidth();
213 ImGui::DragFloat2((
"Position##posi" + std::to_string(
id)).c_str(), posi, 0.01f);
214 ImGui::PushItemWidth(100);
215 ImGui::DragFloat((
"Rotation##rota" + std::to_string(
id)).c_str(), &rota, 0.1f);
216 ImGui::PopItemWidth();
219 if (ImGui::ImageButton((
void*) heightmap->GetRendererID(), ImVec2(200, 200)))
224 if (ImGui::Button(MAKE_IMGUI_LABEL(
id,
"Change Heightmap")))
230void HeightmapNode::ChangeHeightmap()
232 std::string path = ShowOpenFileDialog(
".png");
242 if (heightmap->GetData())
244 Log(
"Changed Heightmap : " + heightmap->GetPath());
247 Log(
"Failed To Change Heightmap : " + heightmap->GetPath());
252HeightmapNode::~HeightmapNode()
260HeightmapNode::HeightmapNode()
265 outputPins.push_back(
new NodeEditorPin(NodeEditorPinType::Output));
266 headerColor = ImColor(IMAGE_NODE_COLOR);
267 heightmap =
new Heightmap(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"heightmaps" PATH_SEPARATOR
"flat.png");
276 posi[0] = posi[1] = 0.0f;
a class to store JSON values
std::size_t hash(const BasicJsonType &j)
hash a JSON value