TerraForge3D  2.3.1
3D Terrain And Landscape Generator
PixelateNode.cpp
1#include "Generators/CPUNodeEditor/Nodes/PixelateNode.h"
2#include "Base/ImGuiShapes.h"
3#include "Generators/CPUNodeEditor/CPUNodeEditor.h"
4#include <iostream>
5
6#include <mutex>
7#include "Base/ImGuiCurveEditor.h"
8
9NodeOutput PixelateNode::Evaluate(NodeInputParam input, NodeEditorPin *pin)
10{
11 float x = 1;
12
13 if (input.maxZ == 0)
14 {
15 input.maxZ = 1;
16 }
17
18 float currX = input.x / input.maxX;
19 float currY = input.y / input.maxY;
20 float currZ = input.z / input.maxZ;
21 float boxX = floorf(currX / pixelSize);
22 float boxY = floorf(currY / pixelSize);
23 float boxZ = floorf(currZ / pixelSize);
24 input.x = boxX * pixelSize * input.maxX;
25 input.y = boxY * pixelSize * input.maxY;
26 input.z = boxZ * pixelSize * input.maxZ;
27
28 if(inputPins[0]->IsLinked())
29 return NodeOutput({ inputPins[0]->other->Evaluate(input).value });
30 return NodeOutput({0.0f});
31}
32
33void PixelateNode::Load(nlohmann::json data)
34{
35 pixelSize = data["pixelSize"];
36}
37
38nlohmann::json PixelateNode::Save()
39{
40 nlohmann::json data;
41 data["type"] = MeshNodeEditor::MeshNodeType::Pixelate;
42 data["pixelSize"] = pixelSize;
43 return data;
44}
45
46void PixelateNode::OnRender()
47{
48 DrawHeader("Pixelate Node");
49 inputPins[0]->Render();
50 ImGui::Text("In");
51 inputPins[1]->Render();
52
53 if (inputPins[1]->IsLinked())
54 {
55 ImGui::Text("Pixel Size");
56 }
57
58 else
59 {
60 ImGui::PushItemWidth(100);
61 ImGui::DragFloat(("##" + std::to_string(inputPins[0]->id)).c_str(), &pixelSize, 0.01f, 0.001f, 1);
62 ImGui::PopItemWidth();
63 }
64
65 ImGui::SameLine();
66 ImGui::Text("Out");
67 outputPins[0]->Render();
68 ImGui::Text("Pixelates the input");
69}
70
71PixelateNode::PixelateNode()
72{
73 inputPins.push_back(new NodeEditorPin());
74 inputPins.push_back(new NodeEditorPin());
75 outputPins.push_back(new NodeEditorPin(NodeEditorPinType::Output));
76 headerColor = ImColor(OP_NODE_COLOR);
77 pixelSize = 0.05f;
78}
a class to store JSON values
Definition: json.hpp:17860