Skip to main content
 首页 » 资源教程 » UE4教程

UE4 Material Custom Expressions

2016年10月12日 22:53:52950


The Custom expression allows you to write custom HLSL shader code operating on an arbitrary amount of inputs and outputting the result of the operation.



CodeContains the shader code the expression will execute. (See warnings below)
Output TypeSpecifies the type of the value output by the expression.
DescriptionSpecifies the text to display in the title bar of the expression in the Material Editor.
InputsThe array of inputs used by the expression.

Input Name

Specifies the name of the input. This is the name displayed on the expression in the Material Editor as well as the name used within the HLSL code to reference the input's value.

Add as many inputs as you need to the Inputs array, and name them. You can then write code in the Code property. You can type either a full function body with return statements as shown in the example, or a simple expression such as Input.bgr. You must also specify the output data type in OutputType.

 UE4 Material Custom Expressions UE4教程

Here is the code that was used above so you can try out the Custom node for yourself.

float3 blur = Texture2DSample(Tex, TexSampler, UV);for (int i = 0; i < r; i++){

  blur += Texture2DSample(Tex, TexSampler, UV + float2(i * dist, 0));
  blur += Texture2DSample(Tex, TexSampler, UV - float2(i * dist, 0));}for (int j = 0; j < r; j++){ 

  blur += Texture2DSample(Tex, TexSampler, UV + float2(0, j * dist));
  blur += Texture2DSample(Tex, TexSampler, UV - float2(0, j * dist));}blur /= 2*(2*r)+1;return blur;

Using the custom node prevents constant folding and may use significantly more instructions than an equivalent version done with built in nodes! Constant folding is an optimization that UE4 employs under the hood to reduce shader instruction count when necessary. For example, an expression chain of Time >Sin >Mul by parameter > Add to something' can and will be collapsed by UE4 into a single instruction, the final add. This is possible because all of the inputs of that expression (Time, parameter) are constant for the whole draw call, they do not change per-pixel. UE4 cannot collapse anything in a custom node, which can produce less efficient shaders than an equivalent version made out of existing nodes. As a result, it is best to only use the custom node when it gives you access to functionality not possible with the existing nodes.

Shader code written in a custom node is compiled 'as is' for the target platform. This means that if the shader is being compiled for PC, it is assumed to be valid HLSL. If compiling for PS3, it is assumed to be valid Cg.