Add Custom Global Code node to materials for injecting custom code, includes or constants
This commit is contained in:
@@ -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),
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -7,10 +7,6 @@
|
||||
#include "Engine/Core/Enums.h"
|
||||
#include "Engine/Visject/ShaderGraph.h"
|
||||
|
||||
class MaterialGraphNode : public ShaderGraphNode<>
|
||||
{
|
||||
};
|
||||
|
||||
class MaterialGraph : public ShaderGraph<>
|
||||
{
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user