浏览代码

Various refactorings and improvements (#139)

* Move previewMode to RenderData instead of ShaderData (still some to do here, as apparently there's still a RenderData _and a_ ShaderData per node, but the intention was to have a RenderData per node and then share ShaderData between those)
* Introduce a temporary identifier for nodes. The first part of this identifier is an index (`int`) which is assigned linearly. When a node is deleted, its identifier as added to a free list. Each identifier also contains a version (`int`), which is incremented when the identifier is re-used. This allows us to replace usage of Dictionary with List in a lot of places.
* Introduce IndexSet for efficiently storing and manipulating a list of indices (e.g. from temp IDs). Internally this stores a list of 32-bit integers and uses each bit to represent 1 index. The operations of `ISet<int>` are implemented for this type, although that interface does not exist in Unity. The operations are very fast and cac...
/main
GitHub 7 年前
当前提交
09d669c8
共有 86 个文件被更改,包括 991 次插入790 次删除
  1. 3
      MaterialGraphProject/Assets/NewNodes/Editor/FunctionNAddNode.cs
  2. 6
      MaterialGraphProject/Assets/NewNodes/Editor/Keep/ConvolutionFilterNode.cs
  3. 5
      MaterialGraphProject/Assets/NewNodes/Editor/Kill/ScatterNode.cs
  4. 5
      MaterialGraphProject/Assets/NewNodes/Editor/Kill/ToggleNode.cs
  5. 5
      MaterialGraphProject/Assets/NewNodes/Editor/Kill/UVTransform.cs
  6. 5
      MaterialGraphProject/Assets/NewNodes/Editor/SceneDepthNode.cs
  7. 5
      MaterialGraphProject/Assets/NewNodes/Editor/SceneVelocityNode.cs
  8. 71
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  9. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorMaterialSlot.cs
  10. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorRGBMaterialSlot.cs
  11. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorShaderProperty.cs
  12. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapInputMaterialSlot.cs
  13. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapShaderProperty.cs
  14. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs
  15. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/FloatShaderProperty.cs
  16. 137
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PreviewProperty.cs
  17. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs
  18. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TextureShaderProperty.cs
  19. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs
  20. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs
  21. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2ShaderProperty.cs
  22. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs
  23. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3ShaderProperty.cs
  24. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs
  25. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4ShaderProperty.cs
  26. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/Graph/INode.cs
  27. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs
  28. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs
  29. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/PBRMasterNode.cs
  30. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
  31. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Adjustment/ChannelMixerNode.cs
  32. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Adjustment/HueNode.cs
  33. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Blend/BlendNode.cs
  34. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ChannelMaskNode.cs
  35. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Utility/ColorspaceConversion.cs
  36. 23
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/FlipNode.cs
  37. 23
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/SwizzleNode.cs
  38. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/GeometryNode.cs
  39. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs
  40. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ConstantNode.cs
  41. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector1Node.cs
  42. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector2Node.cs
  43. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector3Node.cs
  44. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector4Node.cs
  45. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Geometry/ScreenPositionNode.cs
  46. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Geometry/UVNode.cs
  47. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix2Node.cs
  48. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix3Node.cs
  49. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix4Node.cs
  50. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/TransformationMatrixNode.cs
  51. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PropertyNode.cs
  52. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapAssetNode.cs
  53. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SampleTexture2DNode.cs
  54. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SamplerStateNode.cs
  55. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DAssetNode.cs
  56. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Advanced/ExponentialNode.cs
  57. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Advanced/LogNode.cs
  58. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Advanced/ReciprocalNode.cs
  59. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs
  60. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/UV/RotateNode.cs
  61. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Utility/SubGraphNode.cs
  62. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GenerationResults.cs
  63. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs
  64. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  65. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs
  66. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  67. 548
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs
  68. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/ColorRGBSlotControlView.cs
  69. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/ColorSlotControlView.cs
  70. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/CubemapSlotControlView.cs
  71. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/MultiFloatSlotControlView.cs
  72. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/TextureSlotControlView.cs
  73. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/UVSlotControlView.cs
  74. 2
      MaterialGraphProject/ProjectSettings/ProjectVersion.txt
  75. 37
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/Identifier.cs
  76. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/Identifier.cs.meta
  77. 357
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/IndexSet.cs
  78. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/IndexSet.cs.meta
  79. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/AbstractNodeEditorView.cs.meta
  80. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/NodeEditorHeaderView.cs.meta
  81. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/StandardNodeEditorView.cs.meta
  82. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/SurfaceMasterNodeEditorView.cs.meta
  83. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/AbstractNodeEditorView.cs
  84. 32
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/NodeEditorHeaderView.cs
  85. 48
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/StandardNodeEditorView.cs
  86. 78
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/SurfaceMasterNodeEditorView.cs

3
MaterialGraphProject/Assets/NewNodes/Editor/FunctionNAddNode.cs


public void OnModified()
{
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
protected override string GetFunctionName()

6
MaterialGraphProject/Assets/NewNodes/Editor/Keep/ConvolutionFilterNode.cs


m_ConvolutionFilter[6].w = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
public float GetConvolutionWeight(int row, int col)

default: m_ConvolutionFilter[vectorIndex].w = value; break;
}
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
protected override string GetFunctionName()

5
MaterialGraphProject/Assets/NewNodes/Editor/Kill/ScatterNode.cs


}
m_num = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

5
MaterialGraphProject/Assets/NewNodes/Editor/Kill/ToggleNode.cs


return;
m_ToggleState = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

5
MaterialGraphProject/Assets/NewNodes/Editor/Kill/UVTransform.cs


return;
m_constant = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}
public UVTransform()

5
MaterialGraphProject/Assets/NewNodes/Editor/SceneDepthNode.cs


return;
m_SceneDepthMode = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

5
MaterialGraphProject/Assets/NewNodes/Editor/SceneVelocityNode.cs


return;
m_SceneVelocityMode = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


#region Node data
[NonSerialized]
Dictionary<Guid, INode> m_Nodes = new Dictionary<Guid, INode>();
Stack<Identifier> m_FreeNodeTempIds = new Stack<Identifier>();
[NonSerialized]
List<AbstractMaterialNode> m_Nodes = new List<AbstractMaterialNode>();
[NonSerialized]
Dictionary<Guid, INode> m_NodeDictionary = new Dictionary<Guid, INode>();
return m_Nodes.Values.OfType<T>();
return m_Nodes.Where(x => x != null).OfType<T>();
}
[SerializeField]

void AddNodeNoValidate(INode node)
{
m_Nodes.Add(node.guid, node);
node.owner = this;
m_AddedNodes.Add(node);
var materialNode = (AbstractMaterialNode)node;
materialNode.owner = this;
if (m_FreeNodeTempIds.Any())
{
var id = m_FreeNodeTempIds.Pop();
id.IncrementVersion();
materialNode.tempId = id;
m_Nodes[id.index] = materialNode;
}
else
{
var id = new Identifier(m_Nodes.Count);
materialNode.tempId = id;
m_Nodes.Add(materialNode);
}
m_NodeDictionary.Add(materialNode.guid, materialNode);
m_AddedNodes.Add(materialNode);
}
public void RemoveNode(INode node)

m_Nodes.Remove(node.guid);
m_RemovedNodes.Add(node);
if (!node.canDeleteNode)
var materialNode = (AbstractMaterialNode)node;
if (!materialNode.canDeleteNode)
m_Nodes.Remove(node.guid);
m_RemovedNodes.Add(node);
m_Nodes[materialNode.tempId.index] = null;
m_FreeNodeTempIds.Push(materialNode.tempId);
m_NodeDictionary.Remove(materialNode.guid);
m_RemovedNodes.Add(materialNode);
}
void AddEdgeToNodeEdges(IEdge edge)

public INode GetNodeFromGuid(Guid guid)
{
INode node;
m_Nodes.TryGetValue(guid, out node);
m_NodeDictionary.TryGetValue(guid, out node);
return node;
}
public INode GetNodeFromTempId(Identifier tempId)
{
var node = m_Nodes[tempId.index];
if (node == null)
throw new Exception("Index does not contain a node.");
if (node.tempId.version != tempId.version)
throw new Exception("Trying to retrieve a node that was removed from the graph.");
return m_Nodes.ContainsKey(guid);
return m_NodeDictionary.ContainsKey(guid);
}
public T GetNodeFromGuid<T>(Guid guid) where T : INode

