This commit is contained in:
2026-11-24 15:51:02 +01:00
parent cca4d73e41
commit f4aa17114a
22092 changed files with 5042406 additions and 0 deletions
@@ -0,0 +1,526 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.Rendering;
using UnityEngine.UIElements;
using UnityEditor.ShaderGraph.Drawing;
using System.Text;
namespace UnityEditor.ShaderGraph
{
[HasDependencies(typeof(MinimalCustomFunctionNode))]
[Title("Utility", "Custom Function")]
class CustomFunctionNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IMayRequireTransform, IHasAssetDependencies
{
// 0 original version
// 1 differentiate between struct-based UnityTexture2D and bare Texture2D resources (for all texture and samplerstate resources)
public override int latestVersion => 1;
public override IEnumerable<int> allowedNodeVersions => new int[] { 1 };
public override string documentationURL => NodeUtils.GetDocumentationString("Custom Function");
[Serializable]
public class MinimalCustomFunctionNode : IHasDependencies
{
[SerializeField]
HlslSourceType m_SourceType = HlslSourceType.File;
[SerializeField]
string m_FunctionName = k_DefaultFunctionName;
[SerializeField]
string m_FunctionSource = null;
public void GetSourceAssetDependencies(AssetCollection assetCollection)
{
if (m_SourceType == HlslSourceType.File)
{
m_FunctionSource = UpgradeFunctionSource(m_FunctionSource);
if (IsValidFunction(m_SourceType, m_FunctionName, m_FunctionSource, null))
{
if (GUID.TryParse(m_FunctionSource, out GUID guid))
{
// as this is just #included into the generated .shader file
// it doesn't actually need to be a dependency, other than for export package
assetCollection.AddAssetDependency(guid, AssetCollection.Flags.IncludeInExportPackage);
}
}
}
}
}
enum SourceFileStatus
{
Empty, // No File specified
DoesNotExist, // Either file doesn't exist (empty name) or guid points to a non-existant file
Invalid, // File exists but isn't of a valid type (such as wrong extension)
Valid
};
// With ShaderInclude asset type, it should no longer be necessary to soft-check the extension.
public static HashSet<string> s_ValidExtensions = new HashSet<string>() { ".hlsl", ".cginc", ".cg" };
const string k_InvalidFileType = "Source file is not a valid file type. Valid file extensions are .hlsl, .cginc, and .cg";
const string k_MissingFile = "Source file does not exist. A valid .hlsl, .cginc, or .cg file must be referenced";
const string k_MissingOutputSlot = "A Custom Function Node must have at least one output slot";
public CustomFunctionNode()
{
UpdateNodeName();
synonyms = new string[] { "code", "HLSL" };
}
void UpdateNodeName()
{
if ((functionName == defaultFunctionName) || (functionName == null))
name = "Custom Function";
else
name = functionName + " (Custom Function)";
}
public override bool hasPreview => true;
public override bool hasUserAuthoredCode => true;
[SerializeField]
HlslSourceType m_SourceType = HlslSourceType.File;
public HlslSourceType sourceType
{
get => m_SourceType;
set => m_SourceType = value;
}
[SerializeField]
string m_FunctionName = k_DefaultFunctionName;
const string k_DefaultFunctionName = "Enter function name here...";
public string functionName
{
get => m_FunctionName;
set
{
m_FunctionName = value;
UpdateNodeName();
}
}
public string hlslFunctionName
{
get => m_FunctionName + "_$precision";
}
public static string defaultFunctionName => k_DefaultFunctionName;
[SerializeField]
string m_FunctionSource;
const string k_DefaultFunctionSource = "Enter function source file path here...";
public string functionSource
{
get => m_FunctionSource;
set => m_FunctionSource = value;
}
[SerializeField]
private bool m_FunctionSourceUsePragmas = true;
public bool functionSourceUsePragmas
{
get => m_FunctionSourceUsePragmas;
set => m_FunctionSourceUsePragmas = value;
}
[SerializeField]
string m_FunctionBody = k_DefaultFunctionBody;
const string k_DefaultFunctionBody = "Enter function body here...";
public string functionBody
{
get => m_FunctionBody;
set => m_FunctionBody = value;
}
public static string defaultFunctionBody => k_DefaultFunctionBody;
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var inputSlots))
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var outputSlots))
{
GetInputSlots<MaterialSlot>(inputSlots);
GetOutputSlots<MaterialSlot>(outputSlots);
if (!IsValidFunction())
{
// invalid functions generate special preview code.. (why?)
if (generationMode.IsPreview() && outputSlots.Count != 0)
{
outputSlots.OrderBy(s => s.id);
var hlslVariableType = outputSlots[0].concreteValueType.ToShaderString();
sb.AppendLine("{0} {1};",
hlslVariableType,
GetVariableNameForSlot(outputSlots[0].id));
}
return;
}
// declare output variables
foreach (var output in outputSlots)
{
sb.AppendLine("{0} {1};",
output.concreteValueType.ToShaderString(),
GetVariableNameForSlot(output.id));
if (output.bareResource)
AssignDefaultBareResource(output, sb);
}
// call function
sb.TryAppendIndentation();
sb.Append(hlslFunctionName);
sb.Append("(");
bool first = true;
foreach (var input in inputSlots)
{
if (!first)
sb.Append(", ");
first = false;
sb.Append(SlotInputValue(input, generationMode));
// fixup input for Bare types
if (input.bareResource)
{
if (input is SamplerStateMaterialSlot)
sb.Append(".samplerstate");
else
sb.Append(".tex");
}
}
foreach (var output in outputSlots)
{
if (!first)
sb.Append(", ");
first = false;
sb.Append(GetVariableNameForSlot(output.id));
// fixup output for Bare types
if (output.bareResource)
{
if (output is SamplerStateMaterialSlot)
sb.Append(".samplerstate");
else
sb.Append(".tex");
}
}
sb.Append(");");
sb.AppendNewLine();
}
}
void AssignDefaultBareResource(MaterialSlot slot, ShaderStringBuilder sb)
{
switch (slot.concreteValueType)
{
case ConcreteSlotValueType.Texture2D:
{
var slotVariable = GetVariableNameForSlot(slot.id);
sb.TryAppendIndentation();
sb.Append(slotVariable);
sb.Append(".samplerstate = default_sampler_Linear_Repeat;");
sb.AppendNewLine();
sb.TryAppendIndentation();
sb.Append(slotVariable);
sb.Append(".texelSize = float4(1.0f/128.0f, 1.0f/128.0f, 128.0f, 128.0f);");
sb.AppendNewLine();
sb.TryAppendIndentation();
sb.Append(slotVariable);
sb.Append(".scaleTranslate = float4(1.0f, 1.0f, 0.0f, 0.0f);");
sb.AppendNewLine();
sb.TryAppendIndentation();
sb.Append(slotVariable);
sb.Append(".hdrDecode = float4(0.0f, 0.0f, 0.0f, 0.0f);");
sb.AppendNewLine();
}
break;
case ConcreteSlotValueType.Texture3D:
case ConcreteSlotValueType.Texture2DArray:
case ConcreteSlotValueType.Cubemap:
{
var slotVariable = GetVariableNameForSlot(slot.id);
sb.TryAppendIndentation();
sb.Append(slotVariable);
sb.Append(".samplerstate = default_sampler_Linear_Repeat;");
sb.AppendNewLine();
}
break;
}
}
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
if (!IsValidFunction())
return;
switch (sourceType)
{
case HlslSourceType.File:
string path = AssetDatabase.GUIDToAssetPath(functionSource);
// This is required for upgrading without console errors
if (string.IsNullOrEmpty(path))
path = functionSource;
registry.RequiresIncludePath(path, shouldIncludeWithPragmas: functionSourceUsePragmas);
break;
case HlslSourceType.String:
registry.ProvideFunction(hlslFunctionName, builder =>
{
// add a hint for the analytic derivative code to ignore user functions
builder.AddLine("// unity-custom-func-begin");
GetFunctionHeader(builder);
using (builder.BlockScope())
{
builder.AppendLines(functionBody);
}
builder.AddLine("// unity-custom-func-end");
});
break;
default:
throw new ArgumentOutOfRangeException();
}
}
void GetFunctionHeader(ShaderStringBuilder sb)
{
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var inputSlots))
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var outputSlots))
{
GetInputSlots(inputSlots);
GetOutputSlots(outputSlots);
sb.Append("void ");
sb.Append(hlslFunctionName);
sb.Append("(");
var first = true;
foreach (var argument in inputSlots)
{
if (!first)
sb.Append(", ");
first = false;
argument.AppendHLSLParameterDeclaration(sb, argument.shaderOutputName);
}
foreach (var argument in outputSlots)
{
if (!first)
sb.Append(", ");
first = false;
sb.Append("out ");
argument.AppendHLSLParameterDeclaration(sb, argument.shaderOutputName);
}
sb.Append(")");
}
}
string SlotInputValue(MaterialSlot port, GenerationMode generationMode)
{
IEdge[] edges = port.owner.owner.GetEdges(port.slotReference).ToArray();
if (edges.Any())
{
var fromSocketRef = edges[0].outputSlot;
var fromNode = fromSocketRef.node;
if (fromNode == null)
return string.Empty;
return fromNode.GetOutputForSlot(fromSocketRef, port.concreteValueType, generationMode);
}
return port.GetDefaultValue(generationMode);
}
bool IsValidFunction()
{
return IsValidFunction(sourceType, functionName, functionSource, functionBody);
}
static bool IsValidFunction(HlslSourceType sourceType, string functionName, string functionSource, string functionBody)
{
bool validFunctionName = !string.IsNullOrEmpty(functionName) && functionName != k_DefaultFunctionName;
if (sourceType == HlslSourceType.String)
{
bool validFunctionBody = !string.IsNullOrEmpty(functionBody) && functionBody != k_DefaultFunctionBody;
return validFunctionName & validFunctionBody;
}
else
{
if (!validFunctionName || string.IsNullOrEmpty(functionSource) || functionSource == k_DefaultFunctionSource)
return false;
string path = AssetDatabase.GUIDToAssetPath(functionSource);
if (string.IsNullOrEmpty(path))
path = functionSource;
string extension = Path.GetExtension(path).ToLower();
return s_ValidExtensions.Contains(extension);
}
}
void ValidateSlotName()
{
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var slots))
{
GetSlots(slots);
foreach (var slot in slots)
{
// check for bad slot names
var error = NodeUtils.ValidateSlotName(slot.RawDisplayName(), out string errorMessage);
if (error)
{
owner.AddValidationError(objectId, errorMessage);
break;
}
}
}
}
void ValidateBareTextureSlots()
{
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var slots))
{
GetOutputSlots(slots);
foreach (var slot in slots)
{
if (slot.bareResource)
{
owner.AddValidationError(objectId, "This node uses Bare Texture or SamplerState outputs, which may produce unexpected results when fed to other nodes. Please convert the node to use the non-Bare struct-based outputs (see the structs defined in com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl)", ShaderCompilerMessageSeverity.Warning);
break;
}
}
}
}
public override void ValidateNode()
{
bool hasAnyOutputs = this.GetOutputSlots<MaterialSlot>().Any();
if (sourceType == HlslSourceType.File)
{
SourceFileStatus fileStatus = SourceFileStatus.Empty;
if (!string.IsNullOrEmpty(functionSource))
{
string path = AssetDatabase.GUIDToAssetPath(functionSource);
if (!string.IsNullOrEmpty(path) && AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(path) != null)
{
string extension = path.Substring(path.LastIndexOf('.')).ToLower();
if (!s_ValidExtensions.Contains(extension))
{
fileStatus = SourceFileStatus.Invalid;
}
else
{
fileStatus = SourceFileStatus.Valid;
}
}
else
fileStatus = SourceFileStatus.DoesNotExist;
}
if (fileStatus == SourceFileStatus.DoesNotExist || (fileStatus == SourceFileStatus.Empty && hasAnyOutputs))
owner.AddValidationError(objectId, k_MissingFile, ShaderCompilerMessageSeverity.Error);
else if (fileStatus == SourceFileStatus.Invalid)
owner.AddValidationError(objectId, k_InvalidFileType, ShaderCompilerMessageSeverity.Error);
else if (fileStatus == SourceFileStatus.Valid)
owner.ClearErrorsForNode(this);
}
if (!hasAnyOutputs)
{
owner.AddValidationError(objectId, k_MissingOutputSlot, ShaderCompilerMessageSeverity.Warning);
}
ValidateSlotName();
ValidateBareTextureSlots();
base.ValidateNode();
}
public bool Reload(HashSet<string> changedFileDependencyGUIDs)
{
if (changedFileDependencyGUIDs.Contains(m_FunctionSource))
{
owner.ClearErrorsForNode(this);
ValidateNode();
Dirty(ModificationScope.Graph);
return true;
}
return false;
}
public static string UpgradeFunctionSource(string functionSource)
{
// Handle upgrade from legacy asset path version
// If functionSource is not empty or a guid then assume it is legacy version
// If asset can be loaded from path then get its guid
// Otherwise it was the default string so set to empty
Guid guid;
if (!string.IsNullOrEmpty(functionSource) && !Guid.TryParse(functionSource, out guid))
{
// not sure why we don't use AssetDatabase.AssetPathToGUID...
// I guess we are testing that it actually exists and can be loaded here before converting?
string guidString = string.Empty;
ShaderInclude shaderInclude = AssetDatabase.LoadAssetAtPath<ShaderInclude>(functionSource);
if (shaderInclude != null)
{
long localId;
AssetDatabase.TryGetGUIDAndLocalFileIdentifier(shaderInclude, out guidString, out localId);
}
functionSource = guidString;
}
return functionSource;
}
public override void OnAfterDeserialize()
{
base.OnAfterDeserialize();
functionSource = UpgradeFunctionSource(functionSource);
UpdateNodeName();
}
public override void OnAfterMultiDeserialize(string json)
{
if (sgVersion < 1)
{
// any Texture2D slots used prior to version 1 should be flagged as "bare" so we can
// generate backwards compatible code
var slots = new List<MaterialSlot>();
GetSlots(slots);
foreach (var slot in slots)
{
slot.bareResource = true;
}
ChangeVersion(1);
}
}
public NeededTransform[] RequiresTransform(ShaderStageCapability stageCapability = ShaderStageCapability.All)
{
return new[]
{
NeededTransform.ObjectToWorld,
NeededTransform.WorldToObject
};
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fe9ce2929aeb84ff4aca41505b173cec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,197 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing;
using UnityEditor.ShaderGraph.Serialization;
namespace UnityEditor.ShaderGraph
{
[Serializable]
[Title("Utility", "Dropdown")]
class DropdownNode : AbstractMaterialNode, IOnAssetEnabled, IGeneratesBodyCode, IShaderInputObserver
{
internal const int k_MinEnumEntries = 2;
protected override bool CanPropagateFloatLiteral => true;
public override string documentationURL => NodeUtils.GetDocumentationString("Dropdown");
public DropdownNode()
{
UpdateNodeAfterDeserialization();
}
[SerializeField]
JsonRef<ShaderDropdown> m_Dropdown;
public ShaderDropdown dropdown
{
get { return m_Dropdown; }
set
{
if (m_Dropdown == value)
return;
m_Dropdown = value;
UpdateNode();
Dirty(ModificationScope.Topological);
}
}
public override bool canSetPrecision => false;
public override bool hasPreview => true;
public const int OutputSlotId = 0;
public override bool allowedInMainGraph { get => false; }
public void UpdateNodeDisplayName(string newDisplayName)
{
MaterialSlot foundSlot = FindSlot<MaterialSlot>(OutputSlotId);
if (foundSlot != null)
foundSlot.displayName = newDisplayName;
}
public void OnEnable()
{
UpdateNode();
}
public void UpdateNode()
{
name = dropdown.displayName;
UpdatePorts();
}
void UpdatePorts()
{
// Get slots
List<MaterialSlot> inputSlots = new List<MaterialSlot>();
GetInputSlots(inputSlots);
// Store the edges
Dictionary<MaterialSlot, List<IEdge>> edgeDict = new Dictionary<MaterialSlot, List<IEdge>>();
foreach (MaterialSlot slot in inputSlots)
edgeDict.Add(slot, (List<IEdge>)slot.owner.owner.GetEdges(slot.slotReference));
// Remove old slots
for (int i = 0; i < inputSlots.Count; i++)
{
RemoveSlot(inputSlots[i].id);
}
// Add output slot
AddSlot(new DynamicVectorMaterialSlot(OutputSlotId, "Out", "Out", SlotType.Output, Vector4.zero, literalMode:true));
// Add input slots
int[] slotIds = new int[dropdown.entries.Count + 1];
slotIds[dropdown.entries.Count] = OutputSlotId;
for (int i = 0; i < dropdown.entries.Count; i++)
{
// Get slot based on entry id
MaterialSlot slot = inputSlots.Where(x =>
x.id == dropdown.entries[i].id &&
x.RawDisplayName() == dropdown.entries[i].displayName &&
x.shaderOutputName == dropdown.entries[i].displayName).FirstOrDefault();
if (slot == null)
{
slot = new DynamicVectorMaterialSlot(dropdown.entries[i].id, dropdown.entries[i].displayName, dropdown.entries[i].displayName, SlotType.Input, Vector4.zero);
}
AddSlot(slot);
slotIds[i] = dropdown.entries[i].id;
}
RemoveSlotsNameNotMatching(slotIds, true);
// Reconnect the edges
foreach (KeyValuePair<MaterialSlot, List<IEdge>> entry in edgeDict)
{
foreach (IEdge edge in entry.Value)
{
owner.Connect(edge.outputSlot, edge.inputSlot);
}
}
ValidateNode();
}
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
var outputSlot = FindOutputSlot<MaterialSlot>(OutputSlotId);
bool isGeneratingSubgraph = owner.isSubGraph && !generationMode.IsPreview();
if (generationMode.IsPreview() || !isGeneratingSubgraph)
{
sb.AppendLine(string.Format($"{outputSlot.concreteValueType.ToShaderString()} {GetVariableNameForSlot(OutputSlotId)};"));
var value = GetSlotValue(GetSlotIdForActiveSelection(), generationMode);
sb.AppendLine(string.Format($"{GetVariableNameForSlot(OutputSlotId)} = {value};"));
}
else
{
// Iterate all entries in the dropdown
for (int i = 0; i < dropdown.entries.Count; i++)
{
if (i == 0)
{
sb.AppendLine(string.Format($"{outputSlot.concreteValueType.ToShaderString()} {GetVariableNameForSlot(OutputSlotId)};"));
sb.AppendLine($"if ({m_Dropdown.value.referenceName} == {i})");
}
else
{
sb.AppendLine($"else if ({m_Dropdown.value.referenceName} == {i})");
}
{
sb.AppendLine("{");
sb.IncreaseIndent();
var value = GetSlotValue(GetSlotIdForPermutation(new KeyValuePair<ShaderDropdown, int>(dropdown, i)), generationMode);
sb.AppendLine(string.Format($"{GetVariableNameForSlot(OutputSlotId)} = {value};"));
sb.DecreaseIndent();
sb.AppendLine("}");
}
if (i == dropdown.entries.Count - 1)
{
sb.AppendLine($"else");
sb.AppendLine("{");
sb.IncreaseIndent();
var value = GetSlotValue(GetSlotIdForPermutation(new KeyValuePair<ShaderDropdown, int>(dropdown, 0)), generationMode);
sb.AppendLine(string.Format($"{GetVariableNameForSlot(OutputSlotId)} = {value};"));
sb.DecreaseIndent();
sb.AppendLine("}");
}
}
}
}
public int GetSlotIdForPermutation(KeyValuePair<ShaderDropdown, int> permutation)
{
return permutation.Key.entries[permutation.Value].id;
}
public int GetSlotIdForActiveSelection()
{
return dropdown.entries[dropdown.value].id;
}
protected override void CalculateNodeHasError()
{
if (dropdown == null || !owner.dropdowns.Any(x => x == dropdown))
{
owner.AddConcretizationError(objectId, "Dropdown Node has no associated dropdown.");
hasError = true;
}
}
public void OnShaderInputUpdated(ModificationScope modificationScope)
{
UpdateNode();
Dirty(modificationScope);
if(modificationScope == ModificationScope.Layout)
UpdateNodeDisplayName(dropdown.displayName);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ec74026c92381d24bbc45e040822e702
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,256 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing;
using UnityEditor.ShaderGraph.Internal;
using UnityEditor.ShaderGraph.Serialization;
namespace UnityEditor.ShaderGraph
{
[Serializable]
[Title("Utility", "Keyword")]
class KeywordNode : AbstractMaterialNode, IOnAssetEnabled, IGeneratesBodyCode, IShaderInputObserver
{
internal const int k_MinEnumEntries = 2;
internal const int k_MaxEnumEntries = 8;
protected override bool CanPropagateFloatLiteral => true;
public override string documentationURL => NodeUtils.GetDocumentationString(keyword.overrideReferenceName switch
{
"MATERIAL_QUALITY" => "Material Quality Keyword",
"RAYTRACING_SHADER_GRAPH" => "Raytracing Quality Keyword",
_ => "Keyword",
});
public KeywordNode()
{
UpdateNodeAfterDeserialization();
}
[SerializeField]
JsonRef<ShaderKeyword> m_Keyword;
public ShaderKeyword keyword
{
get { return m_Keyword; }
set
{
if (m_Keyword == value)
return;
m_Keyword = value;
UpdateNode();
Dirty(ModificationScope.Topological);
}
}
public override bool canSetPrecision => false;
public override bool hasPreview => true;
public const int OutputSlotId = 0;
public void UpdateNodeDisplayName(string newDisplayName)
{
MaterialSlot foundSlot = FindSlot<MaterialSlot>(OutputSlotId);
if (foundSlot != null)
foundSlot.displayName = newDisplayName;
}
public void OnEnable()
{
UpdateNode();
}
public void UpdateNode()
{
name = keyword.displayName;
UpdatePorts();
}
void UpdatePorts()
{
switch (keyword.keywordType)
{
case KeywordType.Boolean:
{
// Boolean type has preset slots
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var temp))
{
GetInputSlots(temp);
if (temp.Count > 0)
{
break;
}
}
AddSlot(new DynamicVectorMaterialSlot(OutputSlotId, "Out", "Out", SlotType.Output, Vector4.zero));
AddSlot(new DynamicVectorMaterialSlot(1, "On", "On", SlotType.Input, Vector4.zero));
AddSlot(new DynamicVectorMaterialSlot(2, "Off", "Off", SlotType.Input, Vector4.zero));
RemoveSlotsNameNotMatching(new int[] { 0, 1, 2 }, true);
break;
}
case KeywordType.Enum:
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var inputSlots))
using (UnityEngine.Pool.ListPool<int>.Get(out var slotIDs))
{
// Get slots
GetInputSlots(inputSlots);
// Add output slot
AddSlot(new DynamicVectorMaterialSlot(OutputSlotId, "Out", "Out", SlotType.Output, Vector4.zero));
slotIDs.Add(OutputSlotId);
// Add input slots
for (int i = 0; i < keyword.entries.Count; i++)
{
// Get slot based on entry id
MaterialSlot slot = inputSlots.Find(x =>
x.id == keyword.entries[i].id &&
x.RawDisplayName() == keyword.entries[i].displayName &&
x.shaderOutputName == keyword.entries[i].referenceName);
// If slot doesn't exist, it's new so create it
if (slot == null)
{
slot = new DynamicVectorMaterialSlot(keyword.entries[i].id, keyword.entries[i].displayName, keyword.entries[i].referenceName, SlotType.Input, Vector4.zero);
}
AddSlot(slot);
slotIDs.Add(keyword.entries[i].id);
}
RemoveSlotsNameNotMatching(slotIDs, true);
bool orderChanged = SetSlotOrder(slotIDs);
if (orderChanged)
{
// unfortunately there is no way to get the view to update slot order other than Topological
Dirty(ModificationScope.Topological);
}
break;
}
}
ValidateNode();
}
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
var outputSlot = FindOutputSlot<MaterialSlot>(OutputSlotId);
if (keyword.keywordDefinition == KeywordDefinition.DynamicBranch
|| keyword.IsShaderBuildSettingsCompatible)
{
switch (keyword.keywordType)
{
case KeywordType.Boolean:
var onValue = GetSlotValue(1, generationMode);
var offValue = GetSlotValue(2, generationMode);
sb.AppendLine(string.Format($"{outputSlot.concreteValueType.ToShaderString()} {GetVariableNameForSlot(OutputSlotId)} = {keyword.referenceName} ? {onValue} : {offValue};"));
break;
case KeywordType.Enum:
sb.AppendLine(string.Format($"{outputSlot.concreteValueType.ToShaderString()} {GetVariableNameForSlot(OutputSlotId)};"));
int i = 0;
foreach ((KeywordEntry entry, int entryValue) in keyword.GetEntriesInBranchOrder())
{
string keywordName = $"{keyword.referenceName}_{entry.referenceName}";
var value = GetSlotValue(entry.id, generationMode);
string assignment = $"{GetVariableNameForSlot(OutputSlotId)} = {value};";
if (i == 0)
sb.AppendLine($"if ({keywordName}) {assignment}");
else if (i == keyword.entries.Count - 1)
sb.AppendLine($"else {assignment}");
else
sb.AppendLine($"else if ({keywordName}) {assignment}");
++i;
}
break;
}
}
else switch (keyword.keywordType)
{
case KeywordType.Boolean:
{
// Get values
var onValue = GetSlotValue(1, generationMode);
var offValue = GetSlotValue(2, generationMode);
// Append code
sb.AppendLine($"#if defined({keyword.referenceName})");
sb.AppendLine(string.Format($"{outputSlot.concreteValueType.ToShaderString()} {GetVariableNameForSlot(OutputSlotId)} = {onValue};"));
sb.AppendLine("#else");
sb.AppendLine(string.Format($"{outputSlot.concreteValueType.ToShaderString()} {GetVariableNameForSlot(OutputSlotId)} = {offValue};"));
sb.AppendLine("#endif");
break;
}
case KeywordType.Enum:
{
// Iterate all entries in the keyword
int i = 0;
foreach ((KeywordEntry entry, int entryValue) in keyword.GetEntriesInBranchOrder())
{
// Insert conditional
if (i == 0)
{
sb.AppendLine($"#if defined({keyword.referenceName}_{entry.referenceName})");
}
else if (i == keyword.entries.Count - 1)
{
sb.AppendLine("#else");
}
else
{
sb.AppendLine($"#elif defined({keyword.referenceName}_{entry.referenceName})");
}
// Append per-slot code
var value = GetSlotValue(GetSlotIdForPermutation(new KeyValuePair<ShaderKeyword, int>(keyword, entryValue)), generationMode);
sb.AppendLine(string.Format($"{outputSlot.concreteValueType.ToShaderString()} {GetVariableNameForSlot(OutputSlotId)} = {value};"));
++i;
}
// End condition
sb.AppendLine("#endif");
break;
}
default:
throw new ArgumentOutOfRangeException();
}
}
public int GetSlotIdForPermutation(KeyValuePair<ShaderKeyword, int> permutation)
{
switch (permutation.Key.keywordType)
{
// Slot 0 is output
case KeywordType.Boolean:
return 1 + permutation.Value;
// Ids are stored manually as slots are added
case KeywordType.Enum:
return permutation.Key.entries[permutation.Value].id;
default:
throw new ArgumentOutOfRangeException();
}
}
protected override void CalculateNodeHasError()
{
if (keyword == null || !owner.keywords.Any(x => x == keyword))
{
owner.AddConcretizationError(objectId, "Keyword Node has no associated keyword.");
hasError = true;
}
}
public void OnShaderInputUpdated(ModificationScope modificationScope)
{
if(modificationScope == ModificationScope.Layout)
UpdateNodeDisplayName(keyword.displayName);
UpdateNode();
Dirty(modificationScope);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 06a79a4c3d9dfd245a9c817c1fd44d20
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1c5103f75b5ec7445bb4c7b5a36f6aec
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,36 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "All")]
class AllNode : CodeFunctionNode
{
public AllNode()
{
name = "All";
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_All", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_All(
[Slot(0, Binding.None)] DynamicDimensionVector In,
[Slot(1, Binding.None)] out Boolean Out)
{
return
@"
{
Out = all(In);
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: aa03a542537ab4fb4a01a004efa97f58
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,37 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "And")]
class AndNode : CodeFunctionNode
{
public AndNode()
{
name = "And";
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_And", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_And(
[Slot(0, Binding.None)] Boolean A,
[Slot(1, Binding.None)] Boolean B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = A && B;
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 34929a6d0bb40407fb00206ab0a10fa1
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,36 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Any")]
class AnyNode : CodeFunctionNode
{
public AnyNode()
{
name = "Any";
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Any", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Any(
[Slot(0, Binding.None)] DynamicDimensionVector In,
[Slot(1, Binding.None)] out Boolean Out)
{
return
@"
{
Out = any(In);
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f3d072ddeeee848848f2e59dbb5cf1d9
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,36 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Branch")]
class BranchNode : CodeFunctionNode
{
protected override bool CanPropagateFloatLiteral => true;
public BranchNode()
{
name = "Branch";
synonyms = new string[] { "switch", "if", "else" };
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Branch", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Branch(
[Slot(0, Binding.None)] Boolean Predicate,
[Slot(1, Binding.None, 1, 1, 1, 1)] DynamicDimensionVector True,
[Slot(2, Binding.None, 0, 0, 0, 0)] DynamicDimensionVector False,
[Slot(3, Binding.None)] out DynamicDimensionVector Out)
{
return
@"
{
Out = Predicate ? True : False;
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 15ad3dc0ff22d7748af5e82ef5503d32
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,59 @@
using System;
using System.Linq;
using UnityEditor.Graphing;
using UnityEditor.Rendering;
namespace UnityEditor.ShaderGraph
{
[Serializable]
[Title("Utility", "Logic", "Branch On Input Connection")]
class BranchOnInputConnectionNode : AbstractMaterialNode, IGeneratesBodyCode
{
public const int InputSlotId = 0;
public const int ConnectedSlotId = 1;
public const int NotConnectedSlotId = 2;
public const int OutSlotId = 3;
const string kInputSlotName = "Input";
const string kConnectedSlotName = "Connected";
const string kNotConnectedSlotName = "NotConnected";
const string kOutSlotName = "Out";
public BranchOnInputConnectionNode()
{
name = "Branch On Input Connection";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new PropertyConnectionStateMaterialSlot(InputSlotId, kInputSlotName, kInputSlotName, Graphing.SlotType.Input));
AddSlot(new DynamicVectorMaterialSlot(ConnectedSlotId, kConnectedSlotName, kConnectedSlotName, Graphing.SlotType.Input, UnityEngine.Vector4.one));
AddSlot(new DynamicVectorMaterialSlot(NotConnectedSlotId, kNotConnectedSlotName, kNotConnectedSlotName, Graphing.SlotType.Input, UnityEngine.Vector4.zero));
AddSlot(new DynamicVectorMaterialSlot(OutSlotId, kOutSlotName, kOutSlotName, Graphing.SlotType.Output, UnityEngine.Vector4.zero));
RemoveSlotsNameNotMatching(new[] { InputSlotId, ConnectedSlotId, NotConnectedSlotId, OutSlotId });
}
public override bool allowedInMainGraph => false;
public override bool hasPreview => true;
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
// inspect one of the dynamic slots to figure out what type we are actually using
var dynSlot = this.FindInputSlot<DynamicVectorMaterialSlot>(ConnectedSlotId);
string dynamicDimension = NodeUtils.GetSlotDimension(dynSlot.concreteValueType);
var dynamicType = "$precision" + dynamicDimension;
// declare output variable
var input = GetSlotValue(InputSlotId, generationMode);
var connected = GetSlotValue(ConnectedSlotId, generationMode);
var notconnected = GetSlotValue(NotConnectedSlotId, generationMode);
var output = GetVariableNameForSlot(OutSlotId);
if (generationMode.IsPreview())
sb.AppendLine($"{dynamicType} {output} = {notconnected};");
else
sb.AppendLine($"{dynamicType} {output} = {input} ? {connected} : {notconnected};");
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: be7845eb62257564492a5c3ba1d0d181
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,146 @@
using System.Reflection;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
namespace UnityEditor.ShaderGraph
{
enum ComparisonType
{
Equal,
NotEqual,
Less,
LessOrEqual,
Greater,
GreaterOrEqual
};
[Title("Utility", "Logic", "Comparison")]
class ComparisonNode : CodeFunctionNode
{
public ComparisonNode()
{
name = "Comparison";
synonyms = new string[] { "equal", "greater than", "less than" };
}
[SerializeField]
private ComparisonType m_ComparisonType = ComparisonType.Equal;
[EnumControl("")]
public ComparisonType comparisonType
{
get { return m_ComparisonType; }
set
{
if (m_ComparisonType == value)
return;
m_ComparisonType = value;
Dirty(ModificationScope.Graph);
}
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
switch (comparisonType)
{
case ComparisonType.NotEqual:
return GetType().GetMethod("Unity_Comparison_NotEqual", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonType.Less:
return GetType().GetMethod("Unity_Comparison_Less", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonType.LessOrEqual:
return GetType().GetMethod("Unity_Comparison_LessOrEqual", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonType.Greater:
return GetType().GetMethod("Unity_Comparison_Greater", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonType.GreaterOrEqual:
return GetType().GetMethod("Unity_Comparison_GreaterOrEqual", BindingFlags.Static | BindingFlags.NonPublic);
default:
return GetType().GetMethod("Unity_Comparison_Equal", BindingFlags.Static | BindingFlags.NonPublic);
}
}
static string Unity_Comparison_Equal(
[Slot(0, Binding.None)] Vector1 A,
[Slot(1, Binding.None)] Vector1 B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = A == B ? 1 : 0;
}
";
}
static string Unity_Comparison_NotEqual(
[Slot(0, Binding.None)] Vector1 A,
[Slot(1, Binding.None)] Vector1 B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = A != B ? 1 : 0;
}
";
}
static string Unity_Comparison_Less(
[Slot(0, Binding.None)] Vector1 A,
[Slot(1, Binding.None)] Vector1 B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = A < B ? 1 : 0;
}
";
}
static string Unity_Comparison_LessOrEqual(
[Slot(0, Binding.None)] Vector1 A,
[Slot(1, Binding.None)] Vector1 B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = A <= B ? 1 : 0;
}
";
}
static string Unity_Comparison_Greater(
[Slot(0, Binding.None)] Vector1 A,
[Slot(1, Binding.None)] Vector1 B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = A > B ? 1 : 0;
}
";
}
static string Unity_Comparison_GreaterOrEqual(
[Slot(0, Binding.None)] Vector1 A,
[Slot(1, Binding.None)] Vector1 B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = A >= B ? 1 : 0;
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c66cc68d0d0862b4c8ddfc00093d0ae0
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,37 @@
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Is Front Face")]
class IsFrontFaceNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireFaceSign
{
public IsFrontFaceNode()
{
name = "Is Front Face";
synonyms = new string[] { "face", "side" };
UpdateNodeAfterDeserialization();
}
public override bool hasPreview { get { return false; } }
public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";
public override void UpdateNodeAfterDeserialization()
{
AddSlot(new BooleanMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, true, ShaderStageCapability.Fragment));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
sb.AppendLine(string.Format("$precision {0} = max(0, IN.{1}.x);", GetVariableNameForSlot(OutputSlotId), ShaderGeneratorNames.FaceSign));
}
public bool RequiresFaceSign(ShaderStageCapability stageCapability = ShaderStageCapability.Fragment)
{
return true;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c7e3dd61523f54e52ade29765e299d3f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,36 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Is Infinite")]
class IsInfiniteNode : CodeFunctionNode
{
public IsInfiniteNode()
{
name = "Is Infinite";
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_IsInfinite", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_IsInfinite(
[Slot(0, Binding.None)] Vector1 In,
[Slot(1, Binding.None)] out Boolean Out)
{
return
@"
{
Out = isinf(In);
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6eaf1bad910084b85ac9ead4319c7820
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,36 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Is NaN")]
class IsNanNode : CodeFunctionNode
{
public IsNanNode()
{
name = "Is NaN";
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_IsNaN", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_IsNaN(
[Slot(0, Binding.None)] Vector1 In,
[Slot(1, Binding.None)] out Boolean Out)
{
return
@"
{
Out = IsNaN(In) ? 1 : 0;
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0e15b8359c9db4d0ea27f4deab94137e
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,37 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Nand")]
class NandNode : CodeFunctionNode
{
public NandNode()
{
name = "Nand";
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Nand", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Nand(
[Slot(0, Binding.None)] Boolean A,
[Slot(1, Binding.None)] Boolean B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = !(A && B);
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4defde87a9d5e44d8b8900d3f8ef6440
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,36 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Not")]
class NotNode : CodeFunctionNode
{
public NotNode()
{
name = "Not";
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Not", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Not(
[Slot(0, Binding.None)] Boolean In,
[Slot(1, Binding.None)] out Boolean Out)
{
return
@"
{
Out = !In;
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: acb4138f9cef644dfa59102e871d1820
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,37 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Or")]
class OrNode : CodeFunctionNode
{
public OrNode()
{
name = "Or";
}
public override bool hasPreview
{
get { return false; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Or", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Or(
[Slot(0, Binding.None)] Boolean A,
[Slot(1, Binding.None)] Boolean B,
[Slot(2, Binding.None)] out Boolean Out)
{
return
@"
{
Out = A || B;
}
";
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0f6f860c0f1e24214b5d2974cc624227
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,248 @@
using System;
using System.Collections.Generic;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing;
using UnityEditor.ShaderGraph.Internal;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Logic", "Switch")]
class SwitchNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IShaderInputObserver
{
const int kPredicateSlot = 0;
const int kDefaultSlot = 1;
const int kOutputSlot = 2;
const int kFirstCaseSlot = 3;
// Case labels are 'A' + index; beyond 'Z' they sanitize to colliding HLSL identifiers.
internal const int kMaxCases = 26;
public override bool hasPreview => true;
[Serializable]
internal struct EntryCase
{
[SerializeField]
internal string name;
[SerializeField]
internal ComparisonType comparisonType;
[SerializeField]
internal float threshold;
}
protected override bool CanPropagateFloatLiteral => true;
[SerializeField]
internal bool m_floorPredicate = true;
[SerializeField]
internal List<EntryCase> m_cases = new();
[SerializeField]
internal bool m_useProperty = false;
[SerializeField]
internal List<EntryCase> m_casesFromUpstreamEnumProperty = new();
internal Vector1ShaderProperty UpstreamFloatEnumProperty
{
get
{
try
{
if (GetInputNodeFromSlot(kPredicateSlot) is PropertyNode propertyNode && propertyNode.property is Vector1ShaderProperty property && property.floatType == FloatType.Enum)
return property;
else return null;
}
catch
{
return null;
}
}
}
List<EntryCase> cases => m_useProperty ? m_casesFromUpstreamEnumProperty : m_cases;
public SwitchNode()
{
name = "Switch";
synonyms = new string[] { "switch" };
UpdateNodeAfterDeserialization();
}
public override void Concretize()
{
// concretize is called when a connection is made.
// we can use this opportunity to check if we need to switch modes.
if (m_useProperty = UpstreamFloatEnumProperty != null) // automatic mode means reserializing our properties.
{
UpstreamFloatEnumProperty?.AddObserver(this);
m_casesFromUpstreamEnumProperty = new();
for (int i = 0; i < UpstreamFloatEnumProperty.enumNames.Count; ++i)
{
m_casesFromUpstreamEnumProperty.Add(new EntryCase { name = UpstreamFloatEnumProperty.enumNames[i], threshold = UpstreamFloatEnumProperty.enumValues[i], comparisonType = ComparisonType.Equal });
}
}
UpdateNodeAfterDeserialization();
base.Concretize();
}
public override void UpdateNodeAfterDeserialization()
{
List<int> validSlots = new() { kPredicateSlot, kDefaultSlot, kOutputSlot };
AddSlot(new Vector1MaterialSlot(kPredicateSlot, "Predicate", "Predicate", SlotType.Input, 0));
AddSlot(new DynamicVectorMaterialSlot(kDefaultSlot, "Fallback", "Fallback", SlotType.Input, Vector4.zero));
AddSlot(new DynamicVectorMaterialSlot(kOutputSlot, "Out", "Out", SlotType.Output, Vector4.zero));
for (int i = 0; i < cases.Count; ++i)
{
var displayName = m_useProperty ? cases[i].name : $"{(char)('A'+i)}";
var safeName = "case_"+NodeUtils.GetHLSLSafeName(displayName);
AddSlot(new DynamicVectorMaterialSlot(kFirstCaseSlot + i, displayName, safeName, SlotType.Input, Vector4.zero));
validSlots.Add(kFirstCaseSlot + i);
}
RemoveSlotsNameNotMatching(validSlots, true);
}
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
registry.ProvideFunction(hlslFunctionName, s => { GetFunctionDefinition(s); });
}
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var inputSlots))
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var outputSlots))
{
GetInputSlots(inputSlots);
GetOutputSlots(outputSlots);
// Declare Outputs
foreach (var output in outputSlots)
sb.AppendLine("{0} {1};", output.concreteValueType.ToShaderString(), GetVariableNameForSlot(output.id));
// Call Function
sb.TryAppendIndentation();
sb.Append(hlslFunctionName);
sb.Append("(");
bool first = true;
foreach (var input in inputSlots)
{
string argument = SlotInputValue(input, generationMode);
if (first && m_floorPredicate) // predicate is first
argument = $"floor({argument})";
if (!first)
sb.Append(", ");
first = false;
sb.Append(argument);
}
foreach (var output in outputSlots)
{
if (!first)
sb.Append(", ");
first = false;
sb.Append(GetVariableNameForSlot(output.id));
}
sb.Append(");");
sb.AppendNewLine();
}
}
string hlslFunctionName => $"SwitchNode_$precision{this.objectId}";
static string Op(ComparisonType op)
{
switch (op)
{
default:
case ComparisonType.Equal: return "==";
case ComparisonType.NotEqual: return "!=";
case ComparisonType.Less: return "<";
case ComparisonType.LessOrEqual: return "<=";
case ComparisonType.Greater: return ">";
case ComparisonType.GreaterOrEqual: return ">=";
}
}
void GetFunctionDefinition(ShaderStringBuilder sb)
{
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var inputSlots))
using (UnityEngine.Pool.ListPool<MaterialSlot>.Get(out var outputSlots))
{
ShaderStringBuilder body = new();
body.AppendLine("Out = Fallback;");
GetInputSlots(inputSlots);
GetOutputSlots(outputSlots);
sb.Append("void ");
sb.Append(hlslFunctionName);
sb.Append("(");
var first = true;
foreach (var argument in inputSlots)
{
if (!first)
sb.Append(", ");
first = false;
argument.AppendHLSLParameterDeclaration(sb, argument.shaderOutputName);
if (argument.id >= kFirstCaseSlot)
{
var entry = cases[argument.id - kFirstCaseSlot];
if (argument.id == kFirstCaseSlot)
body.AppendLine("[branch]");
body.AppendLine($"{(argument.id > kFirstCaseSlot ? "else " : "")}if (Predicate {Op(entry.comparisonType)} {entry.threshold})");
using (body.BlockScope())
{
body.AppendLine($"Out = {argument.shaderOutputName};");
}
}
}
foreach (var argument in outputSlots)
{
if (!first)
sb.Append(", ");
first = false;
sb.Append("out ");
argument.AppendHLSLParameterDeclaration(sb, argument.shaderOutputName);
}
sb.AppendLine(")");
using (sb.BlockScope())
sb.AppendLines(body.ToString());
}
}
string SlotInputValue(MaterialSlot port, GenerationMode generationMode)
{
List<IEdge> edges = new(port.owner.owner.GetEdges(port.slotReference));
if (edges.Count > 0)
{
var fromSocketRef = edges[0].outputSlot;
var fromNode = fromSocketRef.node;
if (fromNode == null)
return string.Empty;
return fromNode.GetOutputForSlot(fromSocketRef, port.concreteValueType, generationMode);
}
return port.GetDefaultValue(generationMode);
}
public void OnShaderInputUpdated(ModificationScope modificationScope)
{
Concretize();
Dirty(ModificationScope.Topological);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 53413c183a3867a44a249b129ae86e6e
@@ -0,0 +1,64 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Utility", "Preview")]
class PreviewNode : CodeFunctionNode
{
public override bool hasPreview { get { return true; } }
internal override bool UsePreviewPref => false;
protected override bool CanPropagateFloatLiteral => true;
[SerializeField]
float m_Width;
[SerializeField]
float m_Height;
public void SetDimensions(float width, float height)
{
float newSize = Mathf.Clamp(Mathf.Min(width, height), 150f, 1000f);
m_Width = newSize;
m_Height = newSize;
}
public float width
{
get { return m_Width; }
}
public float height
{
get { return m_Height; }
}
public PreviewNode()
{
name = "Preview";
m_Width = 208f;
m_Height = 208f;
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Preview", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Preview(
[Slot(0, Binding.None)] DynamicDimensionVector In,
[Slot(1, Binding.None)] out DynamicDimensionVector Out)
{
return
@"
{
Out = In;
}
";
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0a9c3dd2c3b0c4347876f3e448be2046
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,180 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Graphing;
using UnityEditor.Rendering;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
class RedirectNodeData : AbstractMaterialNode
{
public const int kInputSlotID = 0;
public const int kOutputSlotID = 1;
protected override bool CanPropagateFloatLiteral => true;
public override string documentationURL => NodeUtils.GetDocumentationString("Redirect");
public RedirectNodeData()
{
name = "Redirect Node";
}
// Static version for testability
public static RedirectNodeData Create(GraphData graph, ConcreteSlotValueType edgeType, Vector2 absolutePosition, SlotReference inputRef, SlotReference outputRef, GroupData group)
{
var nodeData = new RedirectNodeData();
nodeData.AddSlots(edgeType);
nodeData.SetPosition(absolutePosition);
nodeData.group = group;
// Hard-coded for single input-output. Changes would be needed for multi-input redirects
var nodeInSlotRef = nodeData.GetSlotReference(RedirectNodeData.kInputSlotID);
var nodeOutSlotRef = nodeData.GetSlotReference(RedirectNodeData.kOutputSlotID);
graph.owner.RegisterCompleteObjectUndo("Add Redirect Node");
graph.AddNode(nodeData);
graph.Connect(outputRef, nodeInSlotRef);
graph.Connect(nodeOutSlotRef, inputRef);
return nodeData;
}
void AddSlots(ConcreteSlotValueType edgeType)
{
// Valuetype gets the type should be the type for input and output
switch (edgeType)
{
case ConcreteSlotValueType.Boolean:
AddSlot(new BooleanMaterialSlot(kInputSlotID, "", "", SlotType.Input, false));
AddSlot(new BooleanMaterialSlot(kOutputSlotID, "", "", SlotType.Output, false));
break;
case ConcreteSlotValueType.Vector1:
AddSlot(new DynamicVectorMaterialSlot(kInputSlotID, "", "", SlotType.Input, Vector4.zero, literalMode:true));
AddSlot(new DynamicVectorMaterialSlot(kOutputSlotID, "", "", SlotType.Output, Vector4.zero, literalMode:true));
break;
case ConcreteSlotValueType.Vector2:
AddSlot(new DynamicVectorMaterialSlot(kInputSlotID, "", "", SlotType.Input, Vector4.zero, literalMode:true));
AddSlot(new DynamicVectorMaterialSlot(kOutputSlotID, "", "", SlotType.Output, Vector4.zero, literalMode:true));
break;
case ConcreteSlotValueType.Vector3:
AddSlot(new DynamicVectorMaterialSlot(kInputSlotID, "", "", SlotType.Input, Vector4.zero, literalMode:true));
AddSlot(new DynamicVectorMaterialSlot(kOutputSlotID, "", "", SlotType.Output, Vector4.zero, literalMode:true));
break;
case ConcreteSlotValueType.Vector4:
AddSlot(new DynamicVectorMaterialSlot(kInputSlotID, "", "", SlotType.Input, Vector4.zero, literalMode:true));
AddSlot(new DynamicVectorMaterialSlot(kOutputSlotID, "", "", SlotType.Output, Vector4.zero, literalMode:true));
break;
case ConcreteSlotValueType.Matrix2:
AddSlot(new DynamicMatrixMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new DynamicMatrixMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.Matrix3:
AddSlot(new DynamicMatrixMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new DynamicMatrixMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.Matrix4:
AddSlot(new DynamicMatrixMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new DynamicMatrixMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.Texture2D:
AddSlot(new Texture2DMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new Texture2DMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.Texture2DArray:
AddSlot(new Texture2DArrayMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new Texture2DArrayMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.Texture3D:
AddSlot(new Texture3DMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new Texture3DMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.Cubemap:
AddSlot(new CubemapMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new CubemapMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.SamplerState:
AddSlot(new SamplerStateMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new SamplerStateMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.Gradient:
AddSlot(new GradientMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new GradientMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
case ConcreteSlotValueType.VirtualTexture:
AddSlot(new VirtualTextureMaterialSlot(kInputSlotID, "", "", SlotType.Input));
AddSlot(new VirtualTextureMaterialSlot(kOutputSlotID, "", "", SlotType.Output));
break;
default:
throw new ArgumentOutOfRangeException();
}
}
protected internal override string GetOutputForSlot(SlotReference fromSocketRef, ConcreteSlotValueType valueType, GenerationMode generationMode)
{
var slotRef = NodeUtils.DepthFirstCollectRedirectNodeFromNode(this);
var fromLeftNode = slotRef.node;
if (fromLeftNode is RedirectNodeData)
{
return GetSlotValue(kInputSlotID, generationMode);
}
if (fromLeftNode != null)
{
return GenerationUtils.AdaptNodeOutput(fromLeftNode, slotRef.slotId, valueType);
}
return base.GetOutputForSlot(fromSocketRef, valueType, generationMode);
}
public void SetPosition(Vector2 pos)
{
var temp = drawState;
Vector2 offset = new Vector2(-30, -12);
temp.position = new Rect(pos + offset, Vector2.zero);
drawState = temp;
}
public void GetOutputAndInputSlots(out SlotReference outputSlotRef, out List<SlotReference> inputSlotRefs)
{
var inputSlot = FindSlot<MaterialSlot>(kInputSlotID);
var inEdges = owner.GetEdges(inputSlot.slotReference).ToList();
outputSlotRef = inEdges.Any() ? inEdges.First().outputSlot : new SlotReference();
var outputSlot = FindSlot<MaterialSlot>(kOutputSlotID);
// Get the slot where this edge ends.
var outEdges = owner.GetEdges(outputSlot.slotReference);
inputSlotRefs = new List<SlotReference>(outEdges.Select(edge => edge.inputSlot));
}
public override void ValidateNode()
{
base.ValidateNode();
bool noInputs = false;
bool noOutputs = false;
var slots = new List<MaterialSlot>();
GetInputSlots(slots);
foreach (var inSlot in slots)
{
var edges = owner.GetEdges(inSlot.slotReference).ToList();
noInputs = !edges.Any();
}
slots.Clear();
GetOutputSlots(slots);
foreach (var outSlot in slots)
{
var edges = owner.GetEdges(outSlot.slotReference).ToList();
noOutputs = !edges.Any();
}
if (noInputs && !noOutputs)
{
owner.AddValidationError(objectId, "Node has no inputs and default value will be 0.", ShaderCompilerMessageSeverity.Warning);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b70493ee47ef82b46be4fe3568b0bb2d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing;
using UnityEditor.Experimental.GraphView;
using UnityEditor.Rendering;
namespace UnityEditor.ShaderGraph
{
class RedirectNodeView : RedirectNode, IShaderNodeView
{
IEdgeConnectorListener m_ConnectorListener;
// Tie the nodeView to its data
public void ConnectToData(AbstractMaterialNode inNode, IEdgeConnectorListener connectorListener)
{
if (inNode == null)
return;
// Set references
node = inNode;
title = "";
m_ConnectorListener = connectorListener;
viewDataKey = node.objectId;
// Set the VisualElement's position
SetPosition(new Rect(node.drawState.position.x, node.drawState.position.y, 0, 0));
AddSlots(node.GetSlots<MaterialSlot>());
// Removing a divider that made the ui a bit ugly
VisualElement contents = mainContainer.Q("contents");
VisualElement divider = contents?.Q("divider");
if (divider != null)
{
divider.RemoveFromHierarchy();
}
}
public void AddSlots(IEnumerable<MaterialSlot> slots)
{
foreach (var slot in slots)
{
if (slot.hidden)
continue;
var port = ShaderPort.Create(slot, m_ConnectorListener);
if (slot.isOutputSlot)
outputContainer.Add(port);
else
inputContainer.Add(port);
}
}
#region IShaderNodeView interface
public Node gvNode => this;
public AbstractMaterialNode node { get; private set; }
public VisualElement colorElement { get { return this; } }
public void Dispose()
{
node = null;
userData = null;
m_ConnectorListener = null;
inputContainer.Clear();
outputContainer.Clear();
}
public void UpdatePortInputTypes()
{
foreach (var anchor in inputContainer.Children().Concat(outputContainer.Children()).OfType<ShaderPort>())
{
var slot = anchor.slot;
anchor.portName = slot.displayName;
anchor.visualClass = slot.concreteValueType.ToClassName();
}
}
public void OnModified(ModificationScope scope)
{
if (scope == ModificationScope.Topological)
{
var slots = node.GetSlots<MaterialSlot>().ToList();
var inputPorts = inputContainer.Children().OfType<ShaderPort>().ToList();
foreach (var port in inputPorts)
{
var currentSlot = port.slot;
var newSlot = slots.FirstOrDefault(s => s.id == currentSlot.id);
if (newSlot == null)
{
// Slot doesn't exist anymore, remove it
inputContainer.Remove(port);
}
else
{
port.slot = newSlot;
slots.Remove(newSlot);
}
}
var outputPorts = outputContainer.Children().OfType<ShaderPort>().ToList();
foreach (var port in outputPorts)
{
var currentSlot = port.slot;
var newSlot = slots.FirstOrDefault(s => s.id == currentSlot.id);
if (newSlot == null)
{
outputContainer.Remove(port);
}
else
{
port.slot = newSlot;
slots.Remove(newSlot);
}
}
AddSlots(slots);
slots.Clear();
slots.AddRange(node.GetSlots<MaterialSlot>());
if (inputContainer.childCount > 0)
inputContainer.Sort((x, y) => slots.IndexOf(((ShaderPort)x).slot) - slots.IndexOf(((ShaderPort)y).slot));
if (outputContainer.childCount > 0)
outputContainer.Sort((x, y) => slots.IndexOf(((ShaderPort)x).slot) - slots.IndexOf(((ShaderPort)y).slot));
}
}
public bool FindPort(SlotReference slot, out ShaderPort port)
{
port = contentContainer.Q("top")?.Query<ShaderPort>().Where(p => p.slot.slotReference.Equals(slot)).First();
return port != null;
}
public void AttachMessage(string errString, ShaderCompilerMessageSeverity severity)
{
ClearMessage();
IconBadge badge = InternalBridge.IconBadge.CreateComment(errString);
Add(badge);
badge.AttachTo(outputContainer, SpriteAlignment.RightCenter);
}
public void ClearMessage()
{
var badge = this.Q<IconBadge>();
if (badge != null)
{
badge.Detach();
badge.RemoveFromHierarchy();
}
}
public void SetColor(Color newColor)
{
}
public void ResetColor()
{
}
public void UpdateDropdownEntries()
{
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 04f556529d4fe7c4286efa58a548d86a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,40 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Texture", "Split Texture Transform")]
class SplitTextureTransformNode : CodeFunctionNode
{
public override bool hasPreview { get { return false; } }
public SplitTextureTransformNode()
{
name = "Split Texture Transform";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_SplitTextureTransform", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_SplitTextureTransform(
[Slot(0, Binding.None)] Texture2D In,
[Slot(1, Binding.None)] out Vector2 Tiling,
[Slot(2, Binding.None)] out Vector2 Offset,
[Slot(3, Binding.None)] out Texture2D TextureOnly)
{
TextureOnly = default;
Tiling = default;
Offset = default;
return
@"
{
TextureOnly = In;
TextureOnly.scaleTranslate = float4(1.0f, 1.0f, 0.0f, 0.0f);
Tiling = In.scaleTranslate.xy;
Offset = In.scaleTranslate.zw;
}
";
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 88d9d4ff293792c42a6008e688bbc8fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 4967c0a2468b2c5438ff10f9f8e84f26
timeCreated: 1465559218
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: