浏览代码

Channel Mask node to Mask Field (prt 1)

/main
Matt Dean 7 年前
当前提交
3e4f3590
共有 3 个文件被更改,包括 138 次插入24 次删除
  1. 80
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ChannelMaskNode.cs
  2. 79
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ChannelEnumMaskControl.cs
  3. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ChannelEnumMaskControl.cs.meta

80
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ChannelMaskNode.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;

string GetFunctionName()
{
return string.Format("Unity_ChannelMask_{0}_{1}", channel, precision);
string channelSum;
if (channelMask == 0)
channelSum = "None";
else
{
bool red = (channelMask & 1) != 0;
bool green = (channelMask & 2) != 0;
bool blue = (channelMask & 4) != 0;
bool alpha = (channelMask & 8) != 0;
channelSum = string.Format("{0}{1}{2}{3}", red ? "Red" : "", green ? "Green" : "", blue ? "Blue" : "", alpha ? "Alpha" : "");
}
return string.Format("Unity_ChannelMask_{0}_{1}", channelSum, precision);
}
public sealed override void UpdateNodeAfterDeserialization()

RemoveSlotsNameNotMatching(new[] { InputSlotId, OutputSlotId });
}
public TextureChannel channel;
private TextureChannel m_Channel = TextureChannel.Red;
private int m_ChannelMask = 0;
[ChannelEnumControl("Channel")]
public TextureChannel channel
[ChannelEnumMaskControl("Channel")]
public int channelMask
get { return m_Channel; }
get { return m_ChannelMask; }
if (m_Channel == value)
if (m_ChannelMask == value)
m_Channel = value;
m_ChannelMask = value;
Dirty(ModificationScope.Graph);
}
}

int channelCount = SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(InputSlotId).concreteValueType);
if ((int)channel >= channelCount)
channel = TextureChannel.Red;
if (channelMask > 1 << channelCount)
channelMask = 0;
}
string GetFunctionPrototype(string argIn, string argOut)

{
registry.ProvideFunction(GetFunctionName(), s =>
{
int channelCount = SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(InputSlotId).concreteValueType);
switch (channel)
if(channelMask == 0)
s.AppendLine("Out = 0;");
else if(channelMask == -1)
s.AppendLine("Out = In;");
else
case TextureChannel.Green:
s.AppendLine("Out = In.yyyy;");
break;
case TextureChannel.Blue:
s.AppendLine("Out = In.zzzz;");
break;
case TextureChannel.Alpha:
s.AppendLine("Out = In.wwww;");
break;
case TextureChannel.Red:
s.AppendLine("Out = In.xxxx;");
break;
default:
throw new ArgumentOutOfRangeException();
bool red = (channelMask & 1) != 0;
bool green = (channelMask & 2) != 0;
bool blue = (channelMask & 4) != 0;
bool alpha = (channelMask & 8) != 0;
Debug.Log(red + " " + green + " " + blue + " " + alpha);
switch (channelCount)
{
case 1:
s.AppendLine("Out = In.r;");
break;
case 2:
s.AppendLine(string.Format("Out = {0}2({1}, {2});", precision,
red ? "In.r": "0", green ? "In.g" : "0"));
break;
case 3:
s.AppendLine(string.Format("Out = {0}3({1}, {2}, {3});", precision,
red ? "In.r" : "0", green ? "In.g" : "0", blue ? "In.b" : "0"));
break;
case 4:
s.AppendLine(string.Format("Out = {0}4({1}, {2}, {3}, {4});", precision,
red ? "In.r" : "0", green ? "In.g" : "0", blue ? "In.b" : "0", alpha ? "In.a" : "0"));
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
});

79
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ChannelEnumMaskControl.cs


using System;
using System.Reflection;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{
[AttributeUsage(AttributeTargets.Property)]
public class ChannelEnumMaskControlAttribute : Attribute, IControlAttribute
{
string m_Label;
int m_SlotId;
public ChannelEnumMaskControlAttribute(string label = null, int slotId = 0)
{
m_Label = label;
m_SlotId = slotId;
}
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new ChannelEnumMaskControlView(m_Label, m_SlotId, node, propertyInfo);
}
}
public class ChannelEnumMaskControlView : VisualElement, INodeModificationListener
{
GUIContent m_Label;
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
IMGUIContainer m_Container;
int m_SlotId;
public ChannelEnumMaskControlView(string label, int slotId, AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Node = node;
m_PropertyInfo = propertyInfo;
m_SlotId = slotId;
//if (!propertyInfo.PropertyType.IsEnum)
//throw new ArgumentException("Property must be an enum.", "propertyInfo");
m_Label = new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
m_Container = new IMGUIContainer(OnGUIHandler);
Add(m_Container);
}
void OnGUIHandler()
{
UpdatePopup();
}
public void OnNodeModified(ModificationScope scope)
{
if (scope == ModificationScope.Graph)
m_Container.Dirty(ChangeType.Repaint);
}
private void UpdatePopup()
{
var value = (int)m_PropertyInfo.GetValue(m_Node, null);
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
int channelCount = SlotValueHelper.GetChannelCount(m_Node.FindSlot<MaterialSlot>(m_SlotId).concreteValueType);
var enumEntryCount = m_PropertyInfo.GetValue(m_Node, null);
string[] enumEntryNames = Enum.GetNames(typeof(TextureChannel));
string[] popupEntries = new string[channelCount];
for (int i = 0; i < popupEntries.Length; i++)
popupEntries[i] = enumEntryNames[i];
value = EditorGUILayout.MaskField(m_Label, value, popupEntries);
if (changeCheckScope.changed)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_PropertyInfo.SetValue(m_Node, value, null);
}
}
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ChannelEnumMaskControl.cs.meta


fileFormatVersion: 2
guid: c32d860c6f767f14fa889dffac527bc5
timeCreated: 1507817885
正在加载...
取消
保存