using (var removedNodesPooledObject = ListPool<Guid>.GetDisposable())
{
var removedNodeGuids = removedNodesPooledObject.value;
removedNodeGuids.AddRange(m_Nodes.Keys);
removedNodeGuids.AddRange(m_Nodes.Where(n => n != null).Select(n => n.guid));
RemoveNodeNoValidate(m_Nodes[nodeGuid]);
RemoveNodeNoValidate(m_NodeDictionary[nodeGuid]);
}
ValidateGraph();

public void OnBeforeSerialize()
{
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes.Values);
m_SerializableNodes = SerializationHelper.Serialize(GetNodes<INode>());
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges);
m_SerializedProperties = SerializationHelper.Serialize<IShaderProperty>(m_Properties);
}

// have to deserialize 'globals' before nodes
m_Properties = SerializationHelper.Deserialize<IShaderProperty>(m_SerializedProperties, null);
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, GraphUtil.GetLegacyTypeRemapping());
m_Nodes = new Dictionary<Guid, INode>(nodes.Count);
foreach (var node in nodes)
m_Nodes = new List<AbstractMaterialNode>(nodes.Count);
m_NodeDictionary = new Dictionary<Guid, INode>(nodes.Count);
foreach (var node in nodes.OfType<AbstractMaterialNode>())
m_Nodes.Add(node.guid, node);
node.tempId = new Identifier(m_Nodes.Count);
m_Nodes.Add(node);
m_NodeDictionary.Add(node.guid, node);
}
m_SerializableNodes = null;

7
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorMaterialSlot.cs


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
var pp = new PreviewProperty(PropertyType.Color)
propType = PropertyType.Color,
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),
colorValue = new Color(value.x, value.x, value.z, value.w),
};
return pp;
}

7
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorRGBMaterialSlot.cs


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
var pp = new PreviewProperty(PropertyType.Color)
propType = PropertyType.Color,
vector4Value = new Vector4(value.x, value.y, value.z, 1),
floatValue = value.x,
colorValue = new Vector4(value.x, value.y, value.z, 1),
colorValue = new Color(value.x, value.y, value.z, 1),
};
return pp;
}

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


public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty
return new PreviewProperty(PropertyType.Color)
propType = PropertyType.Color,
colorValue = value
};
}

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


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
var pp = new PreviewProperty(PropertyType.Cubemap)
propType = PropertyType.Cubemap,
cubemapValue = cubemap
};
return pp;

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


public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()
return new PreviewProperty(PropertyType.Cubemap)
propType = PropertyType.Cubemap,
cubemapValue = value.cubemap
};
}

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


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
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),
};
var propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType);
var pp = new PreviewProperty(propType) { name = name };
if (propType == PropertyType.Float)
pp.floatValue = value.x;
else
pp.vector4Value = new Vector4(value.x, value.y, value.z, value.w);
return pp;
}

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


public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()
return new PreviewProperty(PropertyType.Float)
propType = PropertyType.Float,
floatValue = value
};
}

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


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

public string name;
public PropertyType propType;
public string name { get; set; }
public PropertyType propType { get; private set; }
public PreviewProperty(PropertyType type) : this()
{
propType = type;
}
[StructLayout(LayoutKind.Explicit)]
struct Data
{
[FieldOffset(0)]
public Color colorValue;
[FieldOffset(0)]
public Texture textureValue;
[FieldOffset(0)]
public Cubemap cubemapValue;
[FieldOffset(0)]
public Vector4 vector4Value;
[FieldOffset(0)]
public float floatValue;
}
Data m_Data;
public Color colorValue
{
get
{
if (propType != PropertyType.Color)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Color, propType));
return m_Data.colorValue;
}
set
{
if (propType != PropertyType.Color)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Color, propType));
m_Data.colorValue = value;
}
}
public Texture textureValue
{
get
{
if (propType != PropertyType.Texture)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Texture, propType));
return m_Data.textureValue;
}
set
{
if (propType != PropertyType.Texture)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Texture, propType));
m_Data.textureValue = value;
}
}
public Cubemap cubemapValue
{
get
{
if (propType != PropertyType.Cubemap)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Cubemap, propType));
return m_Data.cubemapValue;
}
set
{
if (propType != PropertyType.Cubemap)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Cubemap, propType));
m_Data.cubemapValue = value;
}
}
public Color colorValue;
public Texture textureValue;
public Cubemap cubemapValue;
public Vector4 vector4Value;
public float floatValue;
public Vector4 vector4Value
{
get
{
if (propType != PropertyType.Vector2 && propType != PropertyType.Vector3 && propType != PropertyType.Vector4)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Vector4, propType));
return m_Data.vector4Value;
}
set
{
if (propType != PropertyType.Vector2 && propType != PropertyType.Vector3 && propType != PropertyType.Vector4)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Vector4, propType));
m_Data.vector4Value = value;
}
}
public float floatValue
{
get
{
if (propType != PropertyType.Float)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Float, propType));
return m_Data.floatValue;
}
set
{
if (propType != PropertyType.Float)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Float, propType));
m_Data.floatValue = value;
}
}
const string k_SetErrorMessage = "Cannot set a {0} property on a PreviewProperty with type {1}.";
const string k_GetErrorMessage = "Cannot get a {0} property on a PreviewProperty with type {1}.";
public void SetMaterialPropertyBlockValue(MaterialPropertyBlock block)
{
if (propType == PropertyType.Texture && textureValue != null)
block.SetTexture(name, m_Data.textureValue);
else if (propType == PropertyType.Cubemap && cubemapValue != null)
block.SetTexture(name, m_Data.cubemapValue);
else if (propType == PropertyType.Color)
block.SetColor(name, m_Data.colorValue);
else if (propType == PropertyType.Vector2 || propType == PropertyType.Vector3 || propType == PropertyType.Vector4)
block.SetVector(name, m_Data.vector4Value);
else if (propType == PropertyType.Float)
block.SetFloat(name, m_Data.floatValue);
}
}
public static class PreviewPropertyExtensions
{
public static void SetPreviewProperty(this MaterialPropertyBlock block, PreviewProperty previewProperty)
{
previewProperty.SetMaterialPropertyBlockValue(block);
}
}
}

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


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
var pp = new PreviewProperty(PropertyType.Texture)
propType = PropertyType.Texture,
textureValue = texture
};
return pp;

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


public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()
return new PreviewProperty(PropertyType.Texture)
propType = PropertyType.Texture,
textureValue = value.texture
};
}

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


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
var pp = new PreviewProperty(PropertyType.Float)
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value, value, value, value),
floatValue = value,
colorValue = new Vector4(value, value, value, value),
floatValue = value
};
return pp;
}

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


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
var pp = new PreviewProperty(PropertyType.Vector2)
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value.x, value.y, 0, 0),
floatValue = value.x,
colorValue = new Vector4(value.x, value.x, 0, 0),
vector4Value = new Vector4(value.x, value.y, 0, 0)
};
return pp;
}

public override void CopyValuesFrom(MaterialSlot foundSlot)
{

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


public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()
return new PreviewProperty(PropertyType.Vector2)
propType = PropertyType.Vector2,
vector4Value = value
};
}

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


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
var pp = new PreviewProperty(PropertyType.Vector3)
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),
vector4Value = new Vector4(value.x, value.y, value.z, 0)
};
return pp;
}

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


public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()
return new PreviewProperty(PropertyType.Vector3)
propType = PropertyType.Vector3,
vector4Value = value
};
}

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


public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
var pp = new PreviewProperty(PropertyType.Vector4)
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
floatValue = value.x,
colorValue = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}

public override void CopyValuesFrom(MaterialSlot foundSlot)
{

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


public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()
return new PreviewProperty(PropertyType.Vector4)
propType = PropertyType.Vector4,
vector4Value = value
};
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/Graph/INode.cs


using System;
using System.Collections.Generic;
using UnityEditor.ShaderGraph;
namespace UnityEditor.Graphing
{

public interface INode
{
OnNodeModified onModified { get; set; }
void Dirty(ModificationScope scope);
Identifier tempId { get; set; }
Guid RewriteGuid();
string name { get; set; }
bool canDeleteNode { get; }

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs


"PopulateSurfaceData",
"SurfaceDescription",
null,
null,
usedSlots);
var graph = new ShaderGenerator();

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs


"PopulateSurfaceData",
"SurfaceDescription",
null,
null,
usedSlots);
var graph = new ShaderGenerator();

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/PBRMasterNode.cs


m_Model = value;
UpdateNodeAfterDeserialization();
if (onModified != null)
{
onModified(this, ModificationScope.Topological);
}
Dirty(ModificationScope.Topological);
}
}

return;
m_AlphaMode = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEditor.Graphing;

