In this lesson, you will learn how to bypass the limitations of pre-built Shader Graph nodes by embedding your own custom HLSL logic directly into the graph.
While the Shader Graph library is extensive, certain complex mathematical operations—such as custom trigonometry, conditional branching, or specific coordinate transformations—can become cluttered or inefficient when mapped out with hundreds of standard nodes. The Custom Function Node serves as a bridge, allowing you to inject raw HLSL code into your graph’s pipeline.
The Custom Function Node
When you add a Custom Function node to your graph, you have two primary ways to interface with it: String Mode and File Mode. For quick, standalone operations, "String Mode" is the most efficient.
Within the node’s settings, you define your inputs (the data flowing from other nodes) and outputs (the results you want to use elsewhere). You then write your code in the provided text field. For example, to perform a custom "smooth-step-like" manipulation that isn't standard, you could write:
void MyCustomMath_float(float3 Input, out float3 Out)
{
// A simple custom operation: squaring the input and normalizing
float3 val = Input * Input;
Out = normalize(val);
}
Key Considerations
- Naming Convention: Notice the function name
MyCustomMath_float. In Unity Shader Graph, the suffix_floator_halfis mandatory so the compiler knows the precision of the function. - Input/Output Matching: The parameters in your code snippet must match the names and types of the ports you defined in the Custom Function node settings. If you change a name in the UI, you must update it in the code block, or the node will fail to compile.
- Scope: Keep your code focused on the mathematical operation. Heavy logic or complex branching inside a shader can significantly impact performance, especially if used in the Fragment stage. By offloading this to a single node, you keep your graph readable and optimized.
Key Takeaway: The Custom Function node allows you to encapsulate complex HLSL algorithms into a single, reusable block, bridging the gap between graphical node-based workflow and high-performance custom shader programming.