UE4 Material Custom Expressions

来源:互联网 发布:屏幕录像软件ios 编辑:程序博客网 时间:2024/06/03 15:49

Custom

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.

ItemDescription

Properties

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.

T_Custom_Node.png

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.

0 0
原创粉丝点击