[NonSerialized]
private bool m_HasError;
public Identifier tempId { get; set; }
public void Dirty(ModificationScope scope)
{
if (onModified != null)
onModified(this, scope);
}
public Guid guid
{
get { return m_Guid; }

set
{
m_DrawState = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}

if (previewExpanded == value)
return;
m_PreviewExpanded = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}

m_Slots.Add(slot);
slot.owner = this;
if (onModified != null)
{
onModified(this, ModificationScope.Topological);
}
Dirty(ModificationScope.Topological);
if (foundSlot == null)
return;

//remove slots
m_Slots.RemoveAll(x => x.id == slotId);
if (onModified != null)
{
onModified(this, ModificationScope.Topological);
}
Dirty(ModificationScope.Topological);
}
public void RemoveSlotsNameNotMatching(IEnumerable<int> slotIds, bool supressWarnings = false)

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


if ((value.outRed == m_ChannelMixer.outRed) && (value.outGreen == m_ChannelMixer.outGreen) && (value.outBlue == m_ChannelMixer.outBlue))
return;
m_ChannelMixer = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}

{
base.CollectPreviewMaterialProperties(properties);
properties.Add(new PreviewProperty()
properties.Add(new PreviewProperty(PropertyType.Vector3)
propType = PropertyType.Vector3,
properties.Add(new PreviewProperty()
properties.Add(new PreviewProperty(PropertyType.Vector3)
propType = PropertyType.Vector3,
properties.Add(new PreviewProperty()
properties.Add(new PreviewProperty(PropertyType.Vector3)
propType = PropertyType.Vector3,
vector4Value = channelMixer.outBlue
});
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Adjustment/HueNode.cs


return;
m_HueMode = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Blend/BlendNode.cs


return;
m_BlendMode = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


return;
m_Channel = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


if (m_Conversion.Equals(value))
return;
m_Conversion = value;
if (onModified != null)
onModified(this, ModificationScope.Graph);
Dirty(ModificationScope.Graph);
}
}

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


if (m_RedChannel == value.isOn)
return;
m_RedChannel = value.isOn;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

if (m_GreenChannel == value.isOn)
return;
m_GreenChannel = value.isOn;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

if (m_BlueChannel == value.isOn)
return;
m_BlueChannel = value.isOn;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

if (m_AlphaChannel == value.isOn)
return;
m_AlphaChannel = value.isOn;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

{
base.CollectPreviewMaterialProperties(properties);
properties.Add(new PreviewProperty()
properties.Add(new PreviewProperty(PropertyType.Vector4)
propType = PropertyType.Vector4,
vector4Value = new Vector4(Convert.ToInt32(m_RedChannel), Convert.ToInt32(m_GreenChannel), Convert.ToInt32(m_BlueChannel), Convert.ToInt32(m_AlphaChannel)),
});
}

23
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/SwizzleNode.cs


return;
m_RedChannel = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

return;
m_GreenChannel = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

return;
m_BlueChannel = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

return;
m_AlphaChannel = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

base.CollectPreviewMaterialProperties(properties);
// Encode swizzle values into an integer
var value = ((int)redChannel) | ((int)greenChannel << 2) | ((int)blueChannel << 4) | ((int)alphaChannel << 6);
properties.Add(new PreviewProperty
properties.Add(new PreviewProperty(PropertyType.Float)
propType = PropertyType.Float,
floatValue = value
});
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/GeometryNode.cs


return;
m_Space = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


return;
m_Color = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty
properties.Add(new PreviewProperty(PropertyType.Color)
propType = PropertyType.Color,
colorValue = color
});
}

var prop = new ColorShaderProperty();
prop.value = color;
return prop;
return new ColorShaderProperty { value = color };
}
public int outputSlotId { get { return OutputSlotId; } }

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ConstantNode.cs


return;
m_constant = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


m_Value = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}

m_floatType = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}*/

m_rangeValues = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}
*/

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty()
properties.Add(new PreviewProperty(PropertyType.Float)
propType = PropertyType.Float,
floatValue = m_Value
});
}

var prop = new FloatShaderProperty();
prop.value = value;
return prop;
return new FloatShaderProperty { value = value };
}
public int outputSlotId { get { return OutputSlotId; } }

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


m_Value = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty()
properties.Add(new PreviewProperty(PropertyType.Vector2)
propType = PropertyType.Vector2,
vector4Value = m_Value
});
}

var prop = new Vector2ShaderProperty();
prop.value = value;
return prop;
return new Vector2ShaderProperty { value = value };
}
public int outputSlotId { get { return OutputSlotId; } }

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


m_Value = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty()
properties.Add(new PreviewProperty(PropertyType.Vector3)
propType = PropertyType.Vector3,
vector4Value = m_Value
});
}

var prop = new Vector3ShaderProperty();
prop.value = value;
return prop;
return new Vector3ShaderProperty { value = value };
}
public int outputSlotId { get { return OutputSlotId; } }

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


m_Value = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
Dirty(ModificationScope.Node);
}
}

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty()
properties.Add(new PreviewProperty(PropertyType.Vector4)
propType = PropertyType.Vector4,
vector4Value = m_Value
});
}

var prop = new Vector4ShaderProperty();
prop.value = value;
return prop;
return new Vector4ShaderProperty { value = value };
}
public int outputSlotId { get { return OutputSlotId; } }

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


return;
m_ScreenSpaceType = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


return;
m_OutputChannel = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix2Node.cs


if (value == row)
return;
row = value;
if (onModified != null)
onModified(this, ModificationScope.Graph);
Dirty(ModificationScope.Graph);
}
public Matrix2Node()

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix3Node.cs


if (value == row)
return;
row = value;
if (onModified != null)
onModified(this, ModificationScope.Graph);
Dirty(ModificationScope.Graph);
}
public Matrix3Node()

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix4Node.cs


if (value == row)
return;
row = value;
if (onModified != null)
onModified(this, ModificationScope.Graph);
Dirty(ModificationScope.Graph);
}
public Matrix4Node()

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/TransformationMatrixNode.cs


return;
m_matrix = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PropertyNode.cs


UpdateNode();
if (onModified != null)
{
onModified(this, ModificationScope.Topological);
}
Dirty(ModificationScope.Topological);
}
}

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


if (m_Cubemap.cubemap == value)
return;
m_Cubemap.cubemap = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty
properties.Add(new PreviewProperty(PropertyType.Cubemap)
propType = PropertyType.Cubemap,
cubemapValue = cubemap
});
}

var prop = new CubemapShaderProperty();
prop.value = m_Cubemap;
var prop = new CubemapShaderProperty { value = m_Cubemap };
if (cubemap != null)
prop.displayName = cubemap.name;
return prop;

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SampleTexture2DNode.cs


return;
m_TextureType = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SamplerStateNode.cs


return;
m_filter = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

return;
m_wrap = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


if (m_Texture.texture == value)
return;
m_Texture.texture = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
Dirty(ModificationScope.Node);
}
}

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty
properties.Add(new PreviewProperty(PropertyType.Texture)
propType = PropertyType.Texture,
textureValue = texture
});
}

var prop = new TextureShaderProperty();
prop.value = m_Texture;
var prop = new TextureShaderProperty { value = m_Texture };
if (texture != null)
prop.displayName = texture.name;
return prop;

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Advanced/ExponentialNode.cs


return;
m_ExponentialBase = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Advanced/LogNode.cs


return;
m_LogBase = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Advanced/ReciprocalNode.cs


return;
m_ReciprocalMethod = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

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


if (Equals(m_Conversion, value))
return;
m_Conversion = value;
if (onModified != null)
onModified(this, ModificationScope.Graph);
Dirty(ModificationScope.Graph);
}
}

return conversion.from.ToNeededCoordinateSpace();
}
}
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/UV/RotateNode.cs


return;
m_Unit = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
Dirty(ModificationScope.Graph);
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Utility/SubGraphNode.cs


m_SerializedSubGraph = EditorJsonUtility.ToJson(helper, true);
UpdateSlots();
if (onModified != null)
onModified(this, ModificationScope.Topological);
Dirty(ModificationScope.Topological);
}
}
#else

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GenerationResults.cs


public List<PropertyCollector.TextureInfo> configuredTextures;
public PreviewMode previewMode { get; set; }
public FloatShaderProperty outputIdProperty { get; set; }
public Dictionary<Guid, int> ids { get; set; }
ids = new Dictionary<Guid, int>();
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs


shaderProperties,
requirements,
mode,
outputIdProperty: results.outputIdProperty,
ids: results.ids);
outputIdProperty: results.outputIdProperty);
var finalBuilder = new ShaderStringBuilder();
finalBuilder.AppendLine(@"Shader ""{0}""", name);

