Unity PolySpatial Custom Function Node Support
PolySpatial's shader graph conversion provides partial support for the Custom Function Node (when using the String function type) by parsing a limited subset of HLSL and converting it into MaterialX nodes. Note that this does not allow for any functionality beyond that which is possible by combining built-in shader graph nodes; it simply provides an alternate means of specification that may be more compact or convenient for some purposes.
The parser used for custom function nodes is primitive compared to a full HLSL implementation, and is under active development.
Function Body Format
Custom function bodies should consist of a series of assignment statements for either temporary variables or outputs. Variables may not be reassigned.
Example
The following shows an example of a supported function body. Note that the body consists only of assignment statements, assigning one temporary variable and using it to set the value of an output.
float2 xy = float2(In.r * In.a, In.g) * 2.0 - 1.0;
Out = float3(xy, max(1.0e-16, sqrt(1.0 - saturate(dot(xy, xy)))));
Supported Types
The parser recognizes scalar and vector floating point types: float, float2, float3, float4, float2x2, float3x3, float4x4
Supported Operators
The parser supports basic arithmetic operators: +, -, *, /, %
Swizzling
Swizzling vector values is supported. For example, float3(1, 2, 3).xz is equivalent to float2(1, 3).
Supported Functions
HLSL Intrinsic Functions
The parser supports a subset of HLSL's intrinsic functions: abs, acos, asin, atan, atan2, ceil, clamp, cos, cross, degrees, distance, dot, exp, floor, fmod, frac, length, lerp, log, max, min, mul, normalize, pow, radians, rcp, reflect, round, rsqrt, saturate, sign, sin, smoothstep, sqrt, step, tan, transpose
PolySpatial-Specific Functions
Additionally, the parser supports the custom splitlr function, which implements the splitlr function described in the MaterialX Spec:
genType splitlr(genType valuel, genType valuer, float center, float2 texcoord);
(where genType is one of float, float2, float3, or float4)
Unity Macros
For sampling textures, the parser supports Unity macros: SAMPLE_TEXTURE2D, SAMPLE_TEXTURE2D_LOD
Supported Globals
The parser supports several of Unity's built-in global variables: _Time, _SinTime, _CosTime, unity_DeltaTime