Add Custom Global Code node to materials for injecting custom code, includes or constants

This commit is contained in:
Wojtek Figat
2022-04-15 17:04:28 +02:00
parent 991abb1cf8
commit 493787d4d6
7 changed files with 43 additions and 13 deletions

View File

@@ -192,7 +192,7 @@ namespace FlaxEditor.Surface.Archetypes
GetBox(MaterialNodeBoxes.SubsurfaceColor).Enabled = false;
break;
}
default: throw new ArgumentOutOfRangeException();
default: throw new ArgumentOutOfRangeException();
}
}
@@ -807,7 +807,26 @@ namespace FlaxEditor.Surface.Archetypes
NodeElementArchetype.Factory.Input(0, "RGB", true, typeof(Vector3), 0, 0),
NodeElementArchetype.Factory.Output(0, "HSV", typeof(Vector3), 1),
}
}
},
new NodeArchetype
{
TypeID = 38,
Title = "Custom Global Code",
Description = "Custom global HLSL shader code expression (placed before material shader code). Can contain includes to shader utilities or declare functions to reuse later.",
Flags = NodeFlags.MaterialGraph,
Size = new Vector2(300, 220),
DefaultValues = new object[]
{
"// Here you can add HLSL code\nfloat4 GetCustomColor()\n{\n\treturn float4(1, 0, 0, 1);\n}",
true,
},
Elements = new[]
{
NodeElementArchetype.Factory.Bool(0, 0, 1),
NodeElementArchetype.Factory.Text(20, 0, "Enabled"),
NodeElementArchetype.Factory.TextBox(0, 20, 300, 200, 0),
}
},
};
}
}

View File

@@ -123,7 +123,7 @@ CreateAssetResult CreateMaterial::Create(CreateAssetContext& context)
if (context.AllocateChunk(SHADER_FILE_CHUNK_VISJECT_SURFACE))
return CreateAssetResult::CannotAllocateChunk;
layer->Graph.Nodes.EnsureCapacity(32);
layer->Root = (MaterialGraphNode*)&layer->Graph.Nodes[0];
layer->Root = &layer->Graph.Nodes[0];
for (auto& box : layer->Root->Boxes)
box.Parent = layer->Root;
Meta11 meta;

View File

@@ -9,7 +9,7 @@ MaterialValue* MaterialGenerator::sampleTextureRaw(Node* caller, Value& value, B
ASSERT(texture && box);
// Cache data
const auto parent = box->GetParent<MaterialGraphNode>();
const auto parent = box->GetParent<ShaderGraphNode<>>();
const bool isCubemap = texture->Type == MaterialParameterType::CubeTexture;
const bool isArray = texture->Type == MaterialParameterType::GPUTextureArray;
const bool isVolume = texture->Type == MaterialParameterType::GPUTextureVolume;

View File

@@ -163,7 +163,7 @@ bool MaterialGenerator::Generate(WriteStream& source, MaterialInfo& materialInfo
// Cache data
MaterialLayer* baseLayer = GetRootLayer();
MaterialGraphNode* baseNode = baseLayer->Root;
auto* baseNode = baseLayer->Root;
_treeLayerVarName = baseLayer->GetVariableName(nullptr);
_treeLayer = baseLayer;
_graphStack.Add(&_treeLayer->Graph);
@@ -492,6 +492,21 @@ bool MaterialGenerator::Generate(WriteStream& source, MaterialInfo& materialInfo
// Utilities
{
WRITE_FEATURES(Utilities);
Array<Graph*, InlinedAllocation<8>> graphs;
_functions.GetValues(graphs);
for (MaterialLayer* layer : _layers)
graphs.Add(&layer->Graph);
for (Graph* graph : graphs)
{
for (const MaterialGraph::Node& node : graph->Nodes)
{
if (node.Type == GRAPH_NODE_MAKE_TYPE(1, 38) && (bool)node.Values[1])
{
// Custom Global Code
_writer.Write((StringView)node.Values[0]);
}
}
}
inputs[In_Utilities] = _writer.ToString();
_writer.Clear();
}

View File

@@ -50,7 +50,7 @@ void MaterialLayer::Prepare()
const auto node = &Graph.Nodes[i];
if (node->Type == ROOT_NODE_TYPE)
{
Root = (MaterialGraphNode*)node;
Root = node;
break;
}
}
@@ -159,7 +159,7 @@ MaterialLayer* MaterialLayer::Load(const Guid& id, ReadStream* graphData, const
{
if (layer->Graph.Nodes[i].Type == ROOT_NODE_TYPE)
{
layer->Root = (MaterialGraphNode*)&layer->Graph.Nodes[i];
layer->Root = &layer->Graph.Nodes[i];
break;
}
}
@@ -233,7 +233,7 @@ void MaterialLayer::createRootNode()
#undef INIT_BOX
// Mark as root
Root = (MaterialGraphNode*)&rootNode;
Root = &rootNode;
}
#endif

View File

@@ -50,7 +50,7 @@ public:
/// <summary>
/// Root node
/// </summary>
MaterialGraphNode* Root;
MaterialGraph::Node* Root;
/// <summary>
/// Material structure variable name (different for every layer sampling with different UVs, default UVs are a first index)

View File

@@ -7,10 +7,6 @@
#include "Engine/Core/Enums.h"
#include "Engine/Visject/ShaderGraph.h"
class MaterialGraphNode : public ShaderGraphNode<>
{
};
class MaterialGraph : public ShaderGraph<>
{
};