string functionName = "PopulateSurfaceData",
string surfaceDescriptionName = "SurfaceDescription",
FloatShaderProperty outputIdProperty = null,
Dictionary<Guid, int> ids = null,
IEnumerable<MaterialSlot> slots = null)
{
if (graph == null)

graph.CollectShaderProperties(shaderProperties, mode);
var currentId = -1;
foreach (var activeNode in activeNodeList.OfType<AbstractMaterialNode>())
{
if (activeNode is IGeneratesFunction)

{
var outputSlot = activeNode.GetOutputSlots<MaterialSlot>().FirstOrDefault();
if (outputSlot != null)
{
currentId++;
ids[activeNode.guid] = currentId;
surfaceDescriptionFunction.AddShaderChunk(String.Format("if ({0} == {1}) {{ surface.PreviewOutput = {2}; return surface; }}", outputIdProperty.referenceName, currentId, ShaderGenerator.AdaptNodeOutputForPreview(activeNode, outputSlot.id, activeNode.GetVariableNameForSlot(outputSlot.id))), false);
}
surfaceDescriptionFunction.AddShaderChunk(String.Format("if ({0} == {1}) {{ surface.PreviewOutput = {2}; return surface; }}", outputIdProperty.referenceName, activeNode.tempId.index, ShaderGenerator.AdaptNodeOutputForPreview(activeNode, outputSlot.id, activeNode.GetVariableNameForSlot(outputSlot.id))), false);
}
activeNode.CollectShaderProperties(shaderProperties, mode);

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


VisualElement m_PropertyItems;
VisualElement m_LayerItems;
ObjectField m_PreviewMeshPicker;
AbstractNodeEditorView m_EditorView;
PreviewTextureView m_PreviewTextureView;

Quaternion previewRotation = Quaternion.Euler(m_PreviewScrollPosition.y, 0, 0) * Quaternion.Euler(0, m_PreviewScrollPosition.x, 0);
m_Graph.previewData.rotation = previewRotation;
masterNode.onModified(masterNode, ModificationScope.Node);
masterNode.Dirty(ModificationScope.Node);
}
void OnAddProperty()

{
Mesh changedMesh = changeEvent.newValue as Mesh;
masterNode.onModified(masterNode, ModificationScope.Node);
masterNode.Dirty(ModificationScope.Node);
if (m_Graph.previewData.serializedMesh.mesh != changedMesh)
{

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs


void NotifyNodes()
{
foreach (var node in graph.GetNodes<PropertyNode>())
node.onModified(node, ModificationScope.Node);
node.Dirty(ModificationScope.Node);
}
void FloatField()

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


graphEditorView.HandleGraphChanges();
graphObject.graph.ClearChanges();
}
catch (Exception)
catch (Exception e)
Debug.LogException(e);
throw;
}
}

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


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UnityEngine;

public class PreviewManager : IDisposable
{
AbstractMaterialGraph m_Graph;
Dictionary<Guid, PreviewRenderData> m_RenderDatas = new Dictionary<Guid, PreviewRenderData>();
Dictionary<Guid, PreviewShaderData> m_ShaderDatas = new Dictionary<Guid, PreviewShaderData>();
List<PreviewRenderData> m_RenderDatas = new List<PreviewRenderData>();
HashSet<Guid> m_DirtyPreviews = new HashSet<Guid>();
HashSet<Guid> m_DirtyShaders = new HashSet<Guid>();
HashSet<Guid> m_TimeDependentPreviews = new HashSet<Guid>();
List<Identifier> m_Identifiers = new List<Identifier>();
IndexSet m_DirtyPreviews = new IndexSet();
IndexSet m_DirtyShaders = new IndexSet();
IndexSet m_TimeDependentPreviews = new IndexSet();
string m_UberShaderString;
Dictionary<Guid, int> m_UberShaderIds;
FloatShaderProperty m_OutputIdProperty;
string m_OutputIdName;
public PreviewRenderData masterRenderData
{

m_SceneResources = new PreviewSceneResources();
m_UberShader = ShaderUtil.CreateShaderAsset(k_EmptyShader);
m_UberShader.hideFlags = HideFlags.HideAndDontSave;
m_UberShaderIds = new Dictionary<Guid, int>();
m_MasterRenderData = new PreviewRenderData
{
renderTexture = new RenderTexture(400, 400, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default) { hideFlags = HideFlags.HideAndDontSave }

AddPreview(node);
}
public PreviewRenderData GetPreview(INode node)
public PreviewRenderData GetPreview(AbstractMaterialNode node)
return m_RenderDatas[node.guid];
return m_RenderDatas[node.tempId.index];
PreviewShaderData shaderData;
if (!m_ShaderDatas.TryGetValue(node.guid, out shaderData))
var shaderData = new PreviewShaderData
shaderData = new PreviewShaderData
{
node = node
};
m_ShaderDatas[node.guid] = shaderData;
}
var previewData = new PreviewRenderData
node = node
};
var renderData = new PreviewRenderData
if (m_RenderDatas.ContainsKey(node.guid))
{
Debug.LogWarningFormat("A preview already exists for {0} {1}", node.name, node.guid);
RemovePreview(node);
}
m_RenderDatas.Add(node.guid, previewData);
m_DirtyShaders.Add(node.guid);
Set(m_Identifiers, node.tempId, node.tempId);
Set(m_RenderDatas, node.tempId, renderData);
m_DirtyShaders.Add(node.tempId.index);
m_TimeDependentPreviews.Add(node.guid);
m_TimeDependentPreviews.Add(node.tempId.index);
{
}
}
void RemovePreview(INode node)
{
node.onModified -= OnNodeModified;
m_RenderDatas.Remove(node.guid);
m_TimeDependentPreviews.Remove(node.guid);
m_DirtyPreviews.Remove(node.guid);
m_DirtyShaders.Remove(node.guid);
if (masterRenderData.shaderData != null && masterRenderData.shaderData.node == node)
masterRenderData.shaderData = m_ShaderDatas.Values.FirstOrDefault(x => x.node is IMasterNode);
m_DirtyShaders.Add(node.guid);
m_DirtyShaders.Add(node.tempId.index);
m_DirtyPreviews.Add(node.guid);
m_DirtyPreviews.Add(node.tempId.index);
m_TimeDependentPreviews.Add(node.guid);
m_TimeDependentPreviews.Add(node.tempId.index);
m_TimeDependentPreviews.Remove(node.guid);
m_TimeDependentPreviews.Remove(node.tempId.index);
Stack<Guid> m_Wavefront = new Stack<Guid>();
Stack<Identifier> m_Wavefront = new Stack<Identifier>();
void PropagateNodeSet(HashSet<Guid> nodeGuidSet, bool forward = true, IEnumerable<Guid> initialWavefront = null)
void PropagateNodeSet(IndexSet nodeSet, bool forward = true, IEnumerable<Identifier> initialWavefront = null)
foreach (var guid in initialWavefront ?? nodeGuidSet)
m_Wavefront.Push(guid);
if (initialWavefront != null)
{
foreach (var id in initialWavefront)
m_Wavefront.Push(id);
}
else
{
foreach (var index in nodeSet)
m_Wavefront.Push(m_Identifiers[index]);
}
var nodeGuid = m_Wavefront.Pop();
var node = m_Graph.GetNodeFromGuid(nodeGuid);
var index = m_Wavefront.Pop();
var node = m_Graph.GetNodeFromTempId(index);
if (node == null)
continue;

// We look at each node we feed into.
var connectedSlot = forward ? edge.inputSlot : edge.outputSlot;
var connectedNodeGuid = connectedSlot.nodeGuid;
var connectedNode = m_Graph.GetNodeFromGuid(connectedNodeGuid);
if (nodeGuidSet.Contains(connectedNodeGuid))
if (nodeSet.Contains(connectedNode.tempId.index))
nodeGuidSet.Add(connectedNodeGuid);
m_Wavefront.Push(connectedNodeGuid);
nodeSet.Add(connectedNode.tempId.index);
m_Wavefront.Push(connectedNode.tempId);
HashSet<Guid> m_PropertyNodeGuids = new HashSet<Guid>();
List<PreviewProperty> m_PreviewProperties = new List<PreviewProperty>();
RemovePreview(node);
DestroyPreview(node.tempId);
m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
{
var node = m_Graph.GetNodeFromGuid(edge.inputSlot.nodeGuid);
if (node != null)
m_DirtyShaders.Add(node.tempId.index);
}
m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
{
var node = m_Graph.GetNodeFromGuid(edge.inputSlot.nodeGuid);
if (node != null)
m_DirtyShaders.Add(node.tempId.index);
}
List<PreviewProperty> m_PreviewProperties = new List<PreviewProperty>();
HashSet<Guid> m_NodesWith3DPreview = new HashSet<Guid>();
IndexSet m_PropertyNodes = new IndexSet();
if (m_DirtyShaders.Any())
{
m_NodesWith3DPreview.Clear();
foreach (var node in m_Graph.GetNodes<AbstractMaterialNode>())
{
if (node.previewMode == PreviewMode.Preview3D)
m_NodesWith3DPreview.Add(node.guid);
}
PropagateNodeSet(m_NodesWith3DPreview);
PropagateNodeSet(m_DirtyShaders);
var masterNodes = new List<INode>();
var uberNodes = new List<INode>();
foreach (var guid in m_DirtyShaders)
{
var node = m_Graph.GetNodeFromGuid(guid);
if (node == null)
continue;
if (node is IMasterNode)
masterNodes.Add(node);
else
uberNodes.Add(node);
}
var count = Math.Min(uberNodes.Count, 1) + masterNodes.Count;
UpdateShaders();
try
{
var sw = new Stopwatch();
sw.Start();
var i = 0;
EditorUtility.DisplayProgressBar("Shader Graph", string.Format("Compiling preview shaders ({0}/{1})", i, count), 0f);
foreach (var node in masterNodes)
{
UpdateShader(node.guid);
i++;
EditorUtility.DisplayProgressBar("Shader Graph", string.Format("Compiling preview shaders ({0}/{1})", i, count), 0f);
}
if (uberNodes.Count > 0)
{
m_UberShaderIds.Clear();
var results = m_Graph.GetUberPreviewShader();
m_UberShaderString = results.shader;
m_OutputIdProperty = results.outputIdProperty;
m_UberShaderIds = results.ids;
ShaderUtil.UpdateShaderAsset(m_UberShader, m_UberShaderString);
File.WriteAllText(Application.dataPath + "/../UberShader.shader", (m_UberShaderString ?? "null").Replace("UnityEngine.MaterialGraph", "Generated"));
bool uberShaderHasError = false;
if (MaterialGraphAsset.ShaderHasError(m_UberShader))
{
var errors = MaterialGraphAsset.GetShaderErrors(m_UberShader);
var message = new ShaderStringBuilder();
message.AppendLine(@"Preview shader for graph has {0} error{1}:", errors.Length, errors.Length != 1 ? "s" : "");
foreach (var error in errors)
{
INode node = null;
try
{
node = results.sourceMap.FindNode(error.line);
}
catch (Exception)
{
Debug.LogWarning("ERROR");
continue;
}
message.AppendLine("{0} in {3} at line {1} (on {2})", error.message, error.line, error.platform, node != null ? string.Format("node {0} ({1})", node.name, node.guid) : "graph");
message.AppendLine(error.messageDetails);
message.AppendNewLine();
}
Debug.LogWarning(message.ToString());
ShaderUtil.ClearShaderErrors(m_UberShader);
ShaderUtil.UpdateShaderAsset(m_UberShader, k_EmptyShader);
uberShaderHasError = true;
}
foreach (var node in uberNodes)
{
PreviewShaderData shaderData;
if (!m_ShaderDatas.TryGetValue(node.guid, out shaderData))
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);
}
sw.Stop();
//Debug.LogFormat("Compiled preview shaders in {0} seconds", sw.Elapsed.TotalSeconds);
}
finally
{
EditorUtility.ClearProgressBar();
}
m_DirtyPreviews.UnionWith(m_DirtyShaders);
m_DirtyShaders.Clear();
}
// Union time dependent previews into dirty previews
m_PropertyNodeGuids.Clear();
foreach (var nodeGuid in m_DirtyPreviews)
m_PropertyNodeGuids.Add(nodeGuid);
PropagateNodeSet(m_PropertyNodeGuids, false);
m_PropertyNodes.Clear();
m_PropertyNodes.UnionWith(m_DirtyPreviews);
PropagateNodeSet(m_PropertyNodes, false);
var outputIdName = m_OutputIdProperty != null ? m_OutputIdProperty.referenceName : null;
m_PreviewPropertyBlock.SetFloat(outputIdName, -1);
foreach (var nodeGuid in m_PropertyNodeGuids)
m_PreviewPropertyBlock.SetFloat(m_OutputIdName, -1);
foreach (var index in m_PropertyNodes)
var node = m_Graph.GetNodeFromGuid<AbstractMaterialNode>(nodeGuid);
var node = m_Graph.GetNodeFromTempId(m_Identifiers[index]) as AbstractMaterialNode;
if (node == null)
continue;
node.CollectPreviewMaterialProperties(m_PreviewProperties);

foreach (var previewProperty in m_PreviewProperties)
{
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_PreviewPropertyBlock.SetPreviewProperty(previewProperty);
foreach (var nodeGuid in m_DirtyPreviews)
foreach (var i in m_DirtyPreviews)
PreviewRenderData renderData;
if (!m_RenderDatas.TryGetValue(nodeGuid, out renderData))
continue;
var renderData = m_RenderDatas[i];
if (renderData.shaderData.shader == null)
{
renderData.texture = null;

continue;
}
if (renderData.shaderData.previewMode == PreviewMode.Preview2D)
if (renderData.previewMode == PreviewMode.Preview2D)
if (masterRenderData.shaderData != null && m_DirtyPreviews.Contains(masterRenderData.shaderData.node.guid))
m_RenderList3D.Add(masterRenderData);
m_RenderList3D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));
m_RenderList2D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));

