1#include "Generators/CPUNodeEditor/Nodes/TextureNode.h"
2#include "Utils/Utils.h"
3#include "Data/ProjectData.h"
4#include "Base/Texture2D.h"
5#include "Base/ImGuiShapes.h"
6#include "Generators/CPUNodeEditor/CPUNodeEditor.h"
11#include "Base/ImGuiCurveEditor.h"
14#define CLAMP01(x) x > 1 ? 1 : ( x < 0 ? 0 : x )
16static float fract(
float v)
26 res = sc = x = y = 0.0f;
29 if (pin->id == outputPins[0]->id)
34 else if (pin->id == outputPins[1]->id)
39 else if (pin->id == outputPins[2]->id)
44 if (inputPins[0]->IsLinked())
46 x = inputPins[0]->other->Evaluate(input).value;
54 if (inputPins[1]->IsLinked())
56 y = inputPins[1]->other->Evaluate(input).value;
64 if (inputPins[2]->IsLinked())
66 sc = inputPins[2]->other->Evaluate(input).value;
74 x = (x * 2.0f - 1.0f) * sc - posi[0];
75 y = (y * 2.0f - 1.0f) * sc - posi[1];
76 float cr = cos(rota * 3.1415926535f / 180.0f);
77 float sr = sin(rota * 3.1415926535f / 180.0f);
80 x = tx * cr - ty * sr;
81 y = tx * sr + ty * cr;
87 if(x > numTiles || y > numTiles || x < 0 || y < 0)
94 int xC = (int)(x * (texture->GetWidth()-1));
95 int yC = (int)(y * (texture->GetHeight()-1));
96 unsigned char elevC = texture->GetData()[yC * texture->GetWidth() * 3 + xC * 3 + channel];
97 res = (float)elevC / 256;
101 res = res * 2.0f - 1.0f;
115 scale = data[
"scale"];
117 if (isDefault && data[
"isDefault"])
122 isDefault = data[
"isDefault"];
123 npScale = data[
"npsc"];
125 autoTiled = data[
"autoTiled"];
126 numTiles = data[
"numTiles"];
127 posi[0] = data[
"posiX"];
128 posi[1] = data[
"posiY"];
134 texture =
new Texture2D(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"textures" PATH_SEPARATOR
"white.png",
false,
false);
139 std::string
hash = data[
"texture"];
141 if (!ProjectManager::Get()->AssetExists(hash))
143 ShowMessageBox(
"Failed to Load Texture : " + hash,
"Error");
150 texture =
new Texture2D(ProjectManager::Get()->GetResourcePath() + PATH_SEPARATOR + ProjectManager::Get()->GetAsset(hash));
151 Log(
"Loaded Cached Texture : " + hash);
161 data[
"type"] = MeshNodeEditor::MeshNodeType::Texture;
162 data[
"scale"] = scale;
163 data[
"isDefault"] = isDefault;
165 data[
"npsc"] = npScale;
166 data[
"autoTiled"] = autoTiled;
167 data[
"numTiles"] = numTiles;
168 data[
"posiX"] = posi[0];
169 data[
"posiY"] = posi[1];
174 std::string
hash = MD5File(texture->GetPath()).ToString();
175 data[
"texture"] =
hash;
177 if (!ProjectManager::Get()->AssetExists(hash))
179 ProjectManager::Get()->SaveTexture(texture);
180 Log(
"Cached " + texture->GetPath());
185 Log(
"Texture already Cached : " + hash);
192void TextureNode::OnRender()
194 DrawHeader(
"Texture");
195 inputPins[0]->Render();
198 ImGui::Dummy(ImVec2(150, 10));
201 outputPins[0]->Render();
202 inputPins[1]->Render();
205 ImGui::Dummy(ImVec2(150, 10));
208 outputPins[1]->Render();
209 inputPins[2]->Render();
211 if (inputPins[2]->IsLinked())
213 ImGui::Text(
"Scale");
218 ImGui::PushItemWidth(100);
219 ImGui::DragFloat((
"##" + std::to_string(inputPins[1]->
id)).c_str(), &scale, 0.01f);
220 ImGui::PopItemWidth();
224 ImGui::Dummy(ImVec2(60, 10));
227 outputPins[2]->Render();
229 ImGui::Checkbox((
"Auto Tiled##tild" + std::to_string(
id)).c_str(), &autoTiled);
230 ImGui::Checkbox((
"Inverse Texture##tinv" + std::to_string(
id)).c_str(), &inv);
231 ImGui::Checkbox((
"Scale -1 To 1##tnpsc" + std::to_string(
id)).c_str(), &npScale);
234 ImGui::PushItemWidth(100);
237 ImGui::DragFloat((
"Num Tiles##nmtl" + std::to_string(
id)).c_str(), &numTiles, 0.01f);
239 ImGui::DragFloat2((
"Position##posi" + std::to_string(
id)).c_str(), posi, 0.01f);
240 ImGui::PopItemWidth();
241 ImGui::DragFloat((
"Rotation##rota" + std::to_string(
id)).c_str(), &rota, 0.1f);
244 if (ImGui::ImageButton((ImTextureID)texture->GetRendererID(), ImVec2(200, 200)))
249 if (ImGui::Button(MAKE_IMGUI_LABEL(
id,
"Change Texture")))
255void TextureNode::ChangeTexture()
257 std::string path = ShowOpenFileDialog(
".png");
266 texture =
new Texture2D(path,
true,
false);
267 texture->Resize(256, 256);
268 Log(
"Loaded Texture : " + texture->GetPath());
272TextureNode::~TextureNode()
280TextureNode::TextureNode()
285 outputPins.push_back(
new NodeEditorPin(NodeEditorPinType::Output));
286 outputPins.push_back(
new NodeEditorPin(NodeEditorPinType::Output));
287 outputPins.push_back(
new NodeEditorPin(NodeEditorPinType::Output));
288 headerColor = ImColor(IMAGE_NODE_COLOR);
289 texture =
new Texture2D(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"textures" PATH_SEPARATOR
"white.png",
false,
false);
297 posi[0] = posi[1] = 0.0f;
a class to store JSON values
std::size_t hash(const BasicJsonType &j)
hash a JSON value