Hi,
I’ve read here and there users asking for ways to create a new HLSL file from within the Editor, and for more documentation on Custom Function Nodes.
I’d like then to share some tips and tricks to help you create new HLSL files quickly.
Creating an empty HLSL file
First of all, if you’re looking for a way to create a new empty .hlsl file in your project, there’s one option as part of VFX Graph.
Simply go to Assets/Create/Visual Effects/HLSL file.
HLSL Templates package
Now if you want to create an HLSL file to use with a Custom Function Node, you might want to take a look at this HLSL Templates package.
It adds some HLSL creation items to Assets/Create/HLSL/. and contains a few templates.
Disclaimer: this is something I did on my spare time and is in no way supported by Unity.
Adding your own HLSL Templates
Also, should you want to add your own templates to the Editor or your Project, here’s what you can do.
The Editor actually provides a way for users to add templates for any text based assets.
Start by creating a new text file and name it using this convention:
[order]-[menu__menu item name]-[default filename].hlsl.txt
For example, if you name your file:
100000-Shader Graph__Custom Function Node HLSL-NewCustomFunction.hlsl.txt
This will put the menu item at the very end (100000 is a pretty high order).
The menu item will show at Assets/Create/Shader Graph/Custom Function Node.
The double underscore __ make for creating sub menus. They’re like a slash / in the path.
And default filename will be NewCustomFunction.
Edit the file and format it like this, where #SCRIPTNAME# will be automatically replaced by the name of the file upon creation:
#ifndef #SCRIPTNAME#_DEFINE
#define #SCRIPTNAME#_DEFINE
void #SCRIPTNAME#_float(float3 Color, float Multiplier, out float3 Result)
{
#if defined(SHADERGRAPH_PREVIEW)
#else
#endif
Result = Color * Multiplier;
}
void #SCRIPTNAME#_half(half3 Color, half Multiplier, out half3 Result)
{
#if defined(SHADERGRAPH_PREVIEW)
#else
#endif
Result = Color * Multiplier;
}
It’s good practice to test your hlsl file prior to making it a template.
Now you can put it in your project Assets folder, in a folder named ScriptTemplates.
This has the advantage of being shared with other project team members.
Or you can put it in your Editor installation folder.
On Windows: save your file in %EDITOR_PATH%\Data\Resources\ScriptTemplates.
On Mac:
- locate your Unity application bundle (default is Applications/Unity/Hub/Editor/version/Unity.app).
- right-click on Unity.app and select Show Package Contents.
- save your template file in Contents/Resources/ScriptTemplates.
In both cases, you’ll need to restart the Editor for the menu items to show up.
You can read more about custom templates here.
For more information on how to format Custom Function Node HLSL code, the documentation features some examples.
I hope this is helpful. Please use this thread to share your feedback.
I’ll post more Custom Function Node samples in the future.
Thanks!