|
|
|
|
|
|
namespace UnityEngine.MaterialGraph |
|
|
|
{ |
|
|
|
[Title("Channel/Combine")] |
|
|
|
public class CombineNode : PropertyNode, IGeneratesBodyCode |
|
|
|
public class CombineNode : AbstractMaterialNode, IGeneratesBodyCode |
|
|
|
protected const string kOutputSlot0Id = "Input0"; |
|
|
|
protected const string kOutputSlot1Id = "Input1"; |
|
|
|
protected const string kOutputSlot2Id = "Input2"; |
|
|
|
protected const string kOutputSlot3Id = "Input3"; |
|
|
|
protected const string kInputSlot0Name = "Input1"; |
|
|
|
protected const string kInputSlot1Name = "Input2"; |
|
|
|
protected const string kInputSlot2Name = "Input3"; |
|
|
|
protected const string kInputSlot3Name = "Input4"; |
|
|
|
protected const string kOutputSlotRGBAName = "RGBA"; |
|
|
|
protected const string kOutputSlotRGBSlotName = "RGB"; |
|
|
|
protected const string kOutputSlotRGSlotName = "RG"; |
|
|
|
protected const string kOutputSlotName = "Output"; |
|
|
|
|
|
|
|
public const int InputSlot0Id = 0; |
|
|
|
public const int InputSlot1Id = 1; |
|
|
|
|
|
|
public const int OutputSlotRGBAId = 4; |
|
|
|
public const int OutputSlotRGBId = 5; |
|
|
|
public const int OutputSlotRGId = 6; |
|
|
|
public const int OutputSlotId = 4; |
|
|
|
|
|
|
|
public CombineNode() |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
public sealed override void UpdateNodeAfterDeserialization() |
|
|
|
{ |
|
|
|
AddSlot(new MaterialSlot(InputSlot0Id, kOutputSlot0Id, kOutputSlot0Id, SlotType.Input, SlotValueType.Dynamic, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(InputSlot1Id, kOutputSlot1Id, kOutputSlot1Id, SlotType.Input, SlotValueType.Dynamic, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(InputSlot2Id, kOutputSlot2Id, kOutputSlot2Id, SlotType.Input, SlotValueType.Dynamic, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(InputSlot3Id, kOutputSlot3Id, kOutputSlot3Id, SlotType.Input, SlotValueType.Dynamic, Vector4.zero)); |
|
|
|
|
|
|
|
AddSlot(new MaterialSlot(OutputSlotRGBAId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.Vector4, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(OutputSlotRGBId, kOutputSlotRGBSlotName, kOutputSlotRGBSlotName, SlotType.Output, SlotValueType.Vector3, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(OutputSlotRGId, kOutputSlotRGSlotName, kOutputSlotRGSlotName, SlotType.Output, SlotValueType.Vector2, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(InputSlot0Id, kInputSlot0Name, kInputSlot0Name, SlotType.Input, SlotValueType.Dynamic, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(InputSlot1Id, kInputSlot1Name, kInputSlot1Name, SlotType.Input, SlotValueType.Dynamic, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(InputSlot2Id, kInputSlot2Name, kInputSlot2Name, SlotType.Input, SlotValueType.Dynamic, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(InputSlot3Id, kInputSlot3Name, kInputSlot3Name, SlotType.Input, SlotValueType.Dynamic, Vector4.zero)); |
|
|
|
AddSlot(new MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, SlotValueType.Vector4, Vector4.zero)); |
|
|
|
get { return new[] { InputSlot0Id, InputSlot1Id, InputSlot2Id, InputSlot3Id, OutputSlotRGBAId, OutputSlotRGBId, OutputSlotRGId }; } |
|
|
|
get { return new[] { InputSlot0Id, InputSlot1Id, InputSlot2Id, InputSlot3Id, OutputSlotId}; } |
|
|
|
[SerializeField] |
|
|
|
private Vector4 m_Value; |
|
|
|
|
|
|
|
public Vector4 value |
|
|
|
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|
|
|
get { return m_Value; } |
|
|
|
set |
|
|
|
{ |
|
|
|
if (m_Value == value) |
|
|
|
return; |
|
|
|
|
|
|
|
m_Value = value; |
|
|
|
|
|
|
|
if (onModified != null) |
|
|
|
onModified(this, ModificationScope.Node); |
|
|
|
} |
|
|
|
NodeUtils.SlotConfigurationExceptionIfBadConfiguration(this, new[] { InputSlot0Id, InputSlot1Id, InputSlot2Id, InputSlot3Id }, new[] { OutputSlotId }); |
|
|
|
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForSlot(OutputSlotId) + " = " + GetNodeBody(generationMode) + ";", true); |
|
|
|
public override PropertyType propertyType { get { return PropertyType.Vector4; } } |
|
|
|
|
|
|
|
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode) |
|
|
|
private int GetChannelCountForInput(int inputSlotId) |
|
|
|
if (exposedState == ExposedState.Exposed) |
|
|
|
visitor.AddShaderProperty(new VectorPropertyChunk(propertyName, description, m_Value, PropertyChunk.HideState.Visible)); |
|
|
|
} |
|
|
|
var inputSlot = FindInputSlot<MaterialSlot>(inputSlotId); |
|
|
|
if (inputSlot == null) |
|
|
|
return 0; |
|
|
|
int numInputChannels = (int)inputSlot.concreteValueType; |
|
|
|
if (owner.GetEdges(inputSlot.slotReference).ToList().Count() == 0) |
|
|
|
numInputChannels = 0; |
|
|
|
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode) |
|
|
|
{ |
|
|
|
if (exposedState == ExposedState.Exposed || generationMode.IsPreview()) |
|
|
|
visitor.AddShaderChunk(precision + "4 " + propertyName + ";", true); |
|
|
|
return numInputChannels; |
|
|
|
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|
|
|
private void AddChannelsFromInputSlot(GenerationMode generationMode, int inputSlot, ref string outputString, ref int freeChannel) |
|
|
|
//if (exposedState == ExposedState.Exposed || generationMode.IsPreview())
|
|
|
|
// return;
|
|
|
|
var inputValue = GetSlotValue(InputSlot0Id, generationMode); |
|
|
|
visitor.AddShaderChunk(precision + "4 " + propertyName + " = " + inputValue + ";", false); |
|
|
|
//visitor.AddShaderChunk(precision + "4 " + propertyName + " = " + precision + "4 (" + m_Value.x + ", " + m_Value.y + ", " + m_Value.z + ", " + m_Value.w + ");", true);
|
|
|
|
} |
|
|
|
string[] channelNames = { "r", "g", "b", "a" }; |
|
|
|
protected virtual MaterialSlot GetInputSlot() |
|
|
|
{ |
|
|
|
return new MaterialSlot(InputSlot0Id, GetInputSlotName(), kOutputSlot0Id, SlotType.Input, SlotValueType.Dynamic, Vector4.zero); |
|
|
|
} |
|
|
|
int numChannel = GetChannelCountForInput(inputSlot); |
|
|
|
numChannel = Math.Min(freeChannel, numChannel); |
|
|
|
if (numChannel <= 0) |
|
|
|
return; |
|
|
|
protected virtual string GetInputSlotName() { return "Input"; } |
|
|
|
string channelInputName = GetSlotValue(inputSlot, generationMode); |
|
|
|
outputString += channelInputName; |
|
|
|
public override PreviewProperty GetPreviewProperty() |
|
|
|
{ |
|
|
|
return new PreviewProperty |
|
|
|
{ |
|
|
|
m_Name = propertyName, |
|
|
|
m_PropType = PropertyType.Vector4, |
|
|
|
m_Vector4 = m_Value |
|
|
|
}; |
|
|
|
freeChannel -= numChannel; |
|
|
|
if (freeChannel != 0) |
|
|
|
outputString += ","; |
|
|
|
/*public override string GetVariableNameForSlot(int slotId) |
|
|
|
protected string GetNodeBody(GenerationMode generationMode) |
|
|
|
string slotOutput; |
|
|
|
switch (slotId) |
|
|
|
string outputString = precision + "4("; |
|
|
|
int freeChannels = 4; |
|
|
|
AddChannelsFromInputSlot(generationMode, InputSlot0Id, ref outputString, ref freeChannels); |
|
|
|
AddChannelsFromInputSlot(generationMode, InputSlot1Id, ref outputString, ref freeChannels); |
|
|
|
AddChannelsFromInputSlot(generationMode, InputSlot2Id, ref outputString, ref freeChannels); |
|
|
|
AddChannelsFromInputSlot(generationMode, InputSlot3Id, ref outputString, ref freeChannels); |
|
|
|
|
|
|
|
for (int i = freeChannels; i > 0; --i) |
|
|
|
case OutputSlotRId: |
|
|
|
slotOutput = ".r"; |
|
|
|
break; |
|
|
|
case OutputSlotGId: |
|
|
|
slotOutput = ".g"; |
|
|
|
break; |
|
|
|
case OutputSlotBId: |
|
|
|
slotOutput = ".b"; |
|
|
|
break; |
|
|
|
case OutputSlotAId: |
|
|
|
slotOutput = ".a"; |
|
|
|
break; |
|
|
|
default: |
|
|
|
slotOutput = ""; |
|
|
|
break; |
|
|
|
outputString += "0.0"; |
|
|
|
if (i > 1) |
|
|
|
outputString += ", "; |
|
|
|
return propertyName + slotOutput; |
|
|
|
//return GetVariableNameForNode() + slotOutput;
|
|
|
|
}*/ |
|
|
|
|
|
|
|
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties) |
|
|
|
{ |
|
|
|
properties.Add(GetPreviewProperty()); |
|
|
|
outputString += ")"; |
|
|
|
|
|
|
|
return outputString; |
|
|
|
} |
|
|
|
} |
|
|
|
} |