Uploaded December 2018 | Updated November 2026, 2 days ago
With the introduction of Shader Graph Unity also added support for Custom Nodes you can build yourself by creating custom `CodeFunctionNode`. By extending this class we're able to create our own Nodes in Shader Graph that we can extend or implement in any way. In our example here we'll be creating a very simple linear interpolation between two colors, effectively reimplementing the existing `Lerp` or `Branch` nodes depending on what you are looking for.
The cool thing about this is it allows you to really easily extend Shader Graph with cool new functions, add missing features and more. I don't think this API is quite to the level of allowing you to add support for Tessellation or Geometry Shaders (that doesn't seem to be the goal here), instead it makes a somewhat accessible way for you to extend your tooling and help expand it.
That said, there are some issues here. Nodes that no longer compile are removed from the graph which feels a bit... awkward. And the default API relies on reflection to function completely. I feel like this could have been wrapped in many of the modern dependency injection style of models without requiring reflection as a significant part of the API. Still, it's nice to be available.
The code developed during this video is available here: gist.github.com/runewake2/68423db7c32d80993333a1c932f032d1
The official blog post on Shader Graph Custom Nodes can be found here: blogs.unity3d.com/2018/03/27/shader-graph-custom-node-api-using-the-code-function-node
Unity goes into even more depth on the CodeFunctionNode on their GitHub page: github.com/Unity-Technologies/ShaderGraph/wiki/Custom-Nodes-With-CodeFunctionNode
Join the World of Zero Discord channel: discord.gg/hU5Kq2u
With the introduction of Shader Graph Unity also added support for Custom Nodes you can build yourself by creating custom `CodeFunctionNode`. By extending this class we're able to create our own Nodes in Shader Graph that we can extend or implement in any way. In our example here we'll be creating a very simple linear interpolation between two colors, effectively reimplementing the existing `Lerp` or `Branch` nodes depending on what you are looking for.
The cool thing about this is it allows you to really easily extend Shader Graph with cool new functions, add missing features and more. I don't think this API is quite to the level of allowing you to add support for Tessellation or Geometry Shaders (that doesn't seem to be the goal here), instead it makes a somewhat accessible way for you to extend your tooling and help expand it.
That said, there are some issues here. Nodes that no longer compile are removed from the graph which feels a bit... awkward. And the default API relies on reflection to function completely. I feel like this could have been wrapped in many of the modern dependency injection style of models without requiring reflection as a significant part of the API. Still, it's nice to be available.
The code developed during this video is available here: gist.github.com/runewake2/68423db7c32d80993333a1c932f032d1
The official blog post on Shader Graph Custom Nodes can be found here: blogs.unity3d.com/2018/03/27/shader-graph-custom-node-api-using-the-code-function-node
Unity goes into even more depth on the CodeFunctionNode on their GitHub page: github.com/Unity-Technologies/ShaderGraph/wiki/Custom-Nodes-With-CodeFunctionNode
Join the World of Zero Discord channel: discord.gg/hU5Kq2u

 for recommending the use of `ContextMenu`s. You can find more of their content as a guest in videos at Infallible Code: https://www.youtube.com/user/charlesamat
### Code Examples
Some example uses of Unity 3Ds `ContextMenu` and `ContextMenuItem` Attributes:
#### `ContextMenu`
A `ContextMenu` Attribute is attached to functions in your `MonoBehavior`. When that menu item is clicked it will invoke the function the attribute is assigned to:
```csharp
[ContextMenu(Context Menu Example)]
void HelloContextMenu() {
Debug.Log(Context Menu Clicked!);
}
```
#### `ContextMenuItem`
A `ContextMenuItem` Attribute is attached to a field and requires you to provide the name of a function that will be invoked when the menu item is clicked.
```csharp
[ContextMenuItem(Context Menu Item Example, nameof(InvokeContextMenuItem))]
public float someVariable;
void InvokeContextMenuItem() {
Debug.Log(Context Menu Item Clicked!);
}
```
[`nameof`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof) is a C# function that returns a string representation for the name of the class, function, field, property etc that is referenced. Using this allows things like refactorings to function better and provides compiler errors if you spell a function name incorrectly (`nameof(FunctionName)` will throw an error if `FunctionName` is not defined).
Note: The functions invoked by both `ContextMenu` and `ContextMenuItem` should not have any arguments.
***
Unitys documentation on the `ContextMenu` Attribute: https://docs.unity3d.com/ScriptReference/ContextMenu.html
Unitys documentation on the `ContextMenuItem` Attribute: https://docs.unity3d.com/ScriptReference/ContextMenuItemAttribute.html
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using ContextMenu and ContextMenuItem in Unity 3D](https://i.ytimg.com/vi/GKG-vUFt3D4/mqdefault.jpg)


![Building a Custom Dice Model with OpenSCAD
OpenSCAD is a tool that can compile programs into 3D models allowing you to code your own CAD designs instead of building them inside of a WYSIWYG type editor like Fusion 360, SketchUp etc.
The module we created for a basic dice looks like this:
```OpenSCAD
module CreateDice(size = 16) {
textSize = size * 0.75;
textLocation = size / 2 - 1;
textExtrusionSize = 2;
difference() {
cube([size, size, size], center = true);
translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(1, size=textSize, halign=center, valign=center);
rotate([180, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(6, size=textSize, halign=center, valign=center);
rotate([0, 90, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(2, size=textSize, halign=center, valign=center);
rotate([0, 270, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(5, size=textSize, halign=center, valign=center);
rotate([90, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(3, size=textSize, halign=center, valign=center);
rotate([270, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(4, size=textSize, halign=center, valign=center);
}
}
```
Get OpenSCAD: https://openscad.org/index.html
OpenSCADs documentation: https://openscad.org/documentation.html
OpenSCAD on GitHub: https://github.com/openscad/openscad/
You can get a copy of my models and the SCAD files that generate them here: https://github.com/runewake2/scad-examples
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Building a Custom Dice Model with OpenSCAD](https://i.ytimg.com/vi/HxiyUi4RQzw/mqdefault.jpg)