m_SceneResources.camera.orthographic = true;
foreach (var renderData in m_RenderList2D)
{
int outputId;
if (m_UberShaderIds.TryGetValue(renderData.shaderData.node.guid, out outputId))
m_PreviewPropertyBlock.SetFloat(outputIdName, outputId);
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;
GL.Clear(true, true, Color.black);
Graphics.Blit(Texture2D.whiteTexture, renderData.renderTexture, m_SceneResources.checkerboardMaterial);
Graphics.DrawMesh(m_SceneResources.quad, Matrix4x4.identity, m_PreviewMaterial, 1, m_SceneResources.camera, 0, m_PreviewPropertyBlock, ShadowCastingMode.Off, false, null, false);
var previousUseSRP = Unsupported.useScriptableRenderPipeline;
Unsupported.useScriptableRenderPipeline = false;
m_SceneResources.camera.Render();
Unsupported.useScriptableRenderPipeline = previousUseSRP;
RenderTexture.active = previousRenderTexure;
renderData.texture = renderData.renderTexture;
}
RenderPreview(renderData, m_SceneResources.quad, Matrix4x4.identity);
// Render 3D previews
m_SceneResources.camera.transform.position = -Vector3.forward * 5;

foreach (var renderData in m_RenderList3D)
RenderPreview(renderData, m_SceneResources.sphere, Matrix4x4.identity);
var renderMasterPreview = masterRenderData.shaderData != null && m_DirtyPreviews.Contains(masterRenderData.shaderData.node.tempId.index);
if (renderMasterPreview)
int outputId;
if (m_UberShaderIds.TryGetValue(renderData.shaderData.node.guid, out outputId))
m_PreviewPropertyBlock.SetFloat(outputIdName, outputId);
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;
GL.Clear(true, true, Color.black);
Graphics.Blit(Texture2D.whiteTexture, renderData.renderTexture, m_SceneResources.checkerboardMaterial);
var mesh = (renderData == masterRenderData && m_Graph.previewData.serializedMesh.mesh) ? m_Graph.previewData.serializedMesh.mesh : m_SceneResources.sphere;
Quaternion rotation = (renderData == masterRenderData) ? m_Graph.previewData.rotation : Quaternion.identity;
Matrix4x4 previewTransform = Matrix4x4.identity;
if (renderData == masterRenderData)
{
previewTransform *= Matrix4x4.Rotate(rotation);
previewTransform *= Matrix4x4.Scale(Vector3.one * (Vector3.one).magnitude / mesh.bounds.size.magnitude);
previewTransform *= Matrix4x4.Translate(-mesh.bounds.center);
}
Graphics.DrawMesh(mesh, previewTransform, m_PreviewMaterial, 1, m_SceneResources.camera, 0, m_PreviewPropertyBlock, ShadowCastingMode.Off, false, null, false);
var previousUseSRP = Unsupported.useScriptableRenderPipeline;
Unsupported.useScriptableRenderPipeline = renderData.shaderData.node is IMasterNode;
m_SceneResources.camera.Render();
Unsupported.useScriptableRenderPipeline = previousUseSRP;
RenderTexture.active = previousRenderTexure;
renderData.texture = renderData.renderTexture;
var mesh = m_Graph.previewData.serializedMesh.mesh ? m_Graph.previewData.serializedMesh.mesh : m_SceneResources.sphere;
var previewTransform = Matrix4x4.Rotate(m_Graph.previewData.rotation);
previewTransform *= Matrix4x4.Scale(Vector3.one * (Vector3.one).magnitude / mesh.bounds.size.magnitude);
previewTransform *= Matrix4x4.Translate(-mesh.bounds.center);
RenderPreview(masterRenderData, mesh, previewTransform);
}
m_SceneResources.light0.enabled = false;

