浏览代码

Refactor ColorspaceConversionControl to work on enums in general and implement it for TransformNode

/main
Peter Bay Bastian 7 年前
当前提交
211cf575
共有 6 个文件被更改,包括 155 次插入128 次删除
  1. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Utility/ColorspaceConversion.cs
  2. 110
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs
  3. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss
  4. 81
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/EnumConversionControl.cs
  5. 70
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ColorspaceConversionControl.cs
  6. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/EnumConversionControl.cs.meta

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Utility/ColorspaceConversion.cs


}
[Serializable]
public struct ColorspaceConversion
public struct ColorspaceConversion : IEnumConversion
{
public Colorspace from;
public Colorspace to;

this.from = from;
this.to = to;
}
Enum IEnumConversion.from
{
get { return from; }
set { from = (Colorspace)value; }
}
Enum IEnumConversion.to
{
get { return to; }
set { to = (Colorspace)value; }
}
}
[Title("Artistic", "Utility", "Colorspace Conversion")]

[SerializeField]
ColorspaceConversion m_Conversion = new ColorspaceConversion(Colorspace.RGB, Colorspace.RGB);
[ColorspaceConversionControl]
[EnumConversionControl]
ColorspaceConversion conversion
{
get { return m_Conversion; }

110
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs


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

[Serializable]
public struct CoordinateSpaceConversion : IEnumConversion
{
public CoordinateSpace from;
public CoordinateSpace to;
public CoordinateSpaceConversion(CoordinateSpace from, CoordinateSpace to)
{
this.from = from;
this.to = to;
}
Enum IEnumConversion.from
{
get { return from; }
set { from = (CoordinateSpace)value; }
}
Enum IEnumConversion.to
{
get { return to; }
set { to = (CoordinateSpace)value; }
}
}
[SerializeField]
private CoordinateSpace m_spaceListFrom;
[SerializeField]
private CoordinateSpace m_spaceListTo;
private const int InputSlotId = 0;
private const int OutputSlotId = 1;
private const string kInputSlotName = "In";

UpdateNodeAfterDeserialization();
}
[EnumControl("From")]
public CoordinateSpace spaceFrom
{
get { return m_spaceListFrom; }
set
{
if (m_spaceListFrom == value)
return;
[SerializeField]
CoordinateSpaceConversion m_Conversion = new CoordinateSpaceConversion(CoordinateSpace.Object, CoordinateSpace.World);
m_spaceListFrom = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
[EnumControl("To")]
public CoordinateSpace spaceTo
[EnumConversionControl]
public CoordinateSpaceConversion conversion
get { return m_spaceListTo; }
get { return m_Conversion; }
if (m_spaceListTo == value)
if (Equals(m_Conversion, value))
m_spaceListTo = value;
m_Conversion = value;
{
}
}
}

string transformString = "";
bool requiresTangentTransform = false;
if (spaceFrom == CoordinateSpace.World)
if (conversion.from == CoordinateSpace.World)
if (spaceTo == CoordinateSpace.World)
if (conversion.to == CoordinateSpace.World)
else if (spaceTo == CoordinateSpace.Object)
else if (conversion.to == CoordinateSpace.Object)
else if (spaceTo == CoordinateSpace.Tangent)
else if (conversion.to == CoordinateSpace.Tangent)
else if (spaceTo == CoordinateSpace.View)
else if (conversion.to == CoordinateSpace.View)
else if (spaceFrom == CoordinateSpace.Object)
else if (conversion.from == CoordinateSpace.Object)
if (spaceTo == CoordinateSpace.World)
if (conversion.to == CoordinateSpace.World)
else if (spaceTo == CoordinateSpace.Object)
else if (conversion.to == CoordinateSpace.Object)
else if (spaceTo == CoordinateSpace.Tangent)
else if (conversion.to == CoordinateSpace.Tangent)
else if (spaceTo == CoordinateSpace.View)
else if (conversion.to == CoordinateSpace.View)
else if (spaceFrom == CoordinateSpace.Tangent)
else if (conversion.from == CoordinateSpace.Tangent)
if (spaceTo == CoordinateSpace.World)
if (conversion.to == CoordinateSpace.World)
else if (spaceTo == CoordinateSpace.Object)
else if (conversion.to == CoordinateSpace.Object)
else if (spaceTo == CoordinateSpace.Tangent)
else if (conversion.to == CoordinateSpace.Tangent)
else if (spaceTo == CoordinateSpace.View)
else if (conversion.to == CoordinateSpace.View)
else if (spaceFrom == CoordinateSpace.View)
else if (conversion.from == CoordinateSpace.View)
if (spaceTo == CoordinateSpace.World)
if (conversion.to == CoordinateSpace.World)
else if (spaceTo == CoordinateSpace.Object)
else if (conversion.to == CoordinateSpace.Object)
else if (spaceTo == CoordinateSpace.Tangent)
else if (conversion.to == CoordinateSpace.Tangent)
else if (spaceTo == CoordinateSpace.View)
else if (conversion.to == CoordinateSpace.View)
{
transformString = inputValue;
}

visitor.AddShaderChunk("float3x3 tangentTransform = float3x3(" + spaceFrom.ToString() + "SpaceTangent, " + spaceFrom.ToString() + "SpaceBiTangent, " + spaceFrom.ToString() + "SpaceNormal);", false);
visitor.AddShaderChunk("float3x3 tangentTransform = float3x3(" + conversion.from.ToString() + "SpaceTangent, " + conversion.from.ToString() + "SpaceBiTangent, " + conversion.from.ToString() + "SpaceNormal);", false);
visitor.AddShaderChunk(string.Format("{0} {1} = {2};",
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType),

public NeededCoordinateSpace RequiresTangent()
{
return spaceFrom.ToNeededCoordinateSpace();
return conversion.from.ToNeededCoordinateSpace();
return spaceFrom.ToNeededCoordinateSpace();
return conversion.from.ToNeededCoordinateSpace();
return spaceFrom.ToNeededCoordinateSpace();
return conversion.from.ToNeededCoordinateSpace();
}
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss


padding-bottom: 4;
}
ColorspaceConversionControlView {
EnumConversionControlView {
flex-direction: row;
padding-left: 8;
padding-right: 8;

ColorspaceConversionControlView > EnumField {
EnumConversionControlView > EnumField {
flex: 1;
margin-left: 0;
margin-right: 0;

ColorspaceConversionControlView > Label {
EnumConversionControlView > Label {
margin-left: 4;
margin-right: 4;
margin-top: 0;

81
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/EnumConversionControl.cs


using System;
using System.Linq;
using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{
public interface IEnumConversion
{
Enum from { get; set; }
Enum to { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
public class EnumConversionControlAttribute : Attribute, IControlAttribute
{
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new EnumConversionControlView(node, propertyInfo);
}
}
public class EnumConversionControlView : VisualElement
{
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
IEnumConversion value
{
get { return (IEnumConversion)m_PropertyInfo.GetValue(m_Node, null); }
set { m_PropertyInfo.SetValue(m_Node, value, null); }
}
public EnumConversionControlView(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
if (!propertyInfo.PropertyType.GetInterfaces().Any(t => t == typeof(IEnumConversion)))
throw new ArgumentException("Property type must implement IEnumConversion.");
m_Node = node;
m_PropertyInfo = propertyInfo;
var currentValue = value;
var ec = (IEnumConversion) propertyInfo.GetValue(m_Node, null);
propertyInfo.SetValue(m_Node, ec, null);
var fromField = new EnumField(currentValue.from);
fromField.OnValueChanged(OnFromChanged);
Add(fromField);
var arrowLabel = new Label("➔");
Add(arrowLabel);
var toField = new EnumField(currentValue.to);
toField.OnValueChanged(OnToChanged);
Add(toField);
}
void OnFromChanged(ChangeEvent<Enum> evt)
{
var currentValue = value;
if (!Equals(currentValue.from, evt.newValue))
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change Colorspace From");
currentValue.from = evt.newValue;
value = currentValue;
}
}
void OnToChanged(ChangeEvent<Enum> evt)
{
var currentValue = value;
if (!Equals(currentValue.to, evt.newValue))
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change Colorspace To");
currentValue.to = evt.newValue;
value = currentValue;
}
}
}
}

70
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ColorspaceConversionControl.cs


using System;
using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{
[AttributeUsage(AttributeTargets.Property)]
public class ColorspaceConversionControlAttribute : Attribute, IControlAttribute
{
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new ColorspaceConversionControlView(node, propertyInfo);
}
}
public class ColorspaceConversionControlView : VisualElement
{
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
ColorspaceConversion conversion
{
get { return (ColorspaceConversion)m_PropertyInfo.GetValue(m_Node, null); }
set { m_PropertyInfo.SetValue(m_Node, value, null); }
}
public ColorspaceConversionControlView(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Node = node;
m_PropertyInfo = propertyInfo;
var value = conversion;
var fromField = new EnumField(value.from);
fromField.OnValueChanged(OnFromChanged);
Add(fromField);
var arrowLabel = new Label("➔");
Add(arrowLabel);
var toField = new EnumField(value.to);
toField.OnValueChanged(OnToChanged);
Add(toField);
}
void OnFromChanged(ChangeEvent<Enum> evt)
{
var value = conversion;
var newValue = (Colorspace)evt.newValue;
if (value.from != newValue)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change Colorspace From");
value.from = newValue;
conversion = value;
}
}
void OnToChanged(ChangeEvent<Enum> evt)
{
var value = conversion;
var newValue = (Colorspace)evt.newValue;
if (value.to != newValue)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change Colorspace To");
value.to = newValue;
conversion = value;
}
}
}
}

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ColorspaceConversionControl.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/EnumConversionControl.cs.meta

正在加载...
取消
保存