Singularity/Library/PackageCache/com.unity.shadergraph@12.1.11/Editor/Data/Nodes/Channel/SplitNode.cs
2024-05-06 11:45:45 -07:00

64 lines
2.6 KiB
C#

using System;
using System.Linq;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Channel", "Split")]
class SplitNode : AbstractMaterialNode, IGeneratesBodyCode
{
const string kInputSlotName = "In";
const string kOutputSlotRName = "R";
const string kOutputSlotGName = "G";
const string kOutputSlotBName = "B";
const string kOutputSlotAName = "A";
public const int InputSlotId = 0;
public const int OutputSlotRId = 1;
public const int OutputSlotGId = 2;
public const int OutputSlotBId = 3;
public const int OutputSlotAId = 4;
public SplitNode()
{
name = "Split";
synonyms = new string[] { "separate" };
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new DynamicVectorMaterialSlot(InputSlotId, kInputSlotName, kInputSlotName, SlotType.Input, Vector4.zero));
AddSlot(new Vector1MaterialSlot(OutputSlotRId, kOutputSlotRName, kOutputSlotRName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotGId, kOutputSlotGName, kOutputSlotGName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotBId, kOutputSlotBName, kOutputSlotBName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotAId, kOutputSlotAName, kOutputSlotAName, SlotType.Output, 0));
RemoveSlotsNameNotMatching(new int[] { InputSlotId, OutputSlotRId, OutputSlotGId, OutputSlotBId, OutputSlotAId });
}
static int[] s_OutputSlots = { OutputSlotRId, OutputSlotGId, OutputSlotBId, OutputSlotAId };
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
var inputValue = GetSlotValue(InputSlotId, generationMode);
var inputSlot = FindInputSlot<MaterialSlot>(InputSlotId);
var numInputChannels = 0;
if (inputSlot != null)
{
numInputChannels = SlotValueHelper.GetChannelCount(inputSlot.concreteValueType);
if (numInputChannels > 4)
numInputChannels = 0;
}
for (var i = 0; i < 4; i++)
{
var outputFormat = numInputChannels == 1 ? inputValue : string.Format("{0}[{1}]", inputValue, i);
var outputValue = i >= numInputChannels ? "0" : outputFormat;
sb.AppendLine(string.Format("$precision {0} = {1};", GetVariableNameForSlot(s_OutputSlots[i]), outputValue));
}
}
}
}