docs.unity3d.com
    Show / Hide Table of Contents

    Custom Function Node

    01

    Description

    Shader Graph allows you to easily author custom shaders in Unity. However, because our default Nodes can't cover everything you want to make, the package now comes with a Custom Function node in the Node Library.

    The Custom Function node lets you inject your own custom HLSL code in Shader Graphs. You can either write small functions directly into graphs using the string mode, or reference external HLSL include files. You can also use the Custom Port Menu to define your own input and output ports on the node itself.

    How to Use

    Use the Create Node Menu to create Custom Function nodes. By default, new Custom Function nodes don't have any input or output ports.

    Use the Node Setttings of the Graph Inspector to open the Custom Port Menu and the Custom Function menu.

    02 03

    Custom Function Menu

    Menu Item Description
    Inputs A Custom Port Menu that defines the input ports of the node.
    Outputs A Custom Port Menu that defines the output ports of the node.
    Type A drop-down menu where you set the type of function. Select File to reference an external file, or string to directly input functions to the node.
    Name The name of the custom function in the final generated code.
    Source An asset field to reference the external HLSL include file. Only available in File mode.
    Body A text box where you enter HLSL code. Only available in String mode.

    Defining the Function via String

    If you select String mode, the graph generates the shader function. The Name field defines the name of the generated function, and the Body field defines the contents of the generated function. The arguments, braces, and indent scope are handled automatically. In String mode you may use the token $precision instead of half or float when filling out the Body field. This will be replaced by the correct type, based on that node's precision, when the node is processed.

    04

    The example above generates the following function:

    05

    Defining the Function via File

    If you select File mode, the graph does not automatically generate the shader function. File mode injects an include reference in the final generated shader, and uses a function from within the referenced file. The Name field must match the name of the function you wish to call. The Source field contains a reference to the HLSL file that includes the function.

    06

    When you use File mode for the Custom Function node, you must manually format the functions properly. One thing to note when creating custom functions for Shader Graph is the precision suffixes. The generated code appends a precision suffix to function names. Your include file function must also append your desired precision suffix (shown below with _float), or contain multiple functions with both _float and _half suffixes, but your Name field must not include the precision suffix.

    07

    File mode allows for more flexbility with custom functions in a graph. You can define uniform variables outside of the function scope, as shown here with a matrix.

    08

    You can define multiple functions in the same file, and call them from your referenced function. Alternatively, you can reference the same file, but use different functions from different Custom Function nodes.

    09

    You can even include other files that contain other functions.

    10

    Reusing Custom Function Nodes

    The Custom Function node, on its own, is a single node instance. If you wish to re-use the same custom functions without re-creating the inputs, outputs, and function referencing, use Sub Graphs. Sub Graphs appear in the Create Node Menu, and they allow you to share or re-use your custom functions.

    11

    Create your custom function either directly in a Sub Graph, or right-click the existing Custom Function node and select Convert to Sub Graph. Add the appropriate input and output ports using the Graph Inspector and Custom Port Menu. After this, you can reuse your custom function as many times as needed, even within other Sub Graphs.

    In This Article
    • Description
    • How to Use
      • Custom Function Menu
      • Defining the Function via String
      • Defining the Function via File
      • Reusing Custom Function Nodes
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023