浏览代码

Massively decreased amount of allocations in preview rendering (~440 kB -> ~23 kB per frame)

/main
Peter Bay Bastian 7 年前
当前提交
da0d10c8
共有 63 个文件被更改,包括 543 次插入279 次删除
  1. 2
      MaterialGraphProject/Assets/NewNodes/Editor/Keep/HeightToNormalNode.cs
  2. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  3. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractShaderProperty.cs
  4. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorShaderProperty.cs
  5. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapInputMaterialSlot.cs
  6. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapShaderProperty.cs
  7. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs
  8. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/FloatShaderProperty.cs
  9. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  10. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PreviewProperty.cs
  11. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SamplerStateShaderProperty.cs
  12. 36
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableTexture.cs
  13. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs
  14. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TextureShaderProperty.cs
  15. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs
  16. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs
  17. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2ShaderProperty.cs
  18. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs
  19. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3ShaderProperty.cs
  20. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs
  21. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4ShaderProperty.cs
  22. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireBitangent.cs
  23. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireNormal.cs
  24. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequirePosition.cs
  25. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireTangent.cs
  26. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireTime.cs
  27. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireViewDirection.cs
  28. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/NeededCoordinateSpace.cs
  29. 62
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
  30. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Adjustment/ChannelMixerNode.cs
  31. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/FlipNode.cs
  32. 50
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  33. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs
  34. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector1Node.cs
  35. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector2Node.cs
  36. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector3Node.cs
  37. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector4Node.cs
  38. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Geometry/ScreenPositionNode.cs
  39. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Geometry/UVNode.cs
  40. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Geometry/VertexColorNode.cs
  41. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapAssetNode.cs
  42. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DAssetNode.cs
  43. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DNode.cs
  44. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraphNode.cs
  45. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraphOutputNode.cs
  46. 108
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs
  47. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/SearchWindowProvider.cs
  48. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/Edge.cs
  49. 49
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableGraph.cs
  50. 49
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableNode.cs
  51. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Interfaces/IGraph.cs
  52. 30
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Interfaces/INode.cs
  53. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Interfaces/SlotReference.cs
  54. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Testing/UnitTests/SerializedGraphTests.cs
  55. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
  56. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireMeshUV.cs
  57. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireMeshUV.cs.meta
  58. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireScreenPosition.cs
  59. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireScreenPosition.cs.meta
  60. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireVertexColor.cs
  61. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireVertexColor.cs.meta
  62. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/MaterialNodeExtensions.cs.meta
  63. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/MaterialNodeExtensions.cs

2
MaterialGraphProject/Assets/NewNodes/Editor/Keep/HeightToNormalNode.cs


public bool RequiresMeshUV(UVChannel channel)
{
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireMeshUV>())
foreach (var slot in this.GetInputSlots<MaterialSlot>().OfType<IMayRequireMeshUV>())
{
if (slot.RequiresMeshUV(channel))
return true;

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs


m_RemovedProperties.Add(guid);
}
static List<IEdge> s_TempEdges = new List<IEdge>();
public void ReplacePropertyNodeWithConcreteNode(PropertyNode propertyNode)
{
var property = properties.FirstOrDefault(x => x.guid == propertyNode.propertyGuid);

node.drawState = propertyNode.drawState;
AddNodeNoValidate(node);
foreach (var edge in GetEdges(slot.slotReference).ToArray())
s_TempEdges.Clear();
GetEdges(slot.slotReference, s_TempEdges);
foreach (var edge in s_TempEdges)
ConnectNoValidate(node.GetSlotReference(slotId), edge.inputSlot);
RemoveNodeNoValidate(propertyNode);

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractShaderProperty.cs


set { m_Name = value; }
}
string m_DefaultReferenceName;
return string.IsNullOrEmpty(overrideReferenceName)
? string.Format("{0}_{1}", propertyType, GuidEncoder.Encode(guid))
: overrideReferenceName;
if (string.IsNullOrEmpty(overrideReferenceName))
{
if (string.IsNullOrEmpty(m_DefaultReferenceName))
m_DefaultReferenceName = string.Format("{0}_{1}", propertyType, GuidEncoder.Encode(guid));
return m_DefaultReferenceName;
}
return overrideReferenceName;
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorShaderProperty.cs


{
return new PreviewProperty
{
m_Name = referenceName,
m_PropType = PropertyType.Color,
m_Color = value
name = referenceName,
propType = PropertyType.Color,
colorValue = value
};
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapInputMaterialSlot.cs


{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = PropertyType.Cubemap,
m_Cubemap = cubemap
name = name,
propType = PropertyType.Cubemap,
cubemapValue = cubemap
};
return pp;
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapShaderProperty.cs


{
return new PreviewProperty()
{
m_Name = referenceName,
m_PropType = PropertyType.Cubemap,
m_Cubemap = value.cubemap
name = referenceName,
propType = PropertyType.Cubemap,
cubemapValue = value.cubemap
};
}
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs


{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, value.w),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, value.w),
name = name,
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value.x, value.y, value.z, value.w),
floatValue = value.x,
colorValue = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/FloatShaderProperty.cs


