Skip to content

Instantly share code, notes, and snippets.

@runewake2
Created December 5, 2018 01:23
Show Gist options
  • Save runewake2/68423db7c32d80993333a1c932f032d1 to your computer and use it in GitHub Desktop.
Save runewake2/68423db7c32d80993333a1c932f032d1 to your computer and use it in GitHub Desktop.
An example of Unity Shader Graph Custom Node's designed and developed on World of Zero: https://youtu.be/FeL4uWGdtUY
// Custom Shader Graph Node for Unity
// Developed for World of Zero: https://youtu.be/FeL4uWGdtUY
// Based on examples from: https://github.com/Unity-Technologies/ShaderGraph/wiki/Custom-Nodes-With-CodeFunctionNode
using UnityEngine;
using UnityEditor.ShaderGraph;
using System.Reflection;
// The Title is a list of menu's and submenu's that will define where this node is placed in Unity's Shader Graph UI
[Title("Custom", "Binary Select (Menu)")]
public class CustomNode : CodeFunctionNode {
public CustomNode() {
// Define the name of the node (this is shown on existing nodes)
name = "Binary Select";
}
protected override MethodInfo GetFunctionToConvert()
{
// Use Reflection to find the BinarySelectFunction below to pass it to Unity's API
// The use of "BinarySelectFunction" here can be swapped for nameof(BinarySelectFunction) in higher versions of .NET
return GetType().GetMethod("BinarySelectFunction",
BindingFlags.Static | BindingFlags.NonPublic);
}
// Define a static function that returns the generated shader code based on the set of predefined inputs and outputs.
static string BinarySelectFunction(
[Slot(0, Binding.None)] Vector1 Input,
[Slot(1, Binding.None)] DynamicDimensionVector Result0,
[Slot(2, Binding.None)] DynamicDimensionVector Result1,
[Slot(3, Binding.None)] out DynamicDimensionVector Out
)
{
// Takes advantage of C#'s verbatim string feature to preserve whitespace and newlines.
return @"
{
Out = ((1 - Input) * Result0) + (Input * Result1);
}
";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment