1#include "Shading/ShaderNodes/ShaderTextureNode.h"
3#include "Utils/Utils.h"
4#include "Data/ProjectData.h"
12 GLSLFunction f(
"getTriplanarBlend",
"vec3 normal",
"vec3");
13 f.AddLine(
GLSLLine(
"vec3 blending = abs(normal);",
""));
14 f.AddLine(
GLSLLine(
"blending = normalize(max(blending, 0.000001));",
"Force weights to sum to 1.0"));
15 f.AddLine(
GLSLLine(
"float b = (blending.x + blending.y + blending.z);",
""));
16 f.AddLine(
GLSLLine(
"blending /= vec3(b);",
""));
17 f.AddLine(
GLSLLine(
"return blending;",
""));
23 handler->AddFunction(GetTriplanarBlendFunction());
24 function->AddLine(
GLSLLine(
"vec3 " + VAR(
"tex") +
";",
"Variable to store the result"));
25 function->AddLine(
GLSLLine(
"if(" + SDATA(4) +
" == 1.0)",
"If triplanar mapping is enabled"));
26 function->AddLine(
GLSLLine(
"{",
""));
28 "\tvec3 " + VAR(
"blending") +
" = getTriplanarBlend(Normal);\n"
29 "\t\tvec3 " + VAR(
"xaxis") +
" = texture(_Textures, vec3(FragPos.yz * " + SDATA(0) +
" + vec2(" + SDATA(1) +
", " + SDATA(2) +
"), " + STR(zCoord) +
")).rgb;\n"
30 "\t\tvec3 " + VAR(
"yaxis") +
" = texture(_Textures, vec3(FragPos.xz * " + SDATA(0) +
" + vec2(" + SDATA(1) +
", " + SDATA(2) +
"), " + STR(zCoord) +
")).rgb;\n"
31 "\t\tvec3 " + VAR(
"zaxis") +
" = texture(_Textures, vec3(FragPos.xy * " + SDATA(0) +
" + vec2(" + SDATA(1) +
", " + SDATA(2) +
"), " + STR(zCoord) +
")).rgb;\n"
32 "\t" + VAR(
"tex") +
" = " + VAR(
"blending") +
".x * " + VAR(
"xaxis") +
" + " + VAR(
"blending") +
".y * " + VAR(
"yaxis") +
" + " + VAR(
"blending") +
".z * " + VAR(
"zaxis") +
";\n"
34 function->AddLine(
GLSLLine(
"}",
""));
35 function->AddLine(
GLSLLine(
"else",
"If texture is not triplanar mapped just sample texture normally"));
36 function->AddLine(
GLSLLine(
"{",
""));
38 "\t" + VAR(
"tex") +
" = texture(_Textures, vec3(TexCoord * " + SDATA(0) +
" + vec2(" + SDATA(1) +
", " + SDATA(2) +
"), " + STR(zCoord) +
")).rgb;\n"));
39 function->AddLine(
GLSLLine(
"}",
""));
40 line->line = VAR(
"tex");
45 if(texture !=
nullptr)
51 texture =
new Texture2D(ProjectManager::Get()->GetResourcePath() + PATH_SEPARATOR + ProjectManager::Get()->GetAsset(data[
"texture"]));
52 scale = data[
"scale"];
53 offsetX = data[
"offsetX"];
54 offsetY = data[
"offsetY"];
55 rotation = data[
"rotation"];
56 isTriplanar = data[
"isTriplanar"];
62 data[
"type"] =
"ShaderTexture";
63 data[
"scale"] = scale;
64 data[
"offsetX"] = offsetX;
65 data[
"offsetY"] = offsetY;
66 data[
"rotation"] = rotation;
67 data[
"isTriplanar"] = isTriplanar;
68 data[
"texture"] = ProjectManager::Get()->SaveTexture(texture);
72void ShaderTextureNode::UpdateShaders()
74 sharedData->d0 = scale;
75 sharedData->d1 = offsetX;
76 sharedData->d2 = offsetY;
77 sharedData->d3 = rotation;
78 sharedData->d4 = isTriplanar ? 1.0f : 0.0f;
81void ShaderTextureNode::OnRender()
83 DrawHeader(
"Texture");
85 if(ImGui::ImageButton((ImTextureID)texture->GetRendererID(), ImVec2(150, 150)))
87 std::string path = ShowOpenFileDialog(
"*.png\0*.jpg\0");
97 textureManager->UploadToGPU(zCoord);
101 if(ImGui::BeginDragDropTarget())
103 if(
const ImGuiPayload *payload = ImGui::AcceptDragDropPayload(
"TerraForge3D_Texture"))
105 std::string path = std::string((
char*)payload->Data);
114 textureManager->UploadToGPU(zCoord);
118 ImGui::EndDragDropTarget();
122 outputPins[0]->Render();
123 ImGui::PushItemWidth(100);
125 if(ImGui::DragFloat(
"Scale", &scale, 0.01f))
127 sharedData->d0 = scale;
130 if(ImGui::DragFloat(
"Offset X", &offsetX, 0.01f))
132 sharedData->d1 = offsetX;
135 if(ImGui::DragFloat(
"Offset Y", &offsetY, 0.01f))
137 sharedData->d2 = offsetY;
140 if(ImGui::DragFloat(
"Rotation", &rotation, 0.01f))
142 sharedData->d3 = rotation;
145 if(ImGui::Checkbox(
"Triplanar Mapping", &isTriplanar))
147 sharedData->d4 = isTriplanar ? 1.0f : 0.0f;
150 ImGui::PopItemWidth();
154 :
SNENode(handler), textureManager(textureManager)
157 headerColor = ImColor(SHADER_TEXTURE_NODE_COLOR);
158 outputPins.push_back(
new SNEPin(NodeEditorPinType::Output, SNEPinType::SNEPinType_Float3));
159 texture =
new Texture2D(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"textures" PATH_SEPARATOR
"white.png");
160 textureManager->Register(
this);
164ShaderTextureNode::~ShaderTextureNode()
166 textureManager->Unregister(
this);
168 if(texture !=
nullptr)
a class to store JSON values