{
return new PreviewProperty()
{
m_Name = referenceName,
m_PropType = PropertyType.Float,
m_Float = value
name = referenceName,
propType = PropertyType.Float,
floatValue = value
};
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs


public virtual PreviewProperty GetPreviewProperty(string name)
{
return null;
return default(PreviewProperty);
}
public abstract void CopyValuesFrom(MaterialSlot foundSlot);

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PreviewProperty.cs


namespace UnityEditor.ShaderGraph
{
public class PreviewProperty
public struct PreviewProperty
public string m_Name;
public PropertyType m_PropType;
public string name;
public PropertyType propType;
public Color m_Color;
public Texture m_Texture;
public Cubemap m_Cubemap;
public Vector4 m_Vector4;
public float m_Float;
public Color colorValue;
public Texture textureValue;
public Cubemap cubemapValue;
public Vector4 vector4Value;
public float floatValue;
}
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SamplerStateShaderProperty.cs


using System;
using System;
using UnityEngine;
namespace UnityEditor.ShaderGraph

public override PreviewProperty GetPreviewMaterialProperty()
{
return null;
return default(PreviewProperty);
}
}
}

36
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableTexture.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class SerializableTexture
public class SerializableTexture : ISerializationCallbackReceiver
{
[SerializeField]
private string m_SerializedTexture;

public Texture texture;
}
Texture m_Texture;
if (string.IsNullOrEmpty(m_SerializedTexture))
return null;
var tex = new TextureHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex);
return tex.texture;
if (m_Texture == null && !string.IsNullOrEmpty(m_SerializedTexture))
{
var tex = new TextureHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex);
m_Texture = tex.texture;
m_SerializedTexture = null;
}
return m_Texture;
set
{
if (texture == value)
return;
set { m_Texture = value; }
}
var tex = new TextureHelper();
tex.texture = value;
m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
}
public void OnBeforeSerialize()
{
var tex = new TextureHelper { texture = texture };
m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
}
public void OnAfterDeserialize()
{
}
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs


{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = PropertyType.Texture,
m_Texture = texture
name = name,
propType = PropertyType.Texture,
textureValue = texture
};
return pp;
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TextureShaderProperty.cs


using System;
using System;
using System.Text;
using UnityEngine;

{
return new PreviewProperty()
{
m_Name = referenceName,
m_PropType = PropertyType.Texture,
m_Texture = value.texture
name = referenceName,
propType = PropertyType.Texture,
textureValue = value.texture
};
}
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs


{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value, value, value, value),
m_Float = value,
m_Color = new Vector4(value, value, value, value),
name = name,
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value, value, value, value),
floatValue = value,
colorValue = new Vector4(value, value, value, value),
};
return pp;
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs


{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, 0, 0),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, 0, 0),
name = name,
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value.x, value.y, 0, 0),
floatValue = value.x,
colorValue = new Vector4(value.x, value.x, 0, 0),
};
return pp;
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2ShaderProperty.cs


{
return new PreviewProperty()
{
m_Name = referenceName,
m_PropType = PropertyType.Vector2,
m_Vector4 = value
name = referenceName,
propType = PropertyType.Vector2,
vector4Value = value
};
}
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs


{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, 0),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, 0),
name = name,
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value.x, value.y, value.z, 0),
floatValue = value.x,
colorValue = new Vector4(value.x, value.x, value.z, 0),
};
return pp;
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3ShaderProperty.cs


{
return new PreviewProperty()
{
m_Name = referenceName,
m_PropType = PropertyType.Vector3,
m_Vector4 = value
name = referenceName,
propType = PropertyType.Vector3,
vector4Value = value
};
}
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs


{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, value.w),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, value.w),
name = name,
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value.x, value.y, value.z, value.w),
floatValue = value.x,
colorValue = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4ShaderProperty.cs


