浏览代码

Flip Node

- Added ToggleControl (first pass)
/main
Matt Dean 7 年前
当前提交
20cf4421
共有 4 个文件被更改,包括 293 次插入0 次删除
  1. 182
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/FlipNode.cs
  2. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/FlipNode.cs.meta
  3. 97
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ToggleControl.cs
  4. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ToggleControl.cs.meta

182
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/FlipNode.cs


using System;
using System.Collections.Generic;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Channel/Flip")]
public class FlipNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction
{
public FlipNode()
{
name = "Flip";
UpdateNodeAfterDeserialization();
}
const int InputSlotId = 0;
const int OutputSlotId = 1;
const string kInputSlotName = "In";
const string kOutputSlotName = "Out";
public override bool hasPreview
{
get { return true; }
}
string GetFunctionName()
{
return "Unity_Flip_" + precision + "_" + GuidEncoder.Encode(guid);
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new DynamicVectorMaterialSlot(InputSlotId, kInputSlotName, kInputSlotName, SlotType.Input, Vector4.zero));
AddSlot(new DynamicVectorMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { InputSlotId, OutputSlotId });
}
int channelCount { get { return (int)SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(InputSlotId).concreteValueType); } }
[SerializeField]
private bool m_RedChannel;
[ToggleControl("Red")]
public ToggleState redChannel
{
get { return ToggleHelper.GetToggleValue(m_RedChannel, channelCount > 0); }
set
{
bool isOn = ToggleHelper.GetBoolValue(value);
if (m_RedChannel == isOn)
return;
m_RedChannel = isOn;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
[SerializeField]
private bool m_GreenChannel;
[ToggleControl("Green")]
public ToggleState greenChannel
{
get { return ToggleHelper.GetToggleValue(m_GreenChannel, channelCount > 1); }
set
{
bool isOn = ToggleHelper.GetBoolValue(value);
if (m_GreenChannel == isOn)
return;
m_GreenChannel = isOn;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
[SerializeField]
private bool m_BlueChannel;
[ToggleControl("Blue")]
public ToggleState blueChannel
{
get { return ToggleHelper.GetToggleValue(m_BlueChannel, channelCount > 2); }
set
{
bool isOn = ToggleHelper.GetBoolValue(value);
if (m_BlueChannel == isOn)
return;
m_BlueChannel = isOn;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
private bool m_AlphaChannel;
[ToggleControl("Alpha")]
public ToggleState alphaChannel
{
get { return ToggleHelper.GetToggleValue(m_AlphaChannel, channelCount > 3); }
set
{
bool isOn = ToggleHelper.GetBoolValue(value);
if (m_AlphaChannel == isOn)
return;
m_AlphaChannel = isOn;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
string GetFunctionPrototype(string inArg, string outArg)
{
return string.Format("void {0} ({1} {2}, out {3} {4})", GetFunctionName(),
ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType), inArg,
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType), outArg);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
string inputValue = GetSlotValue(InputSlotId, generationMode);
string outputValue = GetSlotValue(OutputSlotId, generationMode);
visitor.AddShaderChunk(string.Format("{0} {1};", ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType), GetVariableNameForSlot(OutputSlotId)), true);
visitor.AddShaderChunk(GetFunctionCallBody(inputValue, outputValue), true);
}
string GetFunctionCallBody(string inputValue, string outputValue)
{
return GetFunctionName() + " (" + inputValue + ", " + outputValue + ");";
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("In", "Out"), true);
outputString.AddShaderChunk("{", true);
outputString.Indent();
int channelCount = (int)SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(InputSlotId).concreteValueType);
switch(channelCount)
{
case 2:
outputString.AddShaderChunk(string.Format("Out = {0} ({1}, {2});",
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType),
string.Format("{0}{1}{2}", m_RedChannel ? "1 - " : "", kInputSlotName, ".r"),
string.Format("{0}{1}{2}", m_GreenChannel ? "1 - " : "", kInputSlotName, ".g")), true);
break;
case 3:
outputString.AddShaderChunk(string.Format("Out = {0} ({1}, {2}, {3});",
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType),
string.Format("{0}{1}{2}", m_RedChannel ? "1 - " : "", kInputSlotName, ".r"),
string.Format("{0}{1}{2}", m_GreenChannel ? "1 - " : "", kInputSlotName, ".g"),
string.Format("{0}{1}{2}", m_BlueChannel ? "1 - " : "", kInputSlotName, ".b")), true);
break;
case 4:
outputString.AddShaderChunk(string.Format("Out = {0} ({1}, {2}, {3}, {4});",
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType),
string.Format("{0}{1}{2}", m_RedChannel ? "1 - " : "", kInputSlotName, ".r"),
string.Format("{0}{1}{2}", m_GreenChannel ? "1 - " : "", kInputSlotName, ".g"),
string.Format("{0}{1}{2}", m_BlueChannel ? "1 - " : "", kInputSlotName, ".b"),
string.Format("{0}{1}{2}", m_AlphaChannel ? "1 - " : "", kInputSlotName, ".a")), true);
break;
default:
outputString.AddShaderChunk(string.Format("Out = {0};",
string.Format("{0}{1}{2}", m_RedChannel ? "1 - " : "", kInputSlotName, ".r")), true);
break;
}
outputString.Deindent();
outputString.AddShaderChunk("}", true);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/FlipNode.cs.meta


fileFormatVersion: 2
guid: fa443691530c547418e195e22597f2af
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

97
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ToggleControl.cs


using System;
using System.Reflection;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{
public enum ToggleState
{
EnabledOff,
EnabledOn,
DisabledOff,
DisabledOn
}
public static class ToggleHelper
{
public static bool GetBoolValue(ToggleState value)
{
if ((int)value > 1)
return Convert.ToBoolean((int)value - 2);
else
return Convert.ToBoolean((int)value);
}
public static ToggleState GetToggleValue(bool value)
{
return (ToggleState)Convert.ToInt32(value);
}
public static ToggleState GetToggleValue(bool value, bool enableCondition)
{
return (ToggleState)(Convert.ToInt32(value) + (enableCondition ? 0 : 2));
}
}
[AttributeUsage(AttributeTargets.Property)]
public class ToggleControlAttribute : Attribute, IControlAttribute
{
string m_Label;
public ToggleControlAttribute(string label = null)
{
m_Label = label;
}
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new ToggleControlView(m_Label, node, propertyInfo);
}
}
public class ToggleControlView : VisualElement, INodeModificationListener
{
GUIContent m_Label;
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
IMGUIContainer m_Container;
public ToggleControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Node = node;
m_PropertyInfo = propertyInfo;
if (propertyInfo.PropertyType != typeof(ToggleState))
throw new ArgumentException("Property must be a ToggleState.", "propertyInfo");
m_Label = new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
m_Container = new IMGUIContainer(OnGUIHandler);
Add(m_Container);
}
public void OnNodeModified(ModificationScope scope)
{
if (scope == ModificationScope.Graph)
m_Container.Dirty(ChangeType.Repaint);
}
void OnGUIHandler()
{
var value = (ToggleState) m_PropertyInfo.GetValue(m_Node, null);
bool isEnabled = (int)value < 2;
m_Container.SetEnabled(isEnabled);
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
bool isOn = EditorGUILayout.Toggle(m_Label, (int)value == 1 || (int)value == 3);
value = (ToggleState)(Convert.ToInt32(!isEnabled) + Convert.ToInt32(isOn));
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/ToggleControl.cs.meta


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