renderData.NotifyPreviewChanged();
foreach (var renderData in m_RenderList3D)
renderData.NotifyPreviewChanged();
if (renderMasterPreview)
masterRenderData.NotifyPreviewChanged();
m_RenderList2D.Clear();
m_RenderList3D.Clear();

void UpdateShader(Guid nodeGuid)
IndexSet m_NodesWith3DPreview = new IndexSet();
void UpdateShaders()
var node = m_Graph.GetNodeFromGuid<AbstractMaterialNode>(nodeGuid);
if (m_DirtyShaders.Any())
{
PropagateNodeSet(m_DirtyShaders);
m_NodesWith3DPreview.Clear();
foreach (var index in m_DirtyShaders)
{
var node = (AbstractMaterialNode)m_Graph.GetNodeFromTempId(m_Identifiers[index]);
if (node.previewMode == PreviewMode.Preview3D)
m_NodesWith3DPreview.Add(node.tempId.index);
}
PropagateNodeSet(m_NodesWith3DPreview);
foreach (var index in m_DirtyShaders)
{
var renderData = m_RenderDatas[index];
renderData.previewMode = m_NodesWith3DPreview.Contains(renderData.shaderData.node.tempId.index) ? PreviewMode.Preview3D : PreviewMode.Preview2D;
}
var masterNodes = new List<MasterNode>();
var uberNodes = new List<INode>();
foreach (var index in m_DirtyShaders)
{
var node = m_Graph.GetNodeFromTempId(m_Identifiers[index]);
if (node == null)
continue;
var masterNode = node as MasterNode;
if (masterNode != null)
masterNodes.Add(masterNode);
else
uberNodes.Add(node);
}
var count = Math.Min(uberNodes.Count, 1) + masterNodes.Count;
try
{
var i = 0;
EditorUtility.DisplayProgressBar("Shader Graph", string.Format("Compiling preview shaders ({0}/{1})", i, count), 0f);
foreach (var node in masterNodes)
{
UpdateShader(node.tempId);
i++;
EditorUtility.DisplayProgressBar("Shader Graph", string.Format("Compiling preview shaders ({0}/{1})", i, count), 0f);
}
if (uberNodes.Count > 0)
{
var results = m_Graph.GetUberPreviewShader();
m_OutputIdName = results.outputIdProperty.referenceName;
ShaderUtil.UpdateShaderAsset(m_UberShader, results.shader);
File.WriteAllText(Application.dataPath + "/../UberShader.shader", (results.shader ?? "null").Replace("UnityEngine.MaterialGraph", "Generated"));
bool uberShaderHasError = false;
if (MaterialGraphAsset.ShaderHasError(m_UberShader))
{
var errors = MaterialGraphAsset.GetShaderErrors(m_UberShader);
var message = new ShaderStringBuilder();
message.AppendLine(@"Preview shader for graph has {0} error{1}:", errors.Length, errors.Length != 1 ? "s" : "");
foreach (var error in errors)
{
INode node;
try
{
node = results.sourceMap.FindNode(error.line);
}
catch (Exception e)
{
Debug.LogException(e);
continue;
}
message.AppendLine("{0} in {3} at line {1} (on {2})", error.message, error.line, error.platform, node != null ? string.Format("node {0} ({1})", node.name, node.guid) : "graph");
message.AppendLine(error.messageDetails);
message.AppendNewLine();
}
Debug.LogWarning(message.ToString());
ShaderUtil.ClearShaderErrors(m_UberShader);
ShaderUtil.UpdateShaderAsset(m_UberShader, k_EmptyShader);
uberShaderHasError = true;
}
foreach (var node in uberNodes)
{
var renderData = GetRenderData(node.tempId);
if (renderData == null)
continue;
var shaderData = renderData.shaderData;
shaderData.shader = m_UberShader;
shaderData.hasError = uberShaderHasError;
}
i++;
EditorUtility.DisplayProgressBar("Shader Graph", string.Format("Compiling preview shaders ({0}/{1})", i, count), 0f);
}
}
finally
{
EditorUtility.ClearProgressBar();
}
// Union dirty shaders into dirty previews
m_DirtyPreviews.UnionWith(m_DirtyShaders);
m_DirtyShaders.Clear();
}
}
void RenderPreview(PreviewRenderData renderData, Mesh mesh, Matrix4x4 transform)
{
m_PreviewPropertyBlock.SetFloat(m_OutputIdName, renderData.shaderData.node.tempId.index);
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;
GL.Clear(true, true, Color.black);
Graphics.Blit(Texture2D.whiteTexture, renderData.renderTexture, m_SceneResources.checkerboardMaterial);
Graphics.DrawMesh(mesh, transform, m_PreviewMaterial, 1, m_SceneResources.camera, 0, m_PreviewPropertyBlock, ShadowCastingMode.Off, false, null, false);
var previousUseSRP = Unsupported.useScriptableRenderPipeline;
Unsupported.useScriptableRenderPipeline = renderData.shaderData.node is IMasterNode;
m_SceneResources.camera.Render();
Unsupported.useScriptableRenderPipeline = previousUseSRP;
RenderTexture.active = previousRenderTexure;
renderData.texture = renderData.renderTexture;
}
void UpdateShader(Identifier nodeId)
{
var node = m_Graph.GetNodeFromTempId(nodeId) as AbstractMaterialNode;
PreviewShaderData shaderData;
if (!m_ShaderDatas.TryGetValue(nodeGuid, out shaderData))
var renderData = Get(m_RenderDatas, nodeId);
if (renderData == null || renderData.shaderData == null)
shaderData.previewMode = m_NodesWith3DPreview.Contains(nodeGuid) ? PreviewMode.Preview3D : PreviewMode.Preview2D;
var shaderData = renderData.shaderData;
if (!(node is IMasterNode) && (!node.hasPreview || NodeUtils.FindEffectiveShaderStage(node, true) == ShaderStage.Vertex))
{

}
}
void DestroyPreview(Guid nodeGuid, PreviewRenderData previewRenderData)
void DestroyPreview(Identifier nodeId)
if (m_RenderDatas.Remove(nodeGuid))
var renderData = Get(m_RenderDatas, nodeId);
if (renderData != null)
if (previewRenderData.shaderData.shader != null)
Object.DestroyImmediate(previewRenderData.shaderData.shader, true);
if (previewRenderData.renderTexture != null)
Object.DestroyImmediate(previewRenderData.renderTexture, true);
var node = m_Graph.GetNodeFromGuid(nodeGuid);
if (renderData.shaderData.shader != null && renderData.shaderData.shader != m_UberShader)
Object.DestroyImmediate(renderData.shaderData.shader, true);
if (renderData.renderTexture != null)
Object.DestroyImmediate(renderData.renderTexture, true);
var node = renderData.shaderData.node;
m_DirtyPreviews.Remove(nodeGuid);
m_DirtyShaders.Remove(nodeGuid);
m_TimeDependentPreviews.Remove(nodeGuid);
previewRenderData.shaderData.shader = null;
previewRenderData.renderTexture = null;
previewRenderData.texture = null;
previewRenderData.onPreviewChanged = null;
m_TimeDependentPreviews.Remove(nodeId.index);
m_DirtyPreviews.Remove(nodeId.index);
m_DirtyPreviews.Remove(nodeId.index);
Set(m_RenderDatas, nodeId, null);
Set(m_Identifiers, nodeId, default(Identifier));
if (masterRenderData.shaderData != null && masterRenderData.shaderData.node == node)
masterRenderData.shaderData = m_RenderDatas.Where(x => x != null && x.shaderData.node is IMasterNode).Select(x => x.shaderData).FirstOrDefault();
renderData.shaderData.shader = null;
renderData.renderTexture = null;
renderData.texture = null;
renderData.onPreviewChanged = null;
if (m_UberShader != null)
{
Object.DestroyImmediate(m_UberShader, true);
m_UberShader = null;
}
if (m_ErrorTexture != null)
{
Object.DestroyImmediate(m_ErrorTexture);
m_ErrorTexture = null;
}
{
m_PreviewMaterial = null;
m_PreviewMaterial = null;
}
{
m_SceneResources = null;
var previews = m_RenderDatas.ToList();
foreach (var kvp in previews)
DestroyPreview(kvp.Key, kvp.Value);
m_SceneResources = null;
}
foreach (var renderData in m_RenderDatas.ToList())
DestroyPreview(renderData.shaderData.node.tempId);
}
public void Dispose()

}
}
}";
T Get<T>(List<T> list, Identifier id)
{
var existingId = Get(m_Identifiers, id.index);
if (existingId.valid && existingId.version != id.version)
throw new Exception("Identifier version mismatch");
return Get(list, id.index);
}
static T Get<T>(List<T> list, int index)
{
return index < list.Count ? list[index] : default(T);
}
void Set<T>(List<T> list, Identifier id, T value)
{
var existingId = Get(m_Identifiers, id.index);
if (existingId.valid && existingId.version != id.version)
throw new Exception("Identifier version mismatch");
Set(list, id.index, value);
}
static void Set<T>(List<T> list, int index, T value)
{
// Make sure the list is large enough for the index
for (var i = list.Count; i <= index; i++)
list.Add(default(T));
list[index] = value;
}
PreviewRenderData GetRenderData(Identifier id)
{
var value = Get(m_RenderDatas, id);
if (value != null && value.shaderData.node.tempId.version != id.version)
throw new Exception("Trying to access render data of a previous version of a node");
return value;
}
}
public delegate void OnPreviewChanged();