{
return new PreviewProperty()
{
m_Name = referenceName,
m_PropType = PropertyType.Vector4,
m_Vector4 = value
name = referenceName,
propType = PropertyType.Vector4,
vector4Value = value
};
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireBitangent.cs


using UnityEditor.Graphing;
}
public static class MayRequireBitangentExtensions
{
public static NeededCoordinateSpace RequiresBitangent(this ISlot slot)
{
var mayRequireBitangent = slot as IMayRequireBitangent;
return mayRequireBitangent != null ? mayRequireBitangent.RequiresBitangent() : NeededCoordinateSpace.None;
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireNormal.cs


using UnityEditor.Graphing;
}
public static class MayRequireNormalExtensions
{
public static NeededCoordinateSpace RequiresNormal(this ISlot slot)
{
var mayRequireNormal = slot as IMayRequireNormal;
return mayRequireNormal != null ? mayRequireNormal.RequiresNormal() : NeededCoordinateSpace.None;
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequirePosition.cs


using UnityEditor.Graphing;
}
public static class MayRequirePositionExtensions
{
public static NeededCoordinateSpace RequiresPosition(this ISlot slot)
{
var mayRequirePosition = slot as IMayRequirePosition;
return mayRequirePosition != null ? mayRequirePosition.RequiresPosition() : NeededCoordinateSpace.None;
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireTangent.cs


using UnityEditor.Graphing;
}
public static class MayRequireTangentExtensions
{
public static NeededCoordinateSpace RequiresTangent(this ISlot slot)
{
var mayRequireTangent = slot as IMayRequireTangent;
return mayRequireTangent != null ? mayRequireTangent.RequiresTangent() : NeededCoordinateSpace.None;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireTime.cs


using UnityEditor.Graphing;
}
public static class MayRequireTimeExtensions
{
public static bool RequiresTime(this INode node)
{
var mayRequireTime = node as IMayRequireTime;
return mayRequireTime != null && mayRequireTime.RequiresTime();
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireViewDirection.cs


using UnityEditor.Graphing;
}
public static class MayRequireViewDirectionExtensions
{
public static NeededCoordinateSpace RequiresViewDirection(this ISlot slot)
{
var mayRequireViewDirection = slot as IMayRequireViewDirection;
return mayRequireViewDirection != null ? mayRequireViewDirection.RequiresViewDirection() : NeededCoordinateSpace.None;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/NeededCoordinateSpace.cs


using System;
using System;
using System.Linq;
namespace UnityEditor.ShaderGraph
{

public static class CoordinateSpaceNameExtensions
{
static int s_SpaceCount = Enum.GetValues(typeof(CoordinateSpace)).Length;
static int s_InterpolatorCount = Enum.GetValues(typeof(InterpolatorType)).Length;
static string[] s_VariableNames = new string[s_SpaceCount * s_InterpolatorCount];
return string.Format("{0}Space{1}", space, type);
var index = (int)space * s_SpaceCount + (int)type;
if (string.IsNullOrEmpty(s_VariableNames[index]))
s_VariableNames[index] = string.Format("{0}Space{1}", space, type);
return s_VariableNames[index];
}
public static NeededCoordinateSpace ToNeededCoordinateSpace(this CoordinateSpace space)

62
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs


[Serializable]
public abstract class AbstractMaterialNode : SerializableNode, IGenerateProperties
{
protected static List<MaterialSlot> s_TempSlots = new List<MaterialSlot>();
protected static List<IEdge> s_TempEdges = new List<IEdge>();
public enum OutputPrecision
{
@fixed,

protected set { m_HasError = value; }
}
string m_DefaultVariableName;
string m_NameForDefaultVariableName;
Guid m_GuidForDefaultVariableName;
string defaultVariableName
{
get
{
if (m_NameForDefaultVariableName != name || m_GuidForDefaultVariableName != guid)
{
m_DefaultVariableName = string.Format("{0}_{1}", GetHLSLSafeName(name), GuidEncoder.Encode(guid));
m_NameForDefaultVariableName = name;
m_GuidForDefaultVariableName = guid;
}
return m_DefaultVariableName;
}
}
protected AbstractMaterialNode()
{
version = 0;

{
foreach (var inputSlot in GetInputSlots<MaterialSlot>())
foreach (var inputSlot in this.GetInputSlots<MaterialSlot>())
{
var edges = owner.GetEdges(inputSlot.slotReference);
if (edges.Any())

// all children nodes needs to be updated first
// so do that here
foreach (var inputSlot in GetInputSlots<MaterialSlot>())
var slots = ListPool<MaterialSlot>.Get();
GetInputSlots(slots);
foreach (var inputSlot in slots)
{
inputSlot.hasError = false;

isInError = true;
}
}
ListPool<MaterialSlot>.Release(slots);
foreach (var inputSlot in GetInputSlots<MaterialSlot>())
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var inputSlot in s_TempSlots)
{
// if there is a connection
var edges = owner.GetEdges(inputSlot.slotReference).ToList();

foreach (var skippedSlot in skippedDynamicSlots)
skippedSlot.SetConcreteType(dynamicType);
var inputError = GetInputSlots<MaterialSlot>().Any(x => x.hasError);
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
var inputError = s_TempSlots.Any(x => x.hasError);
foreach (var outputSlot in GetOutputSlots<MaterialSlot>())
s_TempSlots.Clear();
GetOutputSlots(s_TempSlots);
foreach (var outputSlot in s_TempSlots)
{
outputSlot.hasError = false;

}
isInError |= inputError;
isInError |= GetOutputSlots<MaterialSlot>().Any(x => x.hasError);
s_TempSlots.Clear();
GetOutputSlots(s_TempSlots);
isInError |= s_TempSlots.Any(x => x.hasError);
isInError |= CalculateNodeHasError();
hasError = isInError;

public virtual void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
var validSlots = GetInputSlots<MaterialSlot>().ToArray();
for (var index = 0; index < validSlots.Length; index++)
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var s in s_TempSlots)
var s = validSlots[index];
var edges = owner.GetEdges(s.slotReference);
if (edges.Any())
s_TempEdges.Clear();
owner.GetEdges(s.slotReference, s_TempEdges);
if (s_TempEdges.Any())
if (item == null)
if (item.name == null)
continue;
properties.Add(item);

var slot = FindSlot<MaterialSlot>(slotId);
if (slot == null)
throw new ArgumentException(string.Format("Attempting to use MaterialSlot({0}) on node of type {1} where this slot can not be found", slotId, this), "slotId");
return "_" + GetVariableNameForNode() + "_" + GetHLSLSafeName(slot.shaderOutputName);
return string.Format("_{0}_{1}", GetVariableNameForNode(), GetHLSLSafeName(slot.shaderOutputName));
return GetHLSLSafeName(name) + "_" + GuidEncoder.Encode(guid);
return defaultVariableName;
}
public static string GetHLSLSafeName(string input)

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Adjustment/ChannelMixerNode.cs


properties.Add(new PreviewProperty()
{
m_Name = string.Format("_{0}_Red", GetVariableNameForNode()),
m_PropType = PropertyType.Vector3,
m_Vector4 = channelMixer.outRed
name = string.Format("_{0}_Red", GetVariableNameForNode()),
propType = PropertyType.Vector3,
vector4Value = channelMixer.outRed
m_Name = string.Format("_{0}_Green", GetVariableNameForNode()),
m_PropType = PropertyType.Vector3,
m_Vector4 = channelMixer.outGreen
name = string.Format("_{0}_Green", GetVariableNameForNode()),
propType = PropertyType.Vector3,
vector4Value = channelMixer.outGreen
m_Name = string.Format("_{0}_Blue", GetVariableNameForNode()),
m_PropType = PropertyType.Vector3,
m_Vector4 = channelMixer.outBlue
name = string.Format("_{0}_Blue", GetVariableNameForNode()),
propType = PropertyType.Vector3,
vector4Value = channelMixer.outBlue
});
}

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


properties.Add(new PreviewProperty()
{
m_Name = string.Format("_{0}_Flip", GetVariableNameForNode()),
m_PropType = PropertyType.Vector4,
m_Vector4 = new Vector4(Convert.ToInt32(m_RedChannel), Convert.ToInt32(m_GreenChannel), Convert.ToInt32(m_BlueChannel), Convert.ToInt32(m_AlphaChannel)),
name = string.Format("_{0}_Flip", GetVariableNameForNode()),
propType = PropertyType.Vector4,
vector4Value = new Vector4(Convert.ToInt32(m_RedChannel), Convert.ToInt32(m_GreenChannel), Convert.ToInt32(m_BlueChannel), Convert.ToInt32(m_AlphaChannel)),
});
}

50
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs


public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
foreach (var outSlot in GetOutputSlots<MaterialSlot>())
s_TempSlots.Clear();
GetOutputSlots(s_TempSlots);
foreach (var outSlot in s_TempSlots)
{
visitor.AddShaderChunk(GetParamTypeName(outSlot) + " " + GetVariableNameForSlot(outSlot.id) + ";", true);
}

foreach (var slot in GetSlots<MaterialSlot>().OrderBy(x => x.id))
s_TempSlots.Clear();
GetSlots(s_TempSlots);
s_TempSlots.Sort((slot1, slot2) => slot1.id.CompareTo(slot2.id));
foreach (var slot in s_TempSlots)
{
if (!first)
{

{
string header = "void " + GetFunctionName() + "(";
s_TempSlots.Clear();
GetSlots(s_TempSlots);
s_TempSlots.Sort((slot1, slot2) => slot1.id.CompareTo(slot2.id));
foreach (var slot in GetSlots<MaterialSlot>().OrderBy(x => x.id))
foreach (var slot in s_TempSlots)
{
if (!first)
header += ", ";

return string.Empty;
result = result.Replace("{precision}", precision.ToString());
foreach (var slot in GetSlots<MaterialSlot>())
s_TempSlots.Clear();
GetSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
{
var toReplace = string.Format("{{slot{0}dimension}}", slot.id);
var replacement = GetSlotDimension(slot.concreteValueType);

public NeededCoordinateSpace RequiresNormal()
{
var binding = NeededCoordinateSpace.None;
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireNormal>())
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
binding |= slot.RequiresNormal();
return binding;
}

var binding = NeededCoordinateSpace.None;
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireViewDirection>())
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
binding |= slot.RequiresViewDirection();
return binding;
}

s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequirePosition>())
foreach (var slot in s_TempSlots)
binding |= slot.RequiresPosition();
return binding;
}

s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireTangent>())
foreach (var slot in s_TempSlots)
binding |= slot.RequiresTangent();
return binding;
}

s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireBitangent>())
foreach (var slot in s_TempSlots)
binding |= slot.RequiresBitangent();
return binding;
}

foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireMeshUV>())
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
{
if (slot.RequiresMeshUV(channel))
return true;

public bool RequiresScreenPosition()
{
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireScreenPosition>())
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
{
if (slot.RequiresScreenPosition())
return true;

public bool RequiresVertexColor()
{
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireVertexColor>())
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
{
if (slot.RequiresVertexColor())
return true;

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs


{
properties.Add(new PreviewProperty
{
m_Name = GetVariableNameForNode(),
m_PropType = PropertyType.Color,
m_Color = color
name = GetVariableNameForNode(),
propType = PropertyType.Color,
colorValue = color
});
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector1Node.cs


{
properties.Add(new PreviewProperty()
{
m_Name = GetVariableNameForNode(),
m_PropType = PropertyType.Float,
m_Float = m_Value
name = GetVariableNameForNode(),
propType = PropertyType.Float,
floatValue = m_Value
});
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector2Node.cs


{
properties.Add(new PreviewProperty()
{
m_Name = GetVariableNameForNode(),
m_PropType = PropertyType.Vector2,
m_Vector4 = m_Value
name = GetVariableNameForNode(),
propType = PropertyType.Vector2,
vector4Value = m_Value
});
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector3Node.cs


{
properties.Add(new PreviewProperty()
{
m_Name = GetVariableNameForNode(),
m_PropType = PropertyType.Vector3,
m_Vector4 = m_Value
name = GetVariableNameForNode(),
propType = PropertyType.Vector3,
vector4Value = m_Value
});
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector4Node.cs


{
properties.Add(new PreviewProperty()
{
m_Name = GetVariableNameForNode(),
m_PropType = PropertyType.Vector4,
m_Vector4 = m_Value
name = GetVariableNameForNode(),
propType = PropertyType.Vector4,
vector4Value = m_Value
});
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Geometry/ScreenPositionNode.cs


namespace UnityEditor.ShaderGraph
{
public interface IMayRequireScreenPosition
{
bool RequiresScreenPosition();
}
public enum ScreenSpaceType
{
Default,

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Geometry/UVNode.cs


namespace UnityEditor.ShaderGraph
{
interface IMayRequireMeshUV
{
bool RequiresMeshUV(UVChannel channel);
}
[Title("Input", "Geometry", "UV")]
public class UVNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireMeshUV
{

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Geometry/VertexColorNode.cs


namespace UnityEditor.ShaderGraph
{
interface IMayRequireVertexColor
{
bool RequiresVertexColor();
}
[Title("Input", "Geometry", "Vertex Color")]
public class VertexColorNode : AbstractMaterialNode, IMayRequireVertexColor
{

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapAssetNode.cs


{
properties.Add(new PreviewProperty
{
m_Name = GetVariableNameForSlot(OutputSlotId),
m_PropType = PropertyType.Cubemap,
m_Cubemap = cubemap
name = GetVariableNameForSlot(OutputSlotId),
propType = PropertyType.Cubemap,
cubemapValue = cubemap
});
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DAssetNode.cs


{
properties.Add(new PreviewProperty
{
m_Name = GetVariableNameForSlot(OutputSlotId),
m_PropType = PropertyType.Texture,
m_Texture = texture
name = GetVariableNameForSlot(OutputSlotId),
propType = PropertyType.Texture,
textureValue = texture
});
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DNode.cs


public bool RequiresMeshUV(UVChannel channel)
{
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireMeshUV>())
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
{
if (slot.RequiresMeshUV(channel))
return true;

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraphNode.cs


// Step 1...
// find out which output slots are actually used
//var validOutputSlots = NodeUtils.GetSlotsThatOutputToNodeRecurse(this, (graph as BaseMaterialGraph).masterNode);
foreach (var slot in GetOutputSlots<MaterialSlot>())
s_TempSlots.Clear();
GetOutputSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
{
var outDimension = ConvertConcreteSlotValueTypeToString(precision, slot.concreteValueType);
outputString.AddShaderChunk(string.Format("{0} {1} = 0;", outDimension, GetVariableNameForSlot(slot.id)), false);

// Step 5...
// Copy the outputs to the parent context name);
foreach (var slot in GetOutputSlots<MaterialSlot>())
s_TempSlots.Clear();
GetOutputSlots(s_TempSlots);
foreach (var slot in s_TempSlots)
{
var inputValue = subGraphOutputNode.GetSlotValue(slot.id, GenerationMode.ForReals);

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraphOutputNode.cs


public virtual int AddSlot()
{
var index = GetInputSlots<ISlot>().Count() + 1;
var index = this.GetInputSlots<ISlot>().Count() + 1;
AddSlot(new Vector4MaterialSlot(index, "Output " + index, "Output" + index, SlotType.Input, Vector4.zero));
return index;
}

var index = GetInputSlots<ISlot>().Count();
var index = this.GetInputSlots<ISlot>().Count();
if (index == 0)
return;

108
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs


}
Stack<Guid> m_Wavefront = new Stack<Guid>();
List<IEdge> m_Edges = new List<IEdge>();
List<MaterialSlot> m_Slots = new List<MaterialSlot>();
void PropagateNodeSet(HashSet<Guid> nodeGuidSet, bool forward = true, IEnumerable<Guid> initialWavefront = null)
{

continue;
// Loop through all nodes that the node feeds into.
foreach (var slot in forward ? node.GetOutputSlots<ISlot>() : node.GetInputSlots<ISlot>())
m_Slots.Clear();
if (forward)
node.GetOutputSlots(m_Slots);
else
node.GetInputSlots(m_Slots);
foreach (var slot in m_Slots)
foreach (var edge in m_Graph.GetEdges(slot.slotReference))
m_Edges.Clear();
m_Graph.GetEdges(slot.slotReference, m_Edges);
foreach (var edge in m_Edges)
{
// We look at each node we feed into.
var connectedSlot = forward ? edge.inputSlot : edge.outputSlot;

ShaderUtil.UpdateShaderAsset(m_UberShader, m_UberShaderString);
File.WriteAllText(Application.dataPath + "/../UberShader.shader", (m_UberShaderString ?? "null").Replace("UnityEngine.MaterialGraph", "Generated"));
var message = "RecreateUberShader: " + Environment.NewLine + m_UberShaderString;
bool uberShaderHasError = false;
uberShaderHasError = true;
}
foreach (var node in uberNodes)

continue;
shaderData.previewMode = m_NodesWith3DPreview.Contains(node.guid) ? PreviewMode.Preview3D : PreviewMode.Preview2D;
shaderData.shader = m_UberShader;
shaderData.hasError = uberShaderHasError;
}
i++;
EditorUtility.DisplayProgressBar("Shader Graph", string.Format("Compiling preview shaders ({0}/{1})", i, count), 0f);

foreach (var previewProperty in m_PreviewProperties)
{
if (previewProperty.m_PropType == PropertyType.Texture && previewProperty.m_Texture != null)
m_PreviewPropertyBlock.SetTexture(previewProperty.m_Name, previewProperty.m_Texture);
else if (previewProperty.m_PropType == PropertyType.Cubemap && previewProperty.m_Cubemap != null)
m_PreviewPropertyBlock.SetTexture(previewProperty.m_Name, previewProperty.m_Cubemap);
else if (previewProperty.m_PropType == PropertyType.Color)
m_PreviewPropertyBlock.SetColor(previewProperty.m_Name, previewProperty.m_Color);
else if (previewProperty.m_PropType == PropertyType.Vector2)
m_PreviewPropertyBlock.SetVector(previewProperty.m_Name, previewProperty.m_Vector4);
else if (previewProperty.m_PropType == PropertyType.Vector3)
m_PreviewPropertyBlock.SetVector(previewProperty.m_Name, previewProperty.m_Vector4);
else if (previewProperty.m_PropType == PropertyType.Vector4)
m_PreviewPropertyBlock.SetVector(previewProperty.m_Name, previewProperty.m_Vector4);
else if (previewProperty.m_PropType == PropertyType.Float)
m_PreviewPropertyBlock.SetFloat(previewProperty.m_Name, previewProperty.m_Float);
if (previewProperty.propType == PropertyType.Texture && previewProperty.textureValue != null)
m_PreviewPropertyBlock.SetTexture(previewProperty.name, previewProperty.textureValue);
else if (previewProperty.propType == PropertyType.Cubemap && previewProperty.cubemapValue != null)
m_PreviewPropertyBlock.SetTexture(previewProperty.name, previewProperty.cubemapValue);
else if (previewProperty.propType == PropertyType.Color)
m_PreviewPropertyBlock.SetColor(previewProperty.name, previewProperty.colorValue);
else if (previewProperty.propType == PropertyType.Vector2)
m_PreviewPropertyBlock.SetVector(previewProperty.name, previewProperty.vector4Value);
else if (previewProperty.propType == PropertyType.Vector3)
m_PreviewPropertyBlock.SetVector(previewProperty.name, previewProperty.vector4Value);
else if (previewProperty.propType == PropertyType.Vector4)
m_PreviewPropertyBlock.SetVector(previewProperty.name, previewProperty.vector4Value);
else if (previewProperty.propType == PropertyType.Float)
m_PreviewPropertyBlock.SetFloat(previewProperty.name, previewProperty.floatValue);
}
m_PreviewProperties.Clear();
}

renderData.texture = null;
continue;
}
if (MaterialGraphAsset.ShaderHasError(renderData.shaderData.shader))
if (renderData.shaderData.hasError)
{
renderData.texture = m_ErrorTexture;
continue;

m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.orthographicSize = 1;
m_SceneResources.camera.orthographic = true;
m_RenderList2D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));
m_PreviewMaterial.shader = renderData.shaderData.shader;
if (m_PreviewMaterial.shader != renderData.shaderData.shader)
m_PreviewMaterial.shader = renderData.shaderData.shader;
m_SceneResources.camera.targetTexture = renderData.renderTexture;
var previousRenderTexure = RenderTexture.active;
RenderTexture.active = renderData.renderTexture;

m_SceneResources.camera.transform.position = -Vector3.forward * 5;
m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.orthographic = false;
foreach (var previewData in m_RenderList3D)
m_RenderList3D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));
foreach (var renderData in m_RenderList3D)
if (m_UberShaderIds.TryGetValue(previewData.shaderData.node.guid, out outputId))
if (m_UberShaderIds.TryGetValue(renderData.shaderData.node.guid, out outputId))
m_PreviewMaterial.shader = previewData.shaderData.shader;
m_SceneResources.camera.targetTexture = previewData.renderTexture;
if (m_PreviewMaterial.shader != renderData.shaderData.shader)
m_PreviewMaterial.shader = renderData.shaderData.shader;
m_SceneResources.camera.targetTexture = renderData.renderTexture;
RenderTexture.active = previewData.renderTexture;
RenderTexture.active = renderData.renderTexture;
Graphics.Blit(Texture2D.whiteTexture, previewData.renderTexture, m_SceneResources.checkerboardMaterial);
var mesh = previewData.mesh ?? m_SceneResources.sphere;
Graphics.Blit(Texture2D.whiteTexture, renderData.renderTexture, m_SceneResources.checkerboardMaterial);
var mesh = renderData.mesh ?? m_SceneResources.sphere;
Unsupported.useScriptableRenderPipeline = previewData.shaderData.node is IMasterNode;
Unsupported.useScriptableRenderPipeline = renderData.shaderData.node is IMasterNode;
previewData.texture = previewData.renderTexture;
renderData.texture = renderData.renderTexture;
foreach (var previewRenderData in m_RenderList2D.Union(m_RenderList3D))
{
if (previewRenderData.onPreviewChanged != null)
previewRenderData.onPreviewChanged();
}
foreach (var renderData in m_RenderList2D)
renderData.NotifyPreviewChanged();
foreach (var renderData in m_RenderList3D)
renderData.NotifyPreviewChanged();
m_RenderList2D.Clear();
m_RenderList3D.Clear();

if (string.IsNullOrEmpty(shaderData.shaderString))
{
if (shaderData.shader != null)
{
ShaderUtil.ClearShaderErrors(shaderData.shader);
shaderData.shader = null;
shaderData.shader = null;
}
}
if (shaderData.shader != null && MaterialGraphAsset.ShaderHasError(shaderData.shader))
{
ShaderUtil.ClearShaderErrors(shaderData.shader);
Object.DestroyImmediate(shaderData.shader, true);
shaderData.shader = null;
}
if (shaderData.shader == null)

// Debug output
var message = "RecreateShader: " + node.GetVariableNameForNode() + Environment.NewLine + shaderData.shaderString;
if (MaterialGraphAsset.ShaderHasError(shaderData.shader))
{
shaderData.hasError = true;
ShaderUtil.ClearShaderErrors(shaderData.shader);
Object.DestroyImmediate(shaderData.shader, true);
shaderData.shader = null;
}
else
{
shaderData.hasError = false;
}
}
void DestroyPreview(Guid nodeGuid, PreviewRenderData previewRenderData)

public Shader shader { get; set; }
public string shaderString { get; set; }
public PreviewMode previewMode { get; set; }
public bool hasError { get; set; }
}
public class PreviewRenderData

public RenderTexture renderTexture { get; set; }
public Texture texture { get; set; }
public OnPreviewChanged onPreviewChanged;
public void NotifyPreviewChanged()
{
if (onPreviewChanged != null)
onPreviewChanged();
}
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/SearchWindowProvider.cs


using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEditor.Graphing;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using INode = UnityEditor.Graphing.INode;

return true;
}
static List<IEdge> s_TempEdges = new List<IEdge>();
bool OnConvertToProperty()
{
if (m_GraphView == null)

var oldSlot = node.FindSlot<MaterialSlot>(converter.outputSlotId);
var newSlot = propNode.FindSlot<MaterialSlot>(PropertyNode.OutputSlotId);
var edges = m_Graph.GetEdges(oldSlot.slotReference).ToArray();
foreach (var edge in edges)
s_TempEdges.Clear();
m_Graph.GetEdges(oldSlot.slotReference, s_TempEdges);
foreach (var edge in s_TempEdges)
m_Graph.Connect(newSlot.slotReference, edge.inputSlot);
m_Graph.RemoveNode(node);

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/Edge.cs


{
unchecked
{
return ((m_OutputSlot != null ? m_OutputSlot.GetHashCode() : 0) * 397) ^ (m_InputSlot != null ? m_InputSlot.GetHashCode() : 0);
// Can't make fields readonly due to Unity serialization
return (m_OutputSlot.GetHashCode() * 397) ^ m_InputSlot.GetHashCode();
}
}
}

49
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableGraph.cs


return new Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo>();
}
static List<IEdge> s_TempEdges = new List<IEdge>();
if (fromSlotRef == null || toSlotRef == null)
return null;
var fromNode = GetNodeFromGuid(fromSlotRef.nodeGuid);
var toNode = GetNodeFromGuid(toSlotRef.nodeGuid);

var fromSlot = fromNode.FindSlot<ISlot>(fromSlotRef.slotId);
var toSlot = toNode.FindSlot<ISlot>(toSlotRef.slotId);
SlotReference outputSlot = null;
SlotReference inputSlot = null;
if (fromSlot.isOutputSlot == toSlot.isOutputSlot)
return null;
// output must connect to input
if (fromSlot.isOutputSlot)
outputSlot = fromSlotRef;
else if (fromSlot.isInputSlot)
inputSlot = fromSlotRef;
var outputSlot = fromSlot.isOutputSlot ? fromSlotRef : toSlotRef;
var inputSlot = fromSlot.isInputSlot ? fromSlotRef : toSlotRef;
if (toSlot.isOutputSlot)
outputSlot = toSlotRef;
else if (toSlot.isInputSlot)
inputSlot = toSlotRef;
if (inputSlot == null || outputSlot == null)
return null;
var slotEdges = GetEdges(inputSlot).ToList();
s_TempEdges.Clear();
GetEdges(inputSlot, s_TempEdges);
foreach (var edge in slotEdges)
foreach (var edge in s_TempEdges)
{
RemoveEdgeNoValidate(edge);
}

return default(T);
}
public IEnumerable<IEdge> GetEdges(SlotReference s)
public void GetEdges(SlotReference s, List<IEdge> foundEdges)
if (s == null)
return Enumerable.Empty<IEdge>();
return Enumerable.Empty<IEdge>();
return;
return Enumerable.Empty<IEdge>();
return;
return candidateEdges.Where(candidateEdge =>
{
var cs = slot.isInputSlot ? candidateEdge.inputSlot : candidateEdge.outputSlot;
return cs.nodeGuid == s.nodeGuid && cs.slotId == s.slotId;
});
foreach (var edge in candidateEdges)
{
var cs = slot.isInputSlot ? edge.inputSlot : edge.outputSlot;
if (cs.nodeGuid == s.nodeGuid && cs.slotId == s.slotId)
foundEdges.Add(edge);
}
}
public virtual void OnBeforeSerialize()

49
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableNode.cs


public OnNodeModified onModified { get; set; }
public IEnumerable<T> GetInputSlots<T>() where T : ISlot
public void GetInputSlots<T>(List<T> foundSlots) where T : ISlot
return GetSlots<T>().Where(x => x.isInputSlot);
foreach (var slot in m_Slots)
{
if (slot.isInputSlot && slot is T)
foundSlots.Add((T)slot);
}
public IEnumerable<T> GetOutputSlots<T>() where T : ISlot
public void GetOutputSlots<T>(List<T> foundSlots) where T : ISlot
return GetSlots<T>().Where(x => x.isOutputSlot);
foreach (var slot in m_Slots)
{
if (slot.isOutputSlot && slot is T)
foundSlots.Add((T)slot);
}
public IEnumerable<T> GetSlots<T>() where T : ISlot
public void GetSlots<T>(List<T> foundSlots) where T : ISlot
return m_Slots.OfType<T>();
foreach (var slot in m_Slots)
{
if (slot is T)
foundSlots.Add((T)slot);
}
}
public virtual void AddSlot(ISlot slot)

{
var slot = FindSlot<ISlot>(slotId);
if (slot == null)
return null;
throw new ArgumentException("Slot could not be found", "slotId");
return GetSlots<T>().FirstOrDefault(x => x.id == slotId);
foreach (var slot in m_Slots)
{
if (slot.id == slotId && slot is T)
return (T)slot;
}
return default(T);
return GetInputSlots<T>().FirstOrDefault(x => x.id == slotId);
foreach (var slot in m_Slots)
{
if (slot.isInputSlot && slot.id == slotId && slot is T)
return (T)slot;
}
return default(T);
return GetOutputSlots<T>().FirstOrDefault(x => x.id == slotId);
foreach (var slot in m_Slots)
{
if (slot.isOutputSlot && slot.id == slotId && slot is T)
return (T)slot;
}
return default(T);
return GetInputSlots<ISlot>().Where(x => !owner.GetEdges(GetSlotReference(x.id)).Any());
return this.GetInputSlots<ISlot>().Where(x => !owner.GetEdges(GetSlotReference(x.id)).Any());
}
public virtual void OnBeforeSerialize()

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Interfaces/IGraph.cs


INode GetNodeFromGuid(Guid guid);
bool ContainsNodeGuid(Guid guid);
T GetNodeFromGuid<T>(Guid guid) where T : INode;
IEnumerable<IEdge> GetEdges(SlotReference s);
void GetEdges(SlotReference s, List<IEdge> foundEdges);
void ValidateGraph();
void ReplaceWith(IGraph other);
IGraphObject owner { get; set; }

IEnumerable<IEdge> removedEdges { get; }
void ClearChanges();
}
public static class GraphExtensions
{
public static IEnumerable<IEdge> GetEdges(this IGraph graph, SlotReference s)
{
var edges = new List<IEdge>();
graph.GetEdges(s, edges);
return edges;
}
}
}

30
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Interfaces/INode.cs


Guid RewriteGuid();
string name { get; set; }
bool canDeleteNode { get; }
IEnumerable<T> GetInputSlots<T>() where T : ISlot;
IEnumerable<T> GetOutputSlots<T>() where T : ISlot;
IEnumerable<T> GetSlots<T>() where T : ISlot;
void GetInputSlots<T>(List<T> foundSlots) where T : ISlot;
void GetOutputSlots<T>(List<T> foundSlots) where T : ISlot;
void GetSlots<T>(List<T> foundSlots) where T : ISlot;
void AddSlot(ISlot slot);
void RemoveSlot(int slotId);
SlotReference GetSlotReference(int slotId);

bool hasError { get; }
void ValidateNode();
void UpdateNodeAfterDeserialization();
}
public static class NodeExtensions
{
public static IEnumerable<T> GetSlots<T>(this INode node) where T : ISlot
{
var slots = new List<T>();
node.GetSlots(slots);
return slots;
}
public static IEnumerable<T> GetInputSlots<T>(this INode node) where T : ISlot
{
var slots = new List<T>();
node.GetInputSlots(slots);
return slots;
}
public static IEnumerable<T> GetOutputSlots<T>(this INode node) where T : ISlot
{
var slots = new List<T>();
node.GetOutputSlots(slots);
return slots;
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Interfaces/SlotReference.cs


namespace UnityEditor.Graphing
{
[Serializable]
public class SlotReference : ISerializationCallbackReceiver, IEquatable<SlotReference>
public struct SlotReference : ISerializationCallbackReceiver, IEquatable<SlotReference>
{
[SerializeField]
private int m_SlotId;

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Testing/UnitTests/SerializedGraphTests.cs


Assert.AreEqual(2, graph.GetNodes<INode>().Count());
var createdEdge = graph.Connect(outputNode.GetSlotReference(TestableNode.Output0), null);
Assert.AreEqual(0, graph.edges.Count());
Assert.IsNull(createdEdge);
var createdEdge2 = graph.Connect(outputNode.GetSlotReference(TestableNode.Output0), new SlotReference(Guid.NewGuid(), 666));
Assert.AreEqual(0, graph.edges.Count());
Assert.IsNull(createdEdge2);

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs


Assert.AreEqual(1, properties.Count);
var pp = properties.FirstOrDefault();
Assert.AreEqual(m_NodeA.GetVariableNameForSlot(slot.id), pp.m_Name);
Assert.AreEqual(PropertyType.Float, pp.m_PropType);
Assert.AreEqual(slot.value, pp.m_Vector4);
Assert.AreEqual(m_NodeA.GetVariableNameForSlot(slot.id), pp.name);
Assert.AreEqual(PropertyType.Float, pp.propType);
Assert.AreEqual(slot.value, pp.vector4Value);
}
[Test]

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireMeshUV.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public interface IMayRequireMeshUV
{
bool RequiresMeshUV(UVChannel channel);
}
public static class MayRequireMeshUVExtensions
{
public static bool RequiresMeshUV(this ISlot slot, UVChannel channel)
{
var mayRequireMeshUV = slot as IMayRequireMeshUV;
return mayRequireMeshUV != null && mayRequireMeshUV.RequiresMeshUV(channel);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireMeshUV.cs.meta


fileFormatVersion: 2
guid: c23e123c3ff140c997d68b663b484f29
timeCreated: 1512469718

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireScreenPosition.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public interface IMayRequireScreenPosition
{
bool RequiresScreenPosition();
}
public static class MayRequireScreenPositionExtensions
{
public static bool RequiresScreenPosition(this ISlot slot)
{
var mayRequireScreenPosition = slot as IMayRequireScreenPosition;
return mayRequireScreenPosition != null && mayRequireScreenPosition.RequiresScreenPosition();
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireScreenPosition.cs.meta


fileFormatVersion: 2
guid: f7223ad4b07146cc98966aad000f017d
timeCreated: 1512469767

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireVertexColor.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
interface IMayRequireVertexColor
{
bool RequiresVertexColor();
}
public static class MayRequireVertexColorExtensions
{
public static bool RequiresVertexColor(this ISlot slot)
{
var mayRequireVertexColor = slot as IMayRequireVertexColor;
return mayRequireVertexColor != null && mayRequireVertexColor.RequiresVertexColor();
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IMayRequireVertexColor.cs.meta


fileFormatVersion: 2
guid: 04d654703054418c8db537b52ae070dc
timeCreated: 1512469863

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/MaterialNodeExtensions.cs.meta


fileFormatVersion: 2
guid: 73ed72480b864b078821956f4f00da99
timeCreated: 1503563173

13
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/MaterialNodeExtensions.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public static class MaterialNodeExtensions
{
public static bool RequiresTime(this INode node)
{
var timeNode = node as IMayRequireTime;
return timeNode != null && timeNode.RequiresTime();
}
}
}
正在加载...
取消
保存