public INode node { get; set; }
public Shader shader { get; set; }
public string shaderString { get; set; }
public PreviewMode previewMode { get; set; }
public bool hasError { get; set; }
}

public RenderTexture renderTexture { get; set; }
public Texture texture { get; set; }
public PreviewMode previewMode { get; set; }
public OnPreviewChanged onPreviewChanged;
public void NotifyPreviewChanged()

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/ColorRGBSlotControlView.cs


{
m_Slot.owner.owner.owner.RegisterCompleteObjectUndo("Color Change");
m_Slot.value = new Vector3(evt.newValue.r, evt.newValue.g, evt.newValue.b);
if (m_Slot.owner.onModified != null)
m_Slot.owner.onModified(m_Slot.owner, ModificationScope.Node);
m_Slot.owner.Dirty(ModificationScope.Node);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/ColorSlotControlView.cs


{
m_Slot.owner.owner.owner.RegisterCompleteObjectUndo("Color Change");
m_Slot.value = evt.newValue;
if (m_Slot.owner.onModified != null)
m_Slot.owner.onModified(m_Slot.owner, ModificationScope.Node);
m_Slot.owner.Dirty(ModificationScope.Node);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/CubemapSlotControlView.cs


{
m_Slot.owner.owner.owner.RegisterCompleteObjectUndo("Change Cubemap");
m_Slot.cubemap = cubemap;
if (m_Slot.owner.onModified != null)
m_Slot.owner.onModified(m_Slot.owner, ModificationScope.Node);
m_Slot.owner.Dirty(ModificationScope.Node);
}
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/MultiFloatSlotControlView.cs


var value = m_Get();
value[index] = (float)evt.newValue;
m_Set(value);
if (m_Node.onModified != null)
m_Node.onModified(m_Node, ModificationScope.Node);
m_Node.Dirty(ModificationScope.Node);
m_UndoGroup = -1;
});
field.RegisterCallback<InputEvent>(evt =>

{
value[index] = newValue;
m_Set(value);
if (m_Node.onModified != null)
m_Node.onModified(m_Node, ModificationScope.Node);
m_Node.Dirty(ModificationScope.Node);
}
});
field.RegisterCallback<KeyDownEvent>(evt =>

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/TextureSlotControlView.cs


{
m_Slot.owner.owner.owner.RegisterCompleteObjectUndo("Change Texture");
m_Slot.texture = texture;
if (m_Slot.owner.onModified != null)
m_Slot.owner.onModified(m_Slot.owner, ModificationScope.Node);
m_Slot.owner.Dirty(ModificationScope.Node);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/UVSlotControlView.cs


{
m_Slot.owner.owner.owner.RegisterCompleteObjectUndo("Change UV Channel");
m_Slot.channel = channel;
if (m_Slot.owner.onModified != null)
m_Slot.owner.onModified(m_Slot.owner, ModificationScope.Graph);
m_Slot.owner.Dirty(ModificationScope.Graph);
}
}
}

2
MaterialGraphProject/ProjectSettings/ProjectVersion.txt


m_EditorVersion: 2018.1.0b1
m_EditorVersion: 2018.1.0b2

37
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/Identifier.cs


namespace UnityEditor.ShaderGraph
{
public struct Identifier
{
uint m_Version;
int m_Index;
public Identifier(int index, uint version = 1)
{
m_Version = version;
m_Index = index;
}
public void IncrementVersion()
{
if (m_Version == uint.MaxValue)
m_Version = 1;
else
m_Version++;
}
public uint version
{
get { return m_Version; }
}
public int index
{
get { return m_Index; }
}
public bool valid
{
get { return m_Version != 0; }
}
}
}

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


fileFormatVersion: 2
guid: 084958a703164b0fa26e0667aaa1767f
timeCreated: 1513937833

357
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/IndexSet.cs


using System;
using System.Collections;
using System.Collections.Generic;
namespace UnityEditor.ShaderGraph.Drawing
{
public sealed class IndexSet : ICollection<int>
{
List<uint> m_Masks = new List<uint>();
public IndexSet() { }
public IndexSet(IEnumerable<int> indices)
{
foreach (var index in indices)
Add(index);
}
public IEnumerator<int> GetEnumerator()
{
for (var i = 0; i < m_Masks.Count; i++)
{
var mask = m_Masks[i];
if (mask == 0)
continue;
for (var j = 0; j < 32; j++)
{
if ((mask & (1 << j)) > 0)
yield return i * 32 + j;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void UnionWith(IEnumerable<int> other)
{
var otherSet = other as IndexSet;
if (otherSet != null)
{
UnionWith(otherSet);
}
else
{
foreach (var index in other)
Add(index);
}
}
public void UnionWith(IndexSet other)
{
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
m_Masks[i] |= other.m_Masks[i];
for (var i = m_Masks.Count; i < other.m_Masks.Count; i++)
m_Masks.Add(other.m_Masks[i]);
}
public void IntersectWith(IEnumerable<int> other)
{
IntersectWith(other as IndexSet ?? new IndexSet(other));
}
public void IntersectWith(IndexSet other)
{
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
m_Masks[i] &= other.m_Masks[i];
}
public void ExceptWith(IEnumerable<int> other)
{
var otherSet = other as IndexSet;
if (otherSet != null)
{
ExceptWith(otherSet);
}
else
{
foreach (var index in other)
Remove(index);
}
}
public void ExceptWith(IndexSet other)
{
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
m_Masks[i] &= ~other.m_Masks[i];
}
public void SymmetricExceptWith(IEnumerable<int> other)
{
SymmetricExceptWith(other as IndexSet ?? new IndexSet(other));
}
public void SymmetricExceptWith(IndexSet other)
{
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
m_Masks[i] ^= other.m_Masks[i];
}
public bool IsSubsetOf(IEnumerable<int> other)
{
return IsSubsetOf(other as IndexSet ?? new IndexSet(other));
}
public bool IsSubsetOf(IndexSet other)
{
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
{
var mask = m_Masks[i];
var otherMask = other.m_Masks[i];
if ((mask & otherMask) != mask)
return false;
}
for (var i = other.m_Masks.Count; i < m_Masks.Count; i++)
{
if (m_Masks[i] > 0)
return false;
}
return true;
}
public bool IsSupersetOf(IEnumerable<int> other)
{
return IsSupersetOf(other as IndexSet ?? new IndexSet(other));
}
public bool IsSupersetOf(IndexSet other)
{
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
{
var otherMask = other.m_Masks[i];
var mask = m_Masks[i];
if ((otherMask & mask) != otherMask)
return false;
}
for (var i = m_Masks.Count; i < other.m_Masks.Count; i++)
{
if (other.m_Masks[i] > 0)
return false;
}
return true;
}
public bool IsProperSupersetOf(IEnumerable<int> other)
{
return IsProperSupersetOf(other as IndexSet ?? new IndexSet(other));
}
public bool IsProperSupersetOf(IndexSet other)
{
var isProper = false;
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
{
var mask = m_Masks[i];
var otherMask = other.m_Masks[i];
if ((otherMask & mask) != otherMask)
return false;
if ((~otherMask & mask) > 0)
isProper = true;
}
for (var i = m_Masks.Count; i < other.m_Masks.Count; i++)
{
if (other.m_Masks[i] > 0)
return false;
}
if (!isProper)
{
for (var i = other.m_Masks.Count; i < m_Masks.Count; i++)
{
if (m_Masks[i] > 0)
return true;
}
}
return isProper;
}
public bool IsProperSubsetOf(IEnumerable<int> other)
{
return IsProperSubsetOf(other as IndexSet ?? new IndexSet(other));
}
public bool IsProperSubsetOf(IndexSet other)
{
var isProper = false;
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
{
var mask = m_Masks[i];
var otherMask = other.m_Masks[i];
if ((mask & otherMask) != mask)
return false;
if ((~mask & otherMask) > 0)
isProper = true;
}
for (var i = other.m_Masks.Count; i < m_Masks.Count; i++)
{
if (m_Masks[i] > 0)
return false;
}
if (!isProper)
{
for (var i = m_Masks.Count; i < other.m_Masks.Count; i++)
{
if (other.m_Masks[i] > 0)
return true;
}
}
return isProper;
}
public bool Overlaps(IEnumerable<int> other)
{
var otherSet = other as IndexSet;
if (otherSet != null)
return Overlaps(otherSet);
foreach (var index in other)
{
if (Contains(index))
return true;
}
return false;
}
public bool Overlaps(IndexSet other)
{
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
{
if ((m_Masks[i] & other.m_Masks[i]) > 0)
return true;
}
return false;
}
public bool SetEquals(IEnumerable<int> other)
{
var otherSet = other as IndexSet;
if (otherSet != null)
return SetEquals(otherSet);
foreach (var index in other)
{
if (!Contains(index))
return false;
}
return true;
}
public bool SetEquals(IndexSet other)
{
for (var i = 0; i < Math.Min(m_Masks.Count, other.m_Masks.Count); i++)
{
if (m_Masks[i] != other.m_Masks[i])
return false;
}
for (var i = other.m_Masks.Count; i < m_Masks.Count; i++)
{
if (m_Masks[i] > 0)
return false;
}
for (var i = m_Masks.Count; i < other.m_Masks.Count; i++)
{
if (other.m_Masks[i] > 0)
return false;
}
return true;
}
public bool Add(int index)
{
var maskIndex = index >> 5;
var bitIndex = index & 31;
for (var i = m_Masks.Count; i <= maskIndex; i++)
m_Masks.Add(0);
var mask = (uint)1 << bitIndex;
var isNew = (m_Masks[maskIndex] & mask) == 0;
m_Masks[maskIndex] |= mask;
return isNew;
}
void ICollection<int>.Add(int index)
{
Add(index);
}
public void Clear()
{
for (var i = 0; i < m_Masks.Count; i++)
m_Masks[i] = 0;
}
public bool Contains(int index)
{
var maskIndex = index >> 5;
var bitIndex = index & 31;
return (m_Masks[maskIndex] & ((uint)1 << bitIndex)) > 0;
}
public void CopyTo(int[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(int index)
{
var maskIndex = index >> 5;
var bitIndex = index & 31;
if (maskIndex >= m_Masks.Count)
return false;
var mask = (uint)1 << bitIndex;
var exists = (m_Masks[maskIndex] & mask) > 0;
m_Masks[maskIndex] &= ~mask;
return exists;
}
public int Count
{
get
{
var count = 0;
foreach (var mask in m_Masks)
{
for (var j = 0; j < 32; j++)
{
if ((mask & (1 << j)) > 0)
count++;
}
}
return count;
}
}
public bool IsReadOnly
{
get { return false; }
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/IndexSet.cs.meta


fileFormatVersion: 2
guid: 41c67c52dd54464fa78d8ab286eb501b
timeCreated: 1514376310

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/AbstractNodeEditorView.cs.meta


fileFormatVersion: 2
guid: fbedfe40df1243cabce79ccfd4906096
timeCreated: 1504778276

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/NodeEditorHeaderView.cs.meta


fileFormatVersion: 2
guid: 157f3e805d124f169cf20967d94036b3
timeCreated: 1504857315

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/StandardNodeEditorView.cs.meta


fileFormatVersion: 2
guid: 61b4edcdcc1949a29fa0b42e25241429
timeCreated: 1503661486

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/SurfaceMasterNodeEditorView.cs.meta


fileFormatVersion: 2
guid: 6f830a3e1e834df09773f7ccaaac94d3
timeCreated: 1504787084

14
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/AbstractNodeEditorView.cs


using System;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{
public abstract class AbstractNodeEditorView : VisualElement, IDisposable
{
public abstract INode node { get; set; }
public abstract void Dispose();
}
}

32
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/NodeEditorHeaderView.cs


using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{
public class NodeEditorHeaderView : VisualElement
{
Label m_Title;
Label m_Type;
public NodeEditorHeaderView()
{
m_Title = new Label("") { name = "title" };
Add(m_Title);
Add(new Label("(") { name = "preType" });
m_Type = new Label("") { name = "type" };
Add(m_Type);
Add(new Label(")") { name = "postType" });
}
public string title
{
get { return m_Title.text; }
set { m_Title.text = value; }
}
public string type
{
get { return m_Type.text; }
set { m_Type.text = value; }
}
}
}

48
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/StandardNodeEditorView.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{
public class StandardNodeEditorView : AbstractNodeEditorView
{
NodeEditorHeaderView m_HeaderView;
AbstractMaterialNode m_Node;
public override INode node
{
get { return m_Node; }
set
{
if (value == m_Node)
return;
if (m_Node != null)
m_Node.onModified -= OnModified;
m_Node = value as AbstractMaterialNode;
OnModified(m_Node, ModificationScope.Node);
if (m_Node != null)
m_Node.onModified += OnModified;
}
}
public override void Dispose()
{
if (m_Node != null)
m_Node.onModified -= OnModified;
}
public StandardNodeEditorView()
{
AddToClassList("nodeEditor");
m_HeaderView = new NodeEditorHeaderView() { type = "node" };
Add(m_HeaderView);
}
void OnModified(INode changedNode, ModificationScope scope)
{
if (node == null)
return;
m_HeaderView.title = node.name;
}
}
}

78
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/SurfaceMasterNodeEditorView.cs


using System;
using UnityEngine.Experimental.UIElements;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph;
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{
/* public class SurfaceMasterNodeEditorView : AbstractNodeEditorView
{
NodeEditorHeaderView m_HeaderView;
AbstractSurfaceMasterNode m_Node;
public override INode node
{
get { return m_Node; }
set
{
if (value == m_Node)
return;
if (m_Node != null)
m_Node.onModified -= OnModified;
m_Node = value as AbstractSurfaceMasterNode;
OnModified(m_Node, ModificationScope.Node);
if (m_Node != null)
m_Node.onModified += OnModified;
}
}
public override void Dispose()
{
if (m_Node != null)
m_Node.onModified -= OnModified;
}
public SurfaceMasterNodeEditorView()
{
AddToClassList("nodeEditor");
m_HeaderView = new NodeEditorHeaderView { type = "node" };
Add(m_HeaderView);
var optionsSection = new VisualElement() { name = "surfaceOptions" };
optionsSection.AddToClassList("section");
{
optionsSection.Add(new IMGUIContainer(OnGUIHandler));
}
Add(optionsSection);
}
void OnGUIHandler()
{
if (m_Node == null)
return;
var options = m_Node.options;
EditorGUI.BeginChangeCheck();
options.srcBlend = (SurfaceMaterialOptions.BlendMode)EditorGUILayout.EnumPopup("Src Blend", options.srcBlend);
options.dstBlend = (SurfaceMaterialOptions.BlendMode)EditorGUILayout.EnumPopup("Dst Blend", options.dstBlend);
options.cullMode = (SurfaceMaterialOptions.CullMode)EditorGUILayout.EnumPopup("Cull Mode", options.cullMode);
options.zTest = (SurfaceMaterialOptions.ZTest)EditorGUILayout.EnumPopup("Z Test", options.zTest);
options.zWrite = (SurfaceMaterialOptions.ZWrite)EditorGUILayout.EnumPopup("Z Write", options.zWrite);
options.renderQueue = (SurfaceMaterialOptions.RenderQueue)EditorGUILayout.EnumPopup("Render Queue", options.renderQueue);
options.renderType = (SurfaceMaterialOptions.RenderType)EditorGUILayout.EnumPopup("Render Type", options.renderType);
if (EditorGUI.EndChangeCheck())
m_Node.onModified(m_Node, ModificationScope.Graph);
}
void OnModified(INode changedNode, ModificationScope scope)
{
if (m_Node == null)
return;
m_HeaderView.title = m_Node.name;
Dirty(ChangeType.Repaint);
}
}*/
}
正在加载...
取消
保存