浏览代码

Merge branch 'master' into node-artistic-adjustment

/main
Matt Dean 7 年前
当前提交
1b3ac398
共有 61 个文件被更改,包括 1720 次插入1349 次删除
  1. 1
      .gitignore
  2. 46
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/NodeUtils.cs
  3. 23
      MaterialGraphProject/Assets/NewNodes/Editor/Keep/HeightToNormalNode.cs
  4. 224
      MaterialGraphProject/Assets/PartyPreview.ShaderGraph
  5. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderGraph.cs
  6. 41
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  7. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs
  8. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialGraph.cs
  9. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/IMasterNode.cs
  10. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/MasterNode.cs
  11. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/LayeredShaderGraph.cs
  12. 48
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SurfaceModel/SurfaceMaterialOptions.cs
  13. 20
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/EnumControl.cs
  14. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/VectorControl.cs
  15. 46
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  16. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/StandardNodeEditorView.cs
  17. 35
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  18. 193
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs
  19. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  20. 62
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs
  21. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PortInputView.cs
  22. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/MultiFloatSlotControlView.cs
  23. 126
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss
  24. 108
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightUnlitPass.template
  25. 2
      MaterialGraphProject/ProjectSettings/ProjectVersion.txt
  26. 281
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs
  27. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs.meta
  28. 208
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs
  29. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs.meta
  30. 163
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/PBRMasterNode.cs
  31. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/PBRMasterNode.cs.meta
  32. 79
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/UnlitMasterNode.cs
  33. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/UnlitMasterNode.cs.meta
  34. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask.meta
  35. 79
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ChannelEnumControl.cs
  36. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ChannelEnumControl.cs.meta
  37. 40
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRExtraPasses.template
  38. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRExtraPasses.template.meta
  39. 145
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRForwardPass.template
  40. 37
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ColorMaskNode.cs
  41. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ColorMaskNode.cs.meta
  42. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ChannelMaskNode.cs.meta
  43. 124
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ChannelMaskNode.cs
  44. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/AbstractLightweightPBRMasterNode.cs.meta
  45. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightMetallicMasterNode.cs.meta
  46. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightSpecularMasterNode.cs.meta
  47. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightUnlitMasterNode.cs.meta
  48. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/AbstractLightweightMasterNode.cs.meta
  49. 57
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/AbstractLightweightPBRMasterNode.cs
  50. 84
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightMetallicMasterNode.cs
  51. 79
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightSpecularMasterNode.cs
  52. 71
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightUnlitMasterNode.cs
  53. 126
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/AbstractLightweightMasterNode.cs
  54. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/MasterRemapNode.cs.meta
  55. 101
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/MasterRemapNode.cs
  56. 180
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template
  57. 20
      MaterialGraphProject/ProjectSettings/EditorSettings.asset
  58. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightUnlitPass.template.meta
  59. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightUnlitPass.template
  60. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRForwardPass.template.meta

1
.gitignore


MaterialGraphProject/Assets/_MingWai/New Custom Texture.asset.meta
MaterialGraphProject/Packages/*
MaterialGraphProject/GeneratedShader.shader
MaterialGraphProject/UberShader.shader
!MaterialGraphProject/Packages/manifest.json

46
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/NodeUtils.cs


if (node == null)
return;
using (var stackDisposable = StackPool<INode>.GetDisposable())
using (var queueDisposable = QueuePool<INode>.GetDisposable())
{
var stack = stackDisposable.value;
var queue = queueDisposable.value;
queue.Enqueue(node);
// already added this node
if (nodeList.Contains(node))
return;
while (queue.Any())
{
var fromNode = queue.Dequeue();
// already added this node
if (nodeList.Contains(fromNode))
continue;
foreach (var slot in fromNode.GetInputSlots<ISlot>())
{
if (slotIds != null && !slotIds.Contains(slot.id))
continue;
var ids = node.GetInputSlots<ISlot>().Select(x => x.id);
if (slotIds != null)
ids = node.GetInputSlots<ISlot>().Where(x => slotIds.Contains(x.id)).Select(x => x.id);
foreach (var edge in fromNode.owner.GetEdges(fromNode.GetSlotReference(slot.id)))
{
var outputNode = fromNode.owner.GetNodeFromGuid(edge.outputSlot.nodeGuid);
queue.Enqueue(outputNode);
stack.Push(outputNode);
}
}
foreach (var slot in ids)
{
foreach (var edge in node.owner.GetEdges(node.GetSlotReference(slot)))
{
var outputNode = node.owner.GetNodeFromGuid(edge.outputSlot.nodeGuid);
DepthFirstCollectNodesFromNode(nodeList, outputNode);
}
while (stack.Any())
nodeList.Add(stack.Pop());
if (includeSelf == IncludeSelf.Include)
nodeList.Add(node);
}
if (includeSelf == IncludeSelf.Include)
nodeList.Add(node);
}
public static void CollectNodesNodeFeedsInto(List<INode> nodeList, INode node, IncludeSelf includeSelf = IncludeSelf.Include)

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


namespace UnityEditor.ShaderGraph
{
[Title("Utility/Heightmap To Normalmap")]
public class HeightToNormalNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireMeshUV
public class HeightToNormalNode : AbstractMaterialNode, IGeneratesBodyCode
{
public const int TextureInput = 0;
public const int TexCoordInput = 1;

public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Texture2DMaterialSlot(TextureInput, TextureInputName, TextureInputName, SlotType.Input));
AddSlot(new Vector2MaterialSlot(TexCoordInput, TexCoordInputName, TexCoordInputName, SlotType.Input, Vector2.zero));
AddSlot(new UVMaterialSlot(TexCoordInput, TexCoordInputName, TexCoordInputName, UVChannel.uv0));
AddSlot(new Vector1MaterialSlot(TexOffsetInput, TexOffsetInputName, TexOffsetInputName, SlotType.Input, 0.005f));
AddSlot(new Vector1MaterialSlot(StrengthInput, StrengthInputName, StrengthInputName, SlotType.Input, 8f));
AddSlot(new Vector3MaterialSlot(NormalOutput, NormalOutputName, NormalOutputName, SlotType.Output, Vector3.zero));

{
var textureInput = GetSlotValue(TextureInput, generationMode);
var texCoordInput = RequiresMeshUV(UVChannel.uv0)
? string.Format("{0}.xy", UVChannel.uv0.GetUVName())
: GetSlotValue(TexCoordInput, generationMode);
var texCoordInput = GetSlotValue(TexCoordInput, generationMode);
var texOffsetInput = GetSlotValue(TexOffsetInput, generationMode);
var strengthInput = GetSlotValue(StrengthInput, generationMode);
var normalOutput = GetVariableNameForSlot(NormalOutput);

public override bool hasPreview
{
get { return true; }
}
public bool RequiresMeshUV(UVChannel channel)
{
if (channel != UVChannel.uv0)
{
return false;
}
var uvSlot = FindInputSlot<MaterialSlot>(TexCoordInput);
if (uvSlot == null)
return true;
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
return edges.Count == 0;
}
}
}

224
MaterialGraphProject/Assets/PartyPreview.ShaderGraph
文件差异内容过多而无法显示
查看文件

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderGraph.cs


public override void Action(int instanceId, string pathName, string resourceFile)
{
var graph = new ShaderGraph.MaterialGraph();
graph.AddNode(new LightweightMetallicMasterNode());
//graph.AddNode(new LightweightMetallicMasterNode());
File.WriteAllText(pathName, EditorJsonUtility.ToJson(graph));
AssetDatabase.Refresh();
}

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


base.OnAfterDeserialize();
}
protected static ShaderGraphRequirements GetRequirements(List<INode> nodes)
internal static ShaderGraphRequirements GetRequirements(List<INode> nodes)
{
NeededCoordinateSpace requiresNormal = nodes.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal());
NeededCoordinateSpace requiresBitangent = nodes.OfType<IMayRequireBitangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresBitangent());

FloatShaderProperty outputIdProperty;
return GetShader(node, GenerationMode.Preview, string.Format("hidden/preview/{0}", node.GetVariableNameForNode()), out configuredTextures, out previewMode, out outputIdProperty);
}
public string GetUberPreviewShader(Dictionary<Guid, int> ids, out FloatShaderProperty outputIdProperty)
{
List<PropertyCollector.TextureInfo> configuredTextures;

protected static void GenerateSurfaceDescriptionStruct(ShaderGenerator surfaceDescriptionStruct, List<MaterialSlot> slots, bool isMaster)
internal static void GenerateSurfaceDescriptionStruct(ShaderGenerator surfaceDescriptionStruct, List<MaterialSlot> slots, bool isMaster)
{
surfaceDescriptionStruct.AddShaderChunk("struct SurfaceDescription{", false);
surfaceDescriptionStruct.Indent();

surfaceDescriptionStruct.AddShaderChunk(string.Format("{0} {1};", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType), AbstractMaterialNode.GetHLSLSafeName(slot.shaderOutputName)), false);
surfaceDescriptionStruct.Deindent();
surfaceDescriptionStruct.AddShaderChunk("};", false);
surfaceDescriptionStruct.AddShaderChunk("void ScaleSurfaceDescription(inout SurfaceDescription surface, float scale){", false);
surfaceDescriptionStruct.Indent();
foreach (var slot in slots)
surfaceDescriptionStruct.AddShaderChunk(string.Format("surface.{0} = scale * surface.{0};", AbstractMaterialNode.GetHLSLSafeName(slot.shaderOutputName)), false);
surfaceDescriptionStruct.Deindent();
surfaceDescriptionStruct.AddShaderChunk("};", false);
surfaceDescriptionStruct.AddShaderChunk("void AddSurfaceDescription(inout SurfaceDescription base, in SurfaceDescription add){", false);
surfaceDescriptionStruct.Indent();
foreach (var slot in slots)
surfaceDescriptionStruct.AddShaderChunk(string.Format("base.{0} = base.{0} + add.{0};", AbstractMaterialNode.GetHLSLSafeName(slot.shaderOutputName)), false);
}
else
{

surfaceDescriptionStruct.AddShaderChunk("};", false);
}
protected static void GenerateSurfaceDescription(
internal static void GenerateSurfaceDescription(
List<INode> activeNodeList,
AbstractMaterialNode masterNode,
AbstractMaterialGraph graph,

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

{
if (masterNode is IMasterNode)
{
foreach (var input in masterNode.GetInputSlots<MaterialSlot>())
var usedSlots = slots ?? masterNode.GetInputSlots<MaterialSlot>();
foreach (var input in usedSlots)
{
var foundEdges = graph.GetEdges(input.slotReference).ToArray();
if (foundEdges.Any())

};
if (isUber)
shaderProperties.AddShaderProperty(outputIdProperty);
GenerateSurfaceDescription(
activeNodeList,
node,

finalShader.AddShaderChunk(surfaceDescriptionFunction.GetShaderString(2), false);
finalShader.AddShaderChunk("ENDCG", false);
var masterNode = node as IMasterNode;
if (node != null && masterNode != null)
{
var subShaders = masterNode.GetSubshader(requirements, null);
foreach (var ss in subShaders)
finalShader.AddShaderChunk(ss, false);
}
else
{
finalShader.AddShaderChunk(ShaderGenerator.GetPreviewSubShader(node, requirements), false);
}
finalShader.AddShaderChunk(ShaderGenerator.GetPreviewSubShader(node, requirements), false);
ListPool<INode>.Release(activeNodeList);
finalShader.Deindent();

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


protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
var channelCount = (int)SlotValueHelper.GetChannelCount(concreteValueType);
var values = value.x.ToString();
if (channelCount == 1)
return values;
for (var i = 1; i < channelCount; i++)
values += ", " + value[i];
return string.Format("{0}{1}({2})", precision, channelCount, values);
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)

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


public string GetShader(string name, GenerationMode mode, out List<PropertyCollector.TextureInfo> configuredTextures)
{
PreviewMode pmode;
FloatShaderProperty outputIdProperty;
return base.GetShader(masterNode as AbstractMaterialNode, mode, name, out configuredTextures, out pmode, out outputIdProperty);
return masterNode.GetShader(mode, name, out configuredTextures);
}
public void LoadedFromDisk()

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/IMasterNode.cs


public interface IMasterNode
{
SurfaceMaterialOptions options { get; }
IEnumerable<string> GetSubshader(ShaderGraphRequirements graphRequirements, MasterRemapGraph remapper);
string GetShader(GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures);
}
}

13
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/MasterNode.cs


[SerializeField]
protected SurfaceMaterialOptions m_MaterialOptions = new SurfaceMaterialOptions();
public MasterNode()
{
name = "MasterNode";
UpdateNodeAfterDeserialization();
}
public override bool hasPreview
{
get { return true; }

get { return PreviewMode.Preview3D; }
}
public virtual ShaderGraphRequirements GetNodeSpecificRequirements()
{
return ShaderGraphRequirements.none;
}
public SurfaceMaterialOptions options
{
get { return m_MaterialOptions; }

public abstract IEnumerable<string> GetSubshader(ShaderGraphRequirements graphRequirements, MasterRemapGraph remapper);
public abstract string GetShader(GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures);
}
}

13
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/LayeredShaderGraph.cs


var masterNode = baseGraph.masterNode;
GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, ((AbstractMaterialNode) masterNode).GetInputSlots<MaterialSlot>().ToList(), true);
foreach (var layer in layerMap)
/* foreach (var layer in layerMap)
{
activeNodes.Clear();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, layer.Value.masterNode as AbstractMaterialNode);

requirements,
mode,
LayerToFunctionName(layer.Key));
}
}*/
surfaceDescriptionStruct.AddShaderChunk("struct WeightsSurfaceDescription{", false);
surfaceDescriptionStruct.Indent();

activeNodes.Clear();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, outputNode);
GenerateSurfaceDescription(
/*GenerateSurfaceDescription(
activeNodes,
outputNode,
this,

mode,
"PopulateWeightsGraph",
"WeightsSurfaceDescription");
*/
string functionName = "PopulateSurfaceData";
string surfaceDescriptionName = "SurfaceDescription";

if (masterNode != null)
{
var subShaders = masterNode.GetSubshader(requirements, null);
foreach (var ss in subShaders)
finalShader.AddShaderChunk(ss, false);
//var subShaders = masterNode.GetSubshader(requirements, null);
// foreach (var ss in subShaders)
// finalShader.AddShaderChunk(ss, false);
}
finalShader.Deindent();

48
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SurfaceModel/SurfaceMaterialOptions.cs


private RenderType m_RenderType = RenderType.Opaque;
[SerializeField]
private bool m_ShadowPass;
[SerializeField]
private bool m_FullForwardShadows;
[SerializeField]
private bool m_NoAmbient;
[SerializeField]
private bool m_NoVertexLights;
[SerializeField]
private bool m_NoLightmaps;
[SerializeField]
private bool m_NoDirLightmap;
[SerializeField]
private bool m_NoForwardAdd;
[SerializeField]
private bool m_ApproxView;
[SerializeField]
private bool m_HalfAsView;
[SerializeField]
[SerializeField]
private bool m_Expanded;
public void Init()
{

zWrite = ZWrite.On;
renderQueue = RenderQueue.Geometry;
renderType = RenderType.Opaque;
shadowPass = false;
fullForwardShadows = false;
noAmbient = false;
noVertexLights = false;
noLightmaps = false;
noDirLightmap = false;
noForwardAdd = false;
approxView = false;
halfAsView = false;
lod = 200;
}

public ZWrite zWrite { get { return m_ZWrite; } set { m_ZWrite = value; } }
public RenderQueue renderQueue { get { return m_RenderQueue; } set { m_RenderQueue = value; } }
public RenderType renderType { get { return m_RenderType; } set { m_RenderType = value; } }
public bool shadowPass { get { return m_ShadowPass; } set { m_ShadowPass = value; } }
public bool fullForwardShadows { get { return m_FullForwardShadows; } set { m_FullForwardShadows = value; } }
public bool noAmbient { get { return m_NoAmbient; } set { m_NoAmbient = value; } }
public bool noVertexLights { get { return m_NoVertexLights; } set { m_NoVertexLights = value; } }
public bool noLightmaps { get { return m_NoLightmaps; } set { m_NoLightmaps = value; } }
public bool noDirLightmap { get { return m_NoDirLightmap; } set { m_NoDirLightmap = value; } }
public bool noForwardAdd { get { return m_NoForwardAdd; } set { m_NoForwardAdd = value; } }
public bool approxView { get { return m_ApproxView; } set { m_ApproxView = value; } }
public bool halfAsView { get { return m_HalfAsView; } set { m_HalfAsView = value; } }
public int lod { get { return m_LOD; } set { m_LOD = value; } }
}
}

20
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/EnumControl.cs


using System;
using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEngine;
using UnityEngine.Experimental.UIElements;

public class EnumControlView : VisualElement
{
GUIContent m_Label;
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;

m_PropertyInfo = propertyInfo;
if (!propertyInfo.PropertyType.IsEnum)
throw new ArgumentException("Property must be an enum.", "propertyInfo");
m_Label = new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
Add(new IMGUIContainer(OnGUIHandler));
Add(new Label(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name)));
var enumField = new EnumField((Enum) m_PropertyInfo.GetValue(m_Node, null));
enumField.OnValueChanged(OnValueChanged);
Add(enumField);
void OnGUIHandler()
void OnValueChanged(ChangeEvent<Enum> evt)
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
if (!evt.newValue.Equals(value))
value = EditorGUILayout.EnumPopup(m_Label, value);
if (changeCheckScope.changed)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_PropertyInfo.SetValue(m_Node, value, null);
}
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
}
}
}

7
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/VectorControl.cs


void AddField(int index, string subLabel)
{
Add(new Label(subLabel));
var label = new Label(subLabel);
Add(label);
var dragger = new FieldMouseDragger<double>(doubleField);
dragger.SetDragZone(label);
doubleField.RegisterCallback<MouseDownEvent>(Repaint);
doubleField.RegisterCallback<MouseMoveEvent>(Repaint);
doubleField.OnValueChanged(evt =>

SetValue(value);
m_UndoGroup = -1;
Dirty(ChangeType.Repaint);
});
doubleField.RegisterCallback<InputEvent>(evt =>
{

var value = GetValue();
value[index] = newValue;
SetValue(value);
Dirty(ChangeType.Repaint);
});
doubleField.RegisterCallback<KeyDownEvent>(evt =>
{

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


AbstractMaterialGraph m_Graph;
PreviewManager m_PreviewManager;
MasterNode m_MasterNode;
PreviewData m_PreviewHandle;
PreviewRenderData m_PreviewRenderHandle;
List<INode> m_SelectedNodes;

}
Add(bottomContainer);
masterNode = graph.GetNodes<MasterNode>().FirstOrDefault();
m_PreviewRenderHandle = previewManager.masterRenderData;
m_PreviewRenderHandle.onPreviewChanged += OnPreviewChanged;
foreach (var property in m_Graph.properties)
m_PropertyItems.Add(new ShaderPropertyView(m_Graph, property));

MasterNode masterNode
{
get { return m_MasterNode; }
set
{
if (value == m_MasterNode)
return;
if (m_MasterNode != null)
m_PreviewHandle.onPreviewChanged -= OnPreviewChanged;
m_PreviewHandle = null;
m_MasterNode = value;
if (m_MasterNode != null)
{
m_PreviewHandle = m_PreviewManager.GetPreview(m_MasterNode);
m_PreviewHandle.onPreviewChanged += OnPreviewChanged;
}
}
get { return m_PreviewRenderHandle.shaderData.node as MasterNode; }
}
void OnAddProperty()

void OnPreviewChanged()
{
m_PreviewTextureView.image = m_PreviewHandle.texture ?? Texture2D.blackTexture;
m_PreviewTextureView.image = m_PreviewRenderHandle.texture ?? Texture2D.blackTexture;
m_PreviewTextureView.Dirty(ChangeType.Repaint);
}

{
m_PreviewHandle.mesh = null;
m_PreviewRenderHandle.mesh = null;
m_PersistentMasterNodePreviewMesh.mesh = null;
}

{
m_PreviewHandle.mesh = changedMesh;
m_PreviewRenderHandle.mesh = changedMesh;
m_PersistentMasterNodePreviewMesh.mesh = changedMesh;
}

foreach (var layer in layerGraph.addedLayers)
m_LayerItems.Add(new ShaderLayerView(layerGraph, layer));
}
if (masterNode != null)
{
if (m_Graph.removedNodes.Contains(masterNode))
masterNode = null;
}
if (masterNode == null)
{
var addedMasterNode = m_Graph.addedNodes.OfType<MasterNode>().FirstOrDefault();
if (addedMasterNode != null)
masterNode = addedMasterNode;
}
if (m_PreviewHandle != null)
if (m_PreviewRenderHandle != null)
m_PreviewHandle.onPreviewChanged -= OnPreviewChanged;
m_PreviewHandle = null;
m_PreviewRenderHandle.onPreviewChanged -= OnPreviewChanged;
m_PreviewRenderHandle = null;
}
}
}

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


using System.Linq;
using UnityEditor.Graphing.Util;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{

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


graphEditorView = new GraphEditorView(materialGraph, asset.name) {persistenceKey = AssetDatabase.AssetPathToGUID(AssetDatabase.GUIDToAssetPath(selectedGuid))};
}
graphEditorView.previewManager.HandleGraphChanges();
graphEditorView.previewManager.RenderPreviews();
graphEditorView.inspectorView.HandleGraphChanges();
graphObject.graph.ClearChanges();
}

if (path.Length == 0)
return;
graphObject.RegisterCompleteObjectUndo("Convert To Subgraph");
Vector2 middle = Vector2.zero;
var bounds = Rect.MinMaxRect(float.PositiveInfinity, float.PositiveInfinity, float.NegativeInfinity, float.NegativeInfinity);
middle += node.drawState.position.center;
var center = node.drawState.position.center;
bounds = Rect.MinMaxRect(
Mathf.Min(bounds.xMin, center.x),
Mathf.Min(bounds.yMin, center.y),
Mathf.Max(bounds.xMax, center.x),
Mathf.Max(bounds.yMax, center.y));
middle /= nodes.Length;
var middle = bounds.center;
bounds.center = Vector2.zero;
var copyPasteGraph = new CopyPasteGraph(
graphView.selection.OfType<MaterialNodeView>().Where(x => !(x.node is PropertyNode)).Select(x => x.node as INode),

return;
var subGraph = new SubGraph();
subGraph.AddNode(new SubGraphOutputNode());
var subGraphOutputNode = new SubGraphOutputNode();
{
var drawState = subGraphOutputNode.drawState;
drawState.position = new Rect(new Vector2(bounds.xMax + 200f, 0f), drawState.position.size);
subGraphOutputNode.drawState = drawState;
}
subGraph.AddNode(subGraphOutputNode);
var nodeGuidMap = new Dictionary<Guid, Guid>();
foreach (var node in deserialized.GetNodes<INode>())

nodeGuidMap[oldGuid] = newGuid;
var drawState = node.drawState;
drawState.position = new Rect(drawState.position.position - middle, drawState.position.size);
node.drawState = drawState;
subGraph.AddNode(node);
}

{
subGraph.AddShaderProperty(prop);
var propNode = new PropertyNode();
{
var drawState = propNode.drawState;
drawState.position = new Rect(new Vector2(bounds.xMin - 300f, 0f), drawState.position.size);
propNode.drawState = drawState;
}
subGraph.AddNode(propNode);
propNode.propertyGuid = prop.guid;

var subGraphNode = new SubGraphNode();
var ds = subGraphNode.drawState;
ds.position = new Rect(middle, Vector2.one);
ds.position = new Rect(middle - new Vector2(100f, 150f), Vector2.zero);
subGraphNode.drawState = ds;
graphObject.graph.AddNode(subGraphNode);
subGraphNode.subGraphAsset = loadedSubGraph;

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


public class PreviewManager : IDisposable
{
AbstractMaterialGraph m_Graph;
Dictionary<Guid, PreviewData> m_Previews = new Dictionary<Guid, PreviewData>();
Dictionary<Guid, PreviewRenderData> m_RenderDatas = new Dictionary<Guid, PreviewRenderData>();
Dictionary<Guid, PreviewShaderData> m_ShaderDatas = new Dictionary<Guid, PreviewShaderData>();
PreviewRenderData m_MasterRenderData;
HashSet<Guid> m_DirtyPreviews = new HashSet<Guid>();
HashSet<Guid> m_DirtyShaders = new HashSet<Guid>();
HashSet<Guid> m_TimeDependentPreviews = new HashSet<Guid>();

public PreviewRate previewRate { get; set; }
public PreviewRenderData masterRenderData
{
get { return m_MasterRenderData; }
}
public PreviewManager(AbstractMaterialGraph graph)
{
m_Graph = graph;

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 }
};
public PreviewData GetPreview(INode node)
public PreviewRenderData GetPreview(INode node)
return m_Previews[node.guid];
return m_RenderDatas[node.guid];
var previewData = new PreviewData
PreviewShaderData shaderData;
if (!m_ShaderDatas.TryGetValue(node.guid, out shaderData))
{
shaderData = new PreviewShaderData
{
node = node
};
m_ShaderDatas[node.guid] = shaderData;
}
var previewData = new PreviewRenderData
node = node,
renderTexture = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default) { hideFlags = HideFlags.HideAndDontSave }
shaderData = shaderData,
renderTexture = new RenderTexture(200, 200, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default) { hideFlags = HideFlags.HideAndDontSave }
if (m_Previews.ContainsKey(node.guid))
if (m_RenderDatas.ContainsKey(node.guid))
m_Previews.Add(node.guid, previewData);
m_RenderDatas.Add(node.guid, previewData);
var masterNode = node as IMasterNode;
if (masterRenderData.shaderData == null && masterNode != null)
{
masterRenderData.shaderData = shaderData;
}
m_Previews.Remove(node.guid);
m_RenderDatas.Remove(node.guid);
if (masterRenderData.shaderData != null && masterRenderData.shaderData.node == node)
masterRenderData.shaderData = m_ShaderDatas.Values.FirstOrDefault(x => x.node is IMasterNode);
}
void OnNodeModified(INode node, ModificationScope scope)

m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
}
List<PreviewData> m_RenderList2D = new List<PreviewData>();
List<PreviewData> m_RenderList3D = new List<PreviewData>();
List<PreviewRenderData> m_RenderList2D = new List<PreviewRenderData>();
List<PreviewRenderData> m_RenderList3D = new List<PreviewRenderData>();
HashSet<Guid> m_NodesWith3DPreview = new HashSet<Guid>();
public void RenderPreviews()

foreach (var node in uberNodes)
{
PreviewData previewData;
if (!m_Previews.TryGetValue(node.guid, out previewData))
PreviewShaderData shaderData;
if (!m_ShaderDatas.TryGetValue(node.guid, out shaderData))
previewData.previewMode = m_NodesWith3DPreview.Contains(node.guid) ? PreviewMode.Preview3D : PreviewMode.Preview2D;
previewData.shader = m_UberShader;
shaderData.previewMode = m_NodesWith3DPreview.Contains(node.guid) ? PreviewMode.Preview3D : PreviewMode.Preview2D;
shaderData.shader = m_UberShader;
}
i++;
EditorUtility.DisplayProgressBar("Shader Graph", string.Format("Compiling preview shaders ({0}/{1})", i, count), 0f);

foreach (var nodeGuid in m_DirtyPreviews)
{
PreviewData previewData;
if (!m_Previews.TryGetValue(nodeGuid, out previewData))
PreviewRenderData renderData;
if (!m_RenderDatas.TryGetValue(nodeGuid, out renderData))
if (previewData.shader == null)
if (renderData.shaderData.shader == null)
previewData.texture = null;
renderData.texture = null;
if (MaterialGraphAsset.ShaderHasError(previewData.shader))
if (MaterialGraphAsset.ShaderHasError(renderData.shaderData.shader))
previewData.texture = m_ErrorTexture;
renderData.texture = m_ErrorTexture;
if (previewData.previewMode == PreviewMode.Preview2D)
m_RenderList2D.Add(previewData);
if (renderData.shaderData.previewMode == PreviewMode.Preview2D)
m_RenderList2D.Add(renderData);
m_RenderList3D.Add(previewData);
m_RenderList3D.Add(renderData);
if (masterRenderData.shaderData != null && m_DirtyPreviews.Contains(masterRenderData.shaderData.node.guid))
m_RenderList3D.Add(masterRenderData);
var time = Time.realtimeSinceStartup;
EditorUtility.SetCameraAnimateMaterialsTime(m_SceneResources.camera, time);

m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.orthographicSize = 1;
m_SceneResources.camera.orthographic = true;
foreach (var previewData in m_RenderList2D)
foreach (var renderData in m_RenderList2D)
if (m_UberShaderIds.TryGetValue(previewData.node.guid, out outputId))
if (m_UberShaderIds.TryGetValue(renderData.shaderData.node.guid, out outputId))
m_PreviewMaterial.shader = previewData.shader;
m_SceneResources.camera.targetTexture = previewData.renderTexture;
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);
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;

previewData.texture = previewData.renderTexture;
renderData.texture = renderData.renderTexture;
m_RenderList2D.Clear();
// Render 3D previews
m_SceneResources.camera.transform.position = -Vector3.forward * 5;

{
int outputId;
if (m_UberShaderIds.TryGetValue(previewData.node.guid, out outputId))
if (m_UberShaderIds.TryGetValue(previewData.shaderData.node.guid, out outputId))
m_PreviewMaterial.shader = previewData.shader;
m_PreviewMaterial.shader = previewData.shaderData.shader;
m_SceneResources.camera.targetTexture = previewData.renderTexture;
var previousRenderTexure = RenderTexture.active;
RenderTexture.active = previewData.renderTexture;

Graphics.DrawMesh(mesh, Matrix4x4.TRS(-mesh.bounds.center, Quaternion.identity, Vector3.one), m_PreviewMaterial, 1, m_SceneResources.camera, 0, m_PreviewPropertyBlock, ShadowCastingMode.Off, false, null, false);
var previousUseSRP = Unsupported.useScriptableRenderPipeline;
Unsupported.useScriptableRenderPipeline = previewData.node is IMasterNode;
Unsupported.useScriptableRenderPipeline = previewData.shaderData.node is IMasterNode;
m_RenderList3D.Clear();
foreach (var nodeGuid in m_DirtyPreviews)
foreach (var previewRenderData in m_RenderList2D.Union(m_RenderList3D))
PreviewData previewData;
if (!m_Previews.TryGetValue(nodeGuid, out previewData))
continue;
if (previewData.onPreviewChanged != null)
previewData.onPreviewChanged();
if (previewRenderData.onPreviewChanged != null)
previewRenderData.onPreviewChanged();
m_RenderList2D.Clear();
m_RenderList3D.Clear();
m_DirtyPreviews.Clear();
}

if (node == null)
return;
PreviewData previewData;
if (!m_Previews.TryGetValue(nodeGuid, out previewData))
PreviewShaderData shaderData;
if (!m_ShaderDatas.TryGetValue(nodeGuid, out shaderData))
previewData.previewMode = m_NodesWith3DPreview.Contains(nodeGuid) ? PreviewMode.Preview3D : PreviewMode.Preview2D;
shaderData.previewMode = m_NodesWith3DPreview.Contains(nodeGuid) ? PreviewMode.Preview3D : PreviewMode.Preview2D;
previewData.shaderString = null;
shaderData.shaderString = null;
previewData.shaderString = m_Graph.GetPreviewShader(node, out mode);
if (node is IMasterNode)
{
List<PropertyCollector.TextureInfo> configuredTextures;
shaderData.shaderString = ((IMasterNode)node).GetShader(GenerationMode.Preview, node.name, out configuredTextures);
}
else
shaderData.shaderString = m_Graph.GetPreviewShader(node, out mode);
File.WriteAllText(Application.dataPath + "/../GeneratedShader.shader", (previewData.shaderString ?? "null").Replace("UnityEngine.MaterialGraph", "Generated"));
File.WriteAllText(Application.dataPath + "/../GeneratedShader.shader", (shaderData.shaderString ?? "null").Replace("UnityEngine.MaterialGraph", "Generated"));
if (string.IsNullOrEmpty(previewData.shaderString))
if (string.IsNullOrEmpty(shaderData.shaderString))
if (previewData.shader != null)
Object.DestroyImmediate(previewData.shader, true);
previewData.shader = null;
if (shaderData.shader != null)
Object.DestroyImmediate(shaderData.shader, true);
shaderData.shader = null;
if (previewData.shader != null && MaterialGraphAsset.ShaderHasError(previewData.shader))
if (shaderData.shader != null && MaterialGraphAsset.ShaderHasError(shaderData.shader))
ShaderUtil.ClearShaderErrors(previewData.shader);
Object.DestroyImmediate(previewData.shader, true);
previewData.shader = null;
ShaderUtil.ClearShaderErrors(shaderData.shader);
Object.DestroyImmediate(shaderData.shader, true);
shaderData.shader = null;
if (previewData.shader == null)
if (shaderData.shader == null)
previewData.shader = ShaderUtil.CreateShaderAsset(previewData.shaderString);
previewData.shader.hideFlags = HideFlags.HideAndDontSave;
shaderData.shader = ShaderUtil.CreateShaderAsset(shaderData.shaderString);
shaderData.shader.hideFlags = HideFlags.HideAndDontSave;
ShaderUtil.ClearShaderErrors(previewData.shader);
ShaderUtil.UpdateShaderAsset(previewData.shader, previewData.shaderString);
ShaderUtil.ClearShaderErrors(shaderData.shader);
ShaderUtil.UpdateShaderAsset(shaderData.shader, shaderData.shaderString);
var message = "RecreateShader: " + node.GetVariableNameForNode() + Environment.NewLine + previewData.shaderString;
if (MaterialGraphAsset.ShaderHasError(previewData.shader))
var message = "RecreateShader: " + node.GetVariableNameForNode() + Environment.NewLine + shaderData.shaderString;
if (MaterialGraphAsset.ShaderHasError(shaderData.shader))
void DestroyPreview(Guid nodeGuid, PreviewData previewData)
void DestroyPreview(Guid nodeGuid, PreviewRenderData previewRenderData)
if (m_Previews.Remove(nodeGuid))
if (m_RenderDatas.Remove(nodeGuid))
if (previewData.shader != null)
Object.DestroyImmediate(previewData.shader, true);
if (previewData.renderTexture != null)
Object.DestroyImmediate(previewData.renderTexture, true);
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 (node != null)
node.onModified -= OnNodeModified;

previewData.shader = null;
previewData.renderTexture = null;
previewData.texture = null;
previewData.onPreviewChanged = null;
previewRenderData.shaderData.shader = null;
previewRenderData.renderTexture = null;
previewRenderData.texture = null;
previewRenderData.onPreviewChanged = null;
}
}

if (m_SceneResources != null)
m_SceneResources.Dispose();
m_SceneResources = null;
var previews = m_Previews.ToList();
var previews = m_RenderDatas.ToList();
foreach (var kvp in previews)
DestroyPreview(kvp.Key, kvp.Value);
}

public delegate void OnPreviewChanged();
public class PreviewData
public class PreviewShaderData
public Mesh mesh { get; set; }
}
public class PreviewRenderData
{
public PreviewShaderData shaderData { get; set; }
public Mesh mesh { get; set; }
public RenderTexture renderTexture { get; set; }
public Texture texture { get; set; }
public OnPreviewChanged onPreviewChanged;

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


var textureInfo = new List<PropertyCollector.TextureInfo>();
PreviewMode previewMode;
FloatShaderProperty outputIdProperty;
string shader = graph.GetShader(copyFromNode, GenerationMode.ForReals, assetName, out textureInfo, out previewMode, out outputIdProperty);
GUIUtility.systemCopyBuffer = shader;
if (copyFromNode is MasterNode)
{
var shader = ((MasterNode)copyFromNode).GetShader(GenerationMode.ForReals, copyFromNode.name, out textureInfo);
GUIUtility.systemCopyBuffer = shader;
}
else
{
string shader = graph.GetShader(copyFromNode, GenerationMode.ForReals, assetName, out textureInfo, out previewMode, out outputIdProperty);
GUIUtility.systemCopyBuffer = shader;
}
}
));

public void HandleGraphChanges()
{
previewManager.HandleGraphChanges();
previewManager.RenderPreviews();
inspectorView.HandleGraphChanges();
foreach (var node in m_Graph.removedNodes)
{
node.onModified -= OnNodeChanged;

62
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs


public sealed class MaterialNodeView : Node
{
List<VisualElement> m_ControlViews;
PreviewData m_PreviewData;
PreviewRenderData m_PreviewRenderData;
VisualElement m_ControlsDivider;
public void Initialize(GraphView graphView, AbstractMaterialNode inNode, PreviewManager previewManager)
{

{
name = "controls"
};
leftContainer.Add(m_ControlsContainer);
extensionContainer.Add(m_ControlsContainer);
m_ControlsDivider = new VisualElement {name = "divider"};
m_ControlsDivider.AddToClassList("horizontal");
if (node.hasPreview)
{

pickingMode = PickingMode.Ignore,
image = Texture2D.whiteTexture
};
m_PreviewData = previewManager.GetPreview(inNode);
m_PreviewData.onPreviewChanged += UpdatePreviewTexture;
m_PreviewRenderData = previewManager.GetPreview(inNode);
m_PreviewRenderData.onPreviewChanged += UpdatePreviewTexture;
m_PreviewContainer.Add(m_PreviewTextureView);
var collapsePreviewButton = new Label { name = "collapse", text = "▲" };
var collapsePreviewButton = new VisualElement { name = "collapse"};
collapsePreviewButton.Add(new VisualElement { name = "icon" });
collapsePreviewButton.AddManipulator(new Clickable(() =>
{
node.owner.owner.RegisterCompleteObjectUndo("Collapse Preview");

m_PreviewContainer.Add(collapsePreviewButton);
m_PreviewTextureView.Add(collapsePreviewButton);
var expandPreviewButton = new Label { name = "expand", text = "▼" };
var expandPreviewButton = new VisualElement { name = "expand"};
expandPreviewButton.Add(new VisualElement { name = "icon"});
expandPreviewButton.AddManipulator(new Clickable(() =>
{
node.owner.owner.RegisterCompleteObjectUndo("Expand Preview");

}
leftContainer.Add(m_PreviewContainer);
extensionContainer.Add(m_PreviewContainer);
}
m_ControlViews = new List<VisualElement>();

m_Attachers = new List<Attacher>(node.GetInputSlots<MaterialSlot>().Count());
expanded = node.drawState.expanded;
expanded = node.drawState.expanded;
RefreshExpandedState(); //This should not be needed. GraphView needs to improve the extension api here
UpdatePortInputVisibilities();
SetPosition(new Rect(node.drawState.position.x, node.drawState.position.y, 0, 0));

var resizeHandle = new Label { name = "resize", text = "" };
resizeHandle.AddManipulator(new Draggable(OnResize));
Add(resizeHandle);
UpdateSize();
}
}

UpdateControls();
UpdatePortInputVisibilities();
RefreshExpandedState(); //This should not be needed. GraphView needs to improve the extension api here
}
}

return;
if (expanded)
{
if (m_PreviewTextureView.parent != m_PreviewContainer)
{
m_PreviewContainer.Add(m_PreviewTextureView);
}
if (m_PreviewTextureView.parent == m_PreviewContainer)
{
m_PreviewTextureView.RemoveFromHierarchy();
}
m_PreviewContainer.RemoveFromClassList("expanded");
m_PreviewContainer.AddToClassList("collapsed");
}

{
inputContainer.Add(port);
var portInputView = new PortInputView(slot);
m_GraphView.AddElement(portInputView);
m_Attachers.Add(new Attacher(portInputView, port, SpriteAlignment.LeftCenter) { distance = 0f });
Add(portInputView);
m_Attachers.Add(new Attacher(portInputView, port, SpriteAlignment.LeftCenter) { distance = -8f });
}
}
}

void OnResize(Vector2 deltaSize)
{
var updatedWidth = leftContainer.layout.width + deltaSize.x;
var updatedWidth = topContainer.layout.width + deltaSize.x;
var updatedHeight = m_PreviewTextureView.layout.height + deltaSize.y;
var previewNode = node as PreviewNode;

void UpdatePreviewTexture()
{
if (m_PreviewData.texture == null || !node.previewExpanded)
if (m_PreviewRenderData.texture == null || !node.previewExpanded)
{
m_PreviewTextureView.visible = false;
m_PreviewTextureView.image = Texture2D.blackTexture;

m_PreviewTextureView.visible = true;
m_PreviewTextureView.AddToClassList("visible");
m_PreviewTextureView.RemoveFromClassList("hidden");
m_PreviewTextureView.image = m_PreviewData.texture;
if (m_PreviewTextureView.image != m_PreviewRenderData.texture)
m_PreviewTextureView.image = m_PreviewRenderData.texture;
else
m_PreviewTextureView.Dirty(ChangeType.Repaint);
m_PreviewTextureView.Dirty(ChangeType.Repaint);
}
void UpdateControls()

m_ControlsContainer.Clear();
m_ControlsDivider.RemoveFromHierarchy();
}
else if (m_ControlsContainer.childCount != m_ControlViews.Count)
{

extensionContainer.Add(m_ControlsDivider);
if (m_PreviewContainer != null)
m_ControlsDivider.PlaceBehind(m_PreviewContainer);
}
}

var width = previewNode.width;
var height = previewNode.height;
leftContainer.style.width = width;
m_PreviewTextureView.style.height = height;
}

m_Attachers.Clear();
node = null;
if (m_PreviewData != null)
if (m_PreviewRenderData != null)
m_PreviewData.onPreviewChanged -= UpdatePreviewTexture;
m_PreviewData = null;
m_PreviewRenderData.onPreviewChanged -= UpdatePreviewTexture;
m_PreviewRenderData = null;
}
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PortInputView.cs


m_EdgeControl = new EdgeControl
{
@from = new Vector2(212f - 20f, 11.5f),
@from = new Vector2(212f - 21f, 11.5f),
edgeWidth = 2
edgeWidth = 2,
pickingMode = PickingMode.Ignore
};
Add(m_EdgeControl);

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


void AddField(Vector4 initialValue, int index, string subLabel)
{
Add(new Label(subLabel));
var label = new Label(subLabel);
Add(label);
var dragger = new FieldMouseDragger<double>(doubleField);
dragger.SetDragZone(label);
doubleField.OnValueChanged(evt =>
{
var value = m_Get();

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


background-color: rgb(20, 21, 21);
}
MaterialNodeView > .mainContainer > #left > #previewContainer {
width: 200;
MaterialNodeView #previewContainer,
MaterialNodeView #controls {
background-color: #393939;
MaterialNodeView > .mainContainer > #left > #previewContainer.expanded > #preview.active,
MaterialNodeView > .mainContainer > #left > #previewContainer.expanded > #preview.visible {
MaterialNodeView #previewContainer > #preview {
align-items:center;
MaterialNodeView > .mainContainer > #left > #previewContainer.collapsed > #collapse {
MaterialNodeView #previewContainer > #preview > #collapse {
background-color: #000;
border-color: #F0F0F0;
width: 0;
}
MaterialNodeView > .mainContainer > #left > #previewContainer.expanded > #collapse {
border-radius: 2;
text-alignment: middle-center;
position-left: 87;
position-right: 87;
position-top: 5;
position-type: absolute;
height: 0;
border-top-width: 0;
border-bottom-width: 0;
border-left-width: 0;
border-right-width: 0;
}
MaterialNodeView > .mainContainer > #left > #previewContainer.expanded:hover > #collapse {
background-color: rgba(0, 0, 0, 0.5);
text-color: rgba(255, 255, 255, 0.75);
border-color: rgba(255, 255, 255, 0.75);
opacity: 0;
border-radius: 1;
height: 26;
margin-top: 4;
align-items:center;
justify-content:center;
MaterialNodeView > .mainContainer > #left > #previewContainer.expanded > #collapse:hover {
background-color: rgba(0, 0, 0, 0.75);
text-color: rgba(255, 255, 255, 1);
border-color: rgba(255, 255, 255, 1);
MaterialNodeView #previewContainer:hover > #preview > #collapse {
width: 20;
height: 20;
opacity: 0.6;
MaterialNodeView > .mainContainer > #left > #previewContainer.expanded > #collapse:active {
background-color: rgba(0, 0, 0, 0.66);
MaterialNodeView #previewContainer > #preview > #collapse > #icon
{
background-image : resource("GraphView/Nodes/PreviewCollapse.png");
width: 16;
height: 16;
}
MaterialNodeView #previewContainer > #preview > #collapse:hover {
opacity: 1.0;
MaterialNodeView > .mainContainer > #left > #previewContainer.expanded > #expand {
MaterialNodeView #previewContainer.collapsed > #preview > #collapse {
MaterialNodeView > .mainContainer > #left > #previewContainer.collapsed > #expand {
text-alignment: middle-center;
text-color: rgb(153, 153, 153);
MaterialNodeView #previewContainer > #expand {
width: 50;
width: 56;
height: 16;
flex-direction: row;
justify-content:center;
}
MaterialNodeView #previewContainer > #expand > #icon {
align-self: center;
background-image : resource("GraphView/Nodes/PreviewExpand.png");
width: 16;
height: 16;
MaterialNodeView > .mainContainer > #left > #previewContainer.collapsed > #expand:hover {
background-color: rgba(0, 0, 0, 0.5);
text-color: rgba(255, 255, 255, 1);
MaterialNodeView #previewContainer.collapsed > #expand:hover {
background-color: #2B2B2B;
}
MaterialNodeView #previewContainer.expanded > #expand {
height: 0;
}
MaterialNodeView > #resize {

flex-direction: row;
align-items: center;
padding-left: 8;
margin-right: 10;
margin-right: 12;
border-left-width: 1;
border-top-width: 1;
border-right-width: 1;

MultiFloatSlotControlView > Label {
margin-left: 0;
margin-right: 0;
cursor: slide-arrow;
}
MultiFloatSlotControlView > DoubleField {

MultiFloatControlView {
flex-direction: row;
padding-left: 8;
padding-right: 8;
padding-top: 8;
padding-bottom: 8;
cursor: slide-arrow;
flex: 1;
min-width: 30;
flex-grow: 1;
}
ObjectControlView {

ObjectControlView > ObjectField {
flex: 1;
}
EnumControlView {
flex-direction: row;
}
EnumControlView > EnumField {
flex: 1;
margin-left: 0;
margin-right: 8;
margin-top: 8;
margin-bottom: 8;
}
EnumControlView > Label {
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
margin-left: 8;
margin-right: 8;
margin-top: 8;
margin-bottom: 8;
}
PropertyControlView {
padding-left: 8;
padding-right: 8;
padding-top: 8;
padding-bottom: 8;
}

108
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightUnlitPass.template


SubShader
Pass
Tags{"RenderType" = "Opaque" "IgnoreProjectors" = "True" "RenderPipeline" = "LightweightPipeline"}
LOD ${LOD}
Tags{"LightMode" = "LightweightForward"}
${Tags}
${Blending}
${Culling}
${ZTest}
${ZWrite}
CGPROGRAM
#pragma target 3.0
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile_fog
#pragma multi_compile_instancing
Pass
{
Tags{"LightMode" = "LightweightForward"}
${Tags}
${Blending}
${Culling}
${ZTest}
${ZWrite}
CGPROGRAM
#pragma target 3.0
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma vertex vert
#pragma fragment frag
#pragma glsl
#pragma debug
#pragma vertex vert
#pragma fragment frag
#pragma glsl
#pragma debug
#include "UnityCG.cginc"
#include "UnityCG.cginc"
${Defines}
${Defines}
${Graph}
struct GraphVertexOutput
{
float4 position : POSITION;
${Interpolators}
UNITY_VERTEX_OUTPUT_STEREO
};
struct GraphVertexOutput
{
float4 position : POSITION;
${Interpolators}
UNITY_VERTEX_OUTPUT_STEREO
};
GraphVertexOutput vert (GraphVertexInput v)
{
v = PopulateVertexData(v);
UNITY_SETUP_INSTANCE_ID(v);
GraphVertexOutput vert (GraphVertexInput v)
{
v = PopulateVertexData(v);
UNITY_SETUP_INSTANCE_ID(v);
GraphVertexOutput o;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.position = UnityObjectToClipPos(v.vertex);
${VertexShader}
return o;
}
GraphVertexOutput o;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.position = UnityObjectToClipPos(v.vertex);
${VertexShader}
return o;
}
fixed4 frag (GraphVertexOutput IN) : SV_Target
{
${LocalPixelShader}
SurfaceInputs surfaceInput;
${SurfaceInputs}
fixed4 frag (GraphVertexOutput IN) : SV_Target
{
${LocalPixelShader}
SurfaceInputs surfaceInput;
${SurfaceInputs}
SurfaceDescription surf = PopulateSurfaceData(surfaceInput);
float3 Color = 0;
float3 Alpha = 0;
${SurfaceOutputRemap}
#ifdef _ALPHABLEND_ON
return fixed4(Color, Alpha);
#else
return fixed4(Color, 1.0);
#endif
}
ENDCG
SurfaceDescription surf = PopulateSurfaceData(surfaceInput);
float3 Color = 0;
float3 Alpha = 0;
${SurfaceOutputRemap}
return fixed4(Color, Alpha);
ENDCG
}

2
MaterialGraphProject/ProjectSettings/ProjectVersion.txt


m_EditorVersion: 2018.1.0a6
m_EditorVersion: 2018.1.0a7

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


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public class LightWeightPBRSubShader
{
Pass m_ForwardPassMetallic = new Pass()
{
Name = "LightweightForward",
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlbedoSlotId,
PBRMasterNode.NormalSlotId,
PBRMasterNode.EmissionSlotId,
PBRMasterNode.MetallicSlotId,
PBRMasterNode.SmoothnessSlotId,
PBRMasterNode.OcclusionSlotId,
PBRMasterNode.AlphaSlotId
}
};
struct Pass
{
public string Name;
public List<int> VertexShaderSlots;
public List<int> PixelShaderSlots;
}
Pass m_ForwardPassSpecular = new Pass()
{
Name = "LightweightForward",
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlbedoSlotId,
PBRMasterNode.NormalSlotId,
PBRMasterNode.EmissionSlotId,
PBRMasterNode.SpecularSlotId,
PBRMasterNode.SmoothnessSlotId,
PBRMasterNode.OcclusionSlotId,
PBRMasterNode.AlphaSlotId
}
};
private static string GetShaderPassFromTemplate(string template, PBRMasterNode masterNode, Pass pass, GenerationMode mode)
{
var surfaceVertexShader = new ShaderGenerator();
var surfaceDescriptionFunction = new ShaderGenerator();
var surfaceDescriptionStruct = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var surfaceInputs = new ShaderGenerator();
var shaderProperties = new PropertyCollector();
var graphVertexInput = @"
struct GraphVertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 color : COLOR;
float4 texcoord0 : TEXCOORD0;
float4 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};";
surfaceInputs.AddShaderChunk("struct SurfaceInputs{", false);
surfaceInputs.Indent();
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, masterNode, NodeUtils.IncludeSelf.Include, pass.PixelShaderSlots);
var requirements = AbstractMaterialGraph.GetRequirements(activeNodeList);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresNormal, InterpolatorType.Normal, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresTangent, InterpolatorType.Tangent, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresBitangent, InterpolatorType.BiTangent, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresViewDir, InterpolatorType.ViewDirection, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresPosition, InterpolatorType.Position, surfaceInputs);
if (requirements.requiresVertexColor)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.VertexColor), false);
if (requirements.requiresScreenPosition)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.ScreenPosition), false);
foreach (var channel in requirements.requiresMeshUVs.Distinct())
surfaceInputs.AddShaderChunk(string.Format("half4 {0};", channel.GetUVName()), false);
surfaceInputs.Deindent();
surfaceInputs.AddShaderChunk("};", false);
surfaceVertexShader.AddShaderChunk("GraphVertexInput PopulateVertexData(GraphVertexInput v){", false);
surfaceVertexShader.Indent();
surfaceVertexShader.AddShaderChunk("return v;", false);
surfaceVertexShader.Deindent();
surfaceVertexShader.AddShaderChunk("}", false);
var slots = new List<MaterialSlot>();
foreach (var id in pass.PixelShaderSlots)
slots.Add(masterNode.FindSlot<MaterialSlot>(id));
AbstractMaterialGraph.GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, slots, true);
var usedSlots = new List<MaterialSlot>();
foreach (var id in pass.PixelShaderSlots)
usedSlots.Add(masterNode.FindSlot<MaterialSlot>(id));
AbstractMaterialGraph.GenerateSurfaceDescription(
activeNodeList,
masterNode,
masterNode.owner as AbstractMaterialGraph,
surfaceDescriptionFunction,
shaderFunctionVisitor,
shaderProperties,
requirements,
mode,
"PopulateSurfaceData",
"SurfaceDescription",
null,
null,
usedSlots);
var graph = new ShaderGenerator();
graph.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
graph.AddShaderChunk(graphVertexInput, false);
graph.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
graph.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);
graph.AddShaderChunk(shaderProperties.GetPropertiesDeclaration(2), false);
graph.AddShaderChunk(surfaceVertexShader.GetShaderString(2), false);
graph.AddShaderChunk(surfaceDescriptionFunction.GetShaderString(2), false);
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
var materialOptions = new SurfaceMaterialOptions();
switch (masterNode.alphaMode)
{
case PBRMasterNode.AlphaMode.Overwrite:
case PBRMasterNode.AlphaMode.Clip:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.Zero;
materialOptions.cullMode = SurfaceMaterialOptions.CullMode.Back;
materialOptions.zTest = SurfaceMaterialOptions.ZTest.LEqual;
materialOptions.zWrite = SurfaceMaterialOptions.ZWrite.On;
materialOptions.renderQueue = SurfaceMaterialOptions.RenderQueue.Geometry;
materialOptions.renderType = SurfaceMaterialOptions.RenderType.Opaque;
break;
case PBRMasterNode.AlphaMode.AlphaBlend:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.SrcAlpha;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.OneMinusSrcAlpha;
materialOptions.cullMode = SurfaceMaterialOptions.CullMode.Back;
materialOptions.zTest = SurfaceMaterialOptions.ZTest.LEqual;
materialOptions.zWrite = SurfaceMaterialOptions.ZWrite.Off;
materialOptions.renderQueue = SurfaceMaterialOptions.RenderQueue.Transparent;
materialOptions.renderType = SurfaceMaterialOptions.RenderType.Transparent;
break;
case PBRMasterNode.AlphaMode.AdditiveBlend:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.cullMode = SurfaceMaterialOptions.CullMode.Back;
materialOptions.zTest = SurfaceMaterialOptions.ZTest.LEqual;
materialOptions.zWrite = SurfaceMaterialOptions.ZWrite.Off;
materialOptions.renderQueue = SurfaceMaterialOptions.RenderQueue.Transparent;
materialOptions.renderType = SurfaceMaterialOptions.RenderType.Transparent;
break;
}
materialOptions.GetTags(tagsVisitor);
materialOptions.GetBlend(blendingVisitor);
materialOptions.GetCull(cullingVisitor);
materialOptions.GetDepthTest(zTestVisitor);
materialOptions.GetDepthWrite(zWriteVisitor);
var interpolators = new ShaderGenerator();
var localVertexShader = new ShaderGenerator();
var localPixelShader = new ShaderGenerator();
var localSurfaceInputs = new ShaderGenerator();
var surfaceOutputRemap = new ShaderGenerator();
var reqs = ShaderGraphRequirements.none;
reqs.requiresNormal |= NeededCoordinateSpace.World;
reqs.requiresTangent |= NeededCoordinateSpace.World;
reqs.requiresBitangent |= NeededCoordinateSpace.World;
reqs.requiresPosition |= NeededCoordinateSpace.World;
reqs.requiresViewDir |= NeededCoordinateSpace.World;
ShaderGenerator.GenerateStandardTransforms(
3,
10,
interpolators,
localVertexShader,
localPixelShader,
localSurfaceInputs,
requirements,
reqs,
CoordinateSpace.World);
ShaderGenerator defines = new ShaderGenerator();
defines.AddShaderChunk("#define _GLOSSYREFLECTIONS_ON", true);
defines.AddShaderChunk("#define _SPECULARHIGHLIGHTS_ON", true);
if (masterNode.IsSlotConnected(PBRMasterNode.NormalSlotId))
defines.AddShaderChunk("#define _NORMALMAP 1", true);
if (masterNode.model == PBRMasterNode.Model.Metallic)
defines.AddShaderChunk("#define _METALLIC_SETUP 1", true);
switch (masterNode.alphaMode)
{
case PBRMasterNode.AlphaMode.AlphaBlend:
case PBRMasterNode.AlphaMode.AdditiveBlend:
defines.AddShaderChunk("#define _AlphaOut 1", true);
break;
case PBRMasterNode.AlphaMode.Clip:
defines.AddShaderChunk("#define _AlphaClip 1", true);
break;
}
var templateLocation = ShaderGenerator.GetTemplatePath(template);
foreach (var slot in usedSlots)
{
surfaceOutputRemap.AddShaderChunk(slot.shaderOutputName
+ " = surf."
+ slot.shaderOutputName + ";", true);
}
if (!File.Exists(templateLocation))
return string.Empty;
var subShaderTemplate = File.ReadAllText(templateLocation);
var resultPass = subShaderTemplate.Replace("${Defines}", defines.GetShaderString(3));
resultPass = resultPass.Replace("${Graph}", graph.GetShaderString(3));
resultPass = resultPass.Replace("${Interpolators}", interpolators.GetShaderString(3));
resultPass = resultPass.Replace("${VertexShader}", localVertexShader.GetShaderString(3));
resultPass = resultPass.Replace("${LocalPixelShader}", localPixelShader.GetShaderString(3));
resultPass = resultPass.Replace("${SurfaceInputs}", localSurfaceInputs.GetShaderString(3));
resultPass = resultPass.Replace("${SurfaceOutputRemap}", surfaceOutputRemap.GetShaderString(3));
resultPass = resultPass.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${LOD}", "" + materialOptions.lod);
return resultPass;
}
public IEnumerable<string> GetSubshader(PBRMasterNode masterNode, GenerationMode mode)
{
var subShader = new ShaderGenerator();
subShader.AddShaderChunk("SubShader", true);
subShader.AddShaderChunk("{", true);
subShader.Indent();
subShader.AddShaderChunk("Tags{ \"RenderPipeline\" = \"LightweightPipeline\"}", true);
subShader.AddShaderChunk(
GetShaderPassFromTemplate(
"lightweightPBRForwardPass.template",
masterNode,
masterNode.model == PBRMasterNode.Model.Metallic ? m_ForwardPassMetallic : m_ForwardPassSpecular,
mode),
true);
var extraPassesTemplateLocation = ShaderGenerator.GetTemplatePath("lightweightPBRExtraPasses.template");
if (File.Exists(extraPassesTemplateLocation))
subShader.AddShaderChunk(File.ReadAllText(extraPassesTemplateLocation), true);
subShader.Deindent();
subShader.AddShaderChunk("}", true);
return new[] { subShader.GetShaderString(0) };
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs.meta


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

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


using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public class LightWeightUnlitSubShader
{
Pass m_UnlitPass = new Pass()
{
Name = "Unlit",
PixelShaderSlots = new List<int>()
{
UnlitMasterNode.ColorSlotId,
UnlitMasterNode.AlphaSlotId
}
};
struct Pass
{
public string Name;
public List<int> VertexShaderSlots;
public List<int> PixelShaderSlots;
}
private static string GetShaderPassFromTemplate(string template, UnlitMasterNode masterNode, Pass pass, GenerationMode mode)
{
var surfaceVertexShader = new ShaderGenerator();
var surfaceDescriptionFunction = new ShaderGenerator();
var surfaceDescriptionStruct = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var surfaceInputs = new ShaderGenerator();
var shaderProperties = new PropertyCollector();
var graphVertexInput = @"
struct GraphVertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 color : COLOR;
float4 texcoord0 : TEXCOORD0;
float4 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};";
surfaceInputs.AddShaderChunk("struct SurfaceInputs{", false);
surfaceInputs.Indent();
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, masterNode, NodeUtils.IncludeSelf.Include, pass.PixelShaderSlots);
var requirements = AbstractMaterialGraph.GetRequirements(activeNodeList);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresNormal, InterpolatorType.Normal, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresTangent, InterpolatorType.Tangent, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresBitangent, InterpolatorType.BiTangent, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresViewDir, InterpolatorType.ViewDirection, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresPosition, InterpolatorType.Position, surfaceInputs);
if (requirements.requiresVertexColor)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.VertexColor), false);
if (requirements.requiresScreenPosition)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.ScreenPosition), false);
foreach (var channel in requirements.requiresMeshUVs.Distinct())
surfaceInputs.AddShaderChunk(string.Format("half4 {0};", channel.GetUVName()), false);
surfaceInputs.Deindent();
surfaceInputs.AddShaderChunk("};", false);
surfaceVertexShader.AddShaderChunk("GraphVertexInput PopulateVertexData(GraphVertexInput v){", false);
surfaceVertexShader.Indent();
surfaceVertexShader.AddShaderChunk("return v;", false);
surfaceVertexShader.Deindent();
surfaceVertexShader.AddShaderChunk("}", false);
var slots = new List<MaterialSlot>();
foreach (var id in pass.PixelShaderSlots)
{
var slot = masterNode.FindSlot<MaterialSlot>(id);
if (slot != null)
slots.Add(slot);
}
AbstractMaterialGraph.GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, slots, true);
var usedSlots = new List<MaterialSlot>();
foreach (var id in pass.PixelShaderSlots)
usedSlots.Add(masterNode.FindSlot<MaterialSlot>(id));
AbstractMaterialGraph.GenerateSurfaceDescription(
activeNodeList,
masterNode,
masterNode.owner as AbstractMaterialGraph,
surfaceDescriptionFunction,
shaderFunctionVisitor,
shaderProperties,
requirements,
mode,
"PopulateSurfaceData",
"SurfaceDescription",
null,
null,
usedSlots);
var graph = new ShaderGenerator();
graph.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
graph.AddShaderChunk(graphVertexInput, false);
graph.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
graph.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);
graph.AddShaderChunk(shaderProperties.GetPropertiesDeclaration(2), false);
graph.AddShaderChunk(surfaceVertexShader.GetShaderString(2), false);
graph.AddShaderChunk(surfaceDescriptionFunction.GetShaderString(2), false);
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
var materialOptions = new SurfaceMaterialOptions();
materialOptions.GetTags(tagsVisitor);
materialOptions.GetBlend(blendingVisitor);
materialOptions.GetCull(cullingVisitor);
materialOptions.GetDepthTest(zTestVisitor);
materialOptions.GetDepthWrite(zWriteVisitor);
var interpolators = new ShaderGenerator();
var localVertexShader = new ShaderGenerator();
var localPixelShader = new ShaderGenerator();
var localSurfaceInputs = new ShaderGenerator();
var surfaceOutputRemap = new ShaderGenerator();
var reqs = ShaderGraphRequirements.none;
reqs.requiresNormal |= NeededCoordinateSpace.World;
reqs.requiresTangent |= NeededCoordinateSpace.World;
reqs.requiresBitangent |= NeededCoordinateSpace.World;
reqs.requiresPosition |= NeededCoordinateSpace.World;
reqs.requiresViewDir |= NeededCoordinateSpace.World;
ShaderGenerator.GenerateStandardTransforms(
3,
10,
interpolators,
localVertexShader,
localPixelShader,
localSurfaceInputs,
requirements,
reqs,
CoordinateSpace.World);
ShaderGenerator defines = new ShaderGenerator();
var templateLocation = ShaderGenerator.GetTemplatePath(template);
foreach (var slot in usedSlots)
{
surfaceOutputRemap.AddShaderChunk(slot.shaderOutputName
+ " = surf."
+ slot.shaderOutputName + ";", true);
}
if (!File.Exists(templateLocation))
return string.Empty;
var subShaderTemplate = File.ReadAllText(templateLocation);
var resultPass = subShaderTemplate.Replace("${Defines}", defines.GetShaderString(3));
resultPass = resultPass.Replace("${Graph}", graph.GetShaderString(3));
resultPass = resultPass.Replace("${Interpolators}", interpolators.GetShaderString(3));
resultPass = resultPass.Replace("${VertexShader}", localVertexShader.GetShaderString(3));
resultPass = resultPass.Replace("${LocalPixelShader}", localPixelShader.GetShaderString(3));
resultPass = resultPass.Replace("${SurfaceInputs}", localSurfaceInputs.GetShaderString(3));
resultPass = resultPass.Replace("${SurfaceOutputRemap}", surfaceOutputRemap.GetShaderString(3));
resultPass = resultPass.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${LOD}", "" + materialOptions.lod);
return resultPass;
}
public IEnumerable<string> GetSubshader(UnlitMasterNode masterNode, GenerationMode mode)
{
var subShader = new ShaderGenerator();
subShader.AddShaderChunk("SubShader", true);
subShader.AddShaderChunk("{", true);
subShader.Indent();
subShader.AddShaderChunk("Tags{ \"RenderType\" = \"Opaque\" \"RenderPipeline\" = \"LightweightPipeline\"}", true);
subShader.AddShaderChunk(
GetShaderPassFromTemplate(
"lightweightUnlitPass.template",
masterNode,
m_UnlitPass,
mode),
true);
subShader.Deindent();
subShader.AddShaderChunk("}", true);
return new[] { subShader.GetShaderString(0) };
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs.meta


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

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


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
[Title("Master/PBR")]
public class PBRMasterNode : MasterNode
{
public const string AlbedoSlotName = "Albedo";
public const string NormalSlotName = "Normal";
public const string EmissionSlotName = "Emission";
public const string MetallicSlotName = "Metallic";
public const string SpecularSlotName = "Specular";
public const string SmoothnessSlotName = "Smoothness";
public const string OcclusionSlotName = "Occlusion";
public const string AlphaSlotName = "Alpha";
public const string VertexOffsetName = "VertexPosition";
public const int AlbedoSlotId = 0;
public const int NormalSlotId = 1;
public const int MetallicSlotId = 2;
public const int SpecularSlotId = 3;
public const int EmissionSlotId = 4;
public const int SmoothnessSlotId = 5;
public const int OcclusionSlotId = 6;
public const int AlphaSlotId = 7;
public enum Model
{
Specular,
Metallic
}
public enum AlphaMode
{
Overwrite,
AlphaBlend,
AdditiveBlend,
Clip
}
[SerializeField]
private Model m_Model = Model.Metallic;
[EnumControl("")]
public Model model
{
get { return m_Model; }
set
{
if (m_Model == value)
return;
m_Model = value;
UpdateNodeAfterDeserialization();
if (onModified != null)
{
onModified(this, ModificationScope.Topological);
}
}
}
[SerializeField]
private AlphaMode m_AlphaMode;
[EnumControl("")]
public AlphaMode alphaMode
{
get { return m_AlphaMode; }
set
{
if (m_AlphaMode == value)
return;
m_AlphaMode = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public PBRMasterNode()
{
UpdateNodeAfterDeserialization();
}
public bool IsSlotConnected(int slotId)
{
var slot = FindSlot<MaterialSlot>(slotId);
return slot != null && owner.GetEdges(slot.slotReference).Any();
}
public sealed override void UpdateNodeAfterDeserialization()
{
name = "PBR Master";
AddSlot(new Vector3MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, new Vector4(0.5f, 0.5f, 0.5f), ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, new Vector3(0, 0, 1), ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, Vector3.zero, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(MetallicSlotId, MetallicSlotName, MetallicSlotName, SlotType.Input, 0, ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, Vector3.zero, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, 0.5f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, 1f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 1f, ShaderStage.Fragment));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
model == Model.Metallic ? MetallicSlotId : SpecularSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId
});
}
public override string GetShader(GenerationMode mode, string outputName, out List<PropertyCollector.TextureInfo> configuredTextures)
{
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
var shaderProperties = new PropertyCollector();
var abstractMaterialGraph = owner as AbstractMaterialGraph;
if (abstractMaterialGraph != null)
abstractMaterialGraph.CollectShaderProperties(shaderProperties, mode);
foreach (var activeNode in activeNodeList.OfType<AbstractMaterialNode>())
activeNode.CollectShaderProperties(shaderProperties, mode);
var finalShader = new ShaderGenerator();
finalShader.AddShaderChunk(string.Format(@"Shader ""{0}""", outputName), false);
finalShader.AddShaderChunk("{", false);
finalShader.Indent();
finalShader.AddShaderChunk("Properties", false);
finalShader.AddShaderChunk("{", false);
finalShader.Indent();
finalShader.AddShaderChunk(shaderProperties.GetPropertiesBlock(2), false);
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);
var lwSub = new LightWeightPBRSubShader();
foreach (var subshader in lwSub.GetSubshader(this, mode))
finalShader.AddShaderChunk(subshader, true);
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);
configuredTextures = shaderProperties.GetConfiguredTexutres();
return finalShader.GetShaderString(0);
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/PBRMasterNode.cs.meta


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

79
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/UnlitMasterNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
[Title("Master/Unlit")]
public class UnlitMasterNode : MasterNode
{
public const string ColorSlotName = "Color";
public const string AlphaSlotName = "Alpha";
public const string VertexOffsetName = "VertexPosition";
public const int ColorSlotId = 0;
public const int AlphaSlotId = 7;
public UnlitMasterNode()
{
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
name = "Unlit Master";
AddSlot(new Vector3MaterialSlot(ColorSlotId, ColorSlotName, ColorSlotName, SlotType.Input, new Vector3(0.5f, 0.5f, 0.5f), ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 1, ShaderStage.Fragment));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
ColorSlotId,
AlphaSlotId
});
}
public override string GetShader(GenerationMode mode, string outputName, out List<PropertyCollector.TextureInfo> configuredTextures)
{
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
var shaderProperties = new PropertyCollector();
var abstractMaterialGraph = owner as AbstractMaterialGraph;
if (abstractMaterialGraph != null)
abstractMaterialGraph.CollectShaderProperties(shaderProperties, mode);
foreach (var activeNode in activeNodeList.OfType<AbstractMaterialNode>())
activeNode.CollectShaderProperties(shaderProperties, mode);
var finalShader = new ShaderGenerator();
finalShader.AddShaderChunk(string.Format(@"Shader ""{0}""", outputName), false);
finalShader.AddShaderChunk("{", false);
finalShader.Indent();
finalShader.AddShaderChunk("Properties", false);
finalShader.AddShaderChunk("{", false);
finalShader.Indent();
finalShader.AddShaderChunk(shaderProperties.GetPropertiesBlock(2), false);
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);
var lwSub = new LightWeightUnlitSubShader();
foreach (var subshader in lwSub.GetSubshader(this, mode))
finalShader.AddShaderChunk(subshader, true);
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);
configuredTextures = shaderProperties.GetConfiguredTexutres();
return finalShader.GetShaderString(0);
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/UnlitMasterNode.cs.meta


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

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask.meta


fileFormatVersion: 2
guid: cdb8c403de0823942a95a464c32ad0f0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

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


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

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


fileFormatVersion: 2
guid: 74fdde12d8253bd4c874acc555be0585
timeCreated: 1507817885

40
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRExtraPasses.template


Pass
{
Tags{"Lightmode" = "ShadowCaster"}
ZWrite On ZTest LEqual
CGPROGRAM
#pragma target 2.0
#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment
#include "UnityCG.cginc"
#include "LightweightPassShadow.cginc"
ENDCG
}
Pass
{
Tags{"Lightmode" = "DepthOnly"}
ZWrite On
CGPROGRAM
#pragma target 2.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 vert(float4 pos : POSITION) : SV_POSITION
{
return UnityObjectToClipPos(pos);
}
half4 frag() : SV_TARGET
{
return 0;
}
ENDCG
}

7
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRExtraPasses.template.meta


fileFormatVersion: 2
guid: 5fa0a7ad9ac73b34691439e5cf40a1c3
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

145
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRForwardPass.template


Pass
{
Tags{"LightMode" = "LightweightForward"}
${Tags}
${Blending}
${Culling}
${ZTest}
${ZWrite}
CGPROGRAM
#pragma target 3.0
#pragma multi_compile _MAIN_DIRECTIONAL_LIGHT _MAIN_SPOT_LIGHT _MAIN_POINT_LIGHT
#pragma multi_compile _ _ADDITIONAL_LIGHTS
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile _ LIGHTMAP_ON
#pragma multi_compile _ _HARD_SHADOWS _SOFT_SHADOWS _HARD_SHADOWS_CASCADES _SOFT_SHADOWS_CASCADES
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma vertex vert
#pragma fragment frag
#pragma glsl
#pragma debug
${Defines}
#include "LightweightLighting.cginc"
${Graph}
struct GraphVertexOutput
{
float4 position : POSITION;
float4 lwCustom : TEXCOORD0;
float4 fogCoord : TEXCOORD1; // x: fogCoord, yzw: vertexColor
${Interpolators}
UNITY_VERTEX_OUTPUT_STEREO
};
GraphVertexOutput vert (GraphVertexInput v)
{
v = PopulateVertexData(v);
UNITY_SETUP_INSTANCE_ID(v);
GraphVertexOutput o = (GraphVertexOutput)0;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.position = UnityObjectToClipPos(v.vertex);
${VertexShader}
#ifdef LIGHTMAP_ON
o.lwCustom.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
float3 lwWNormal = normalize(UnityObjectToWorldNormal(v.normal));
float3 lwWorldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
// TODO: change to only support point lights per vertex. This will greatly simplify shader ALU
#if defined(_VERTEX_LIGHTS) && defined(_MULTIPLE_LIGHTS)
half3 diffuse = half3(1.0, 1.0, 1.0);
// pixel lights shaded = min(pixelLights, perObjectLights)
// vertex lights shaded = min(vertexLights, perObjectLights) - pixel lights shaded
// Therefore vertexStartIndex = pixelLightCount; vertexEndIndex = min(vertexLights, perObjectLights)
int vertexLightStart = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int vertexLightEnd = min(globalLightCount.y, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = vertexLightStart; lightIter < vertexLightEnd; ++lightIter)
{
int lightIndex = unity_4LightIndices0[lightIter];
LightInput lightInput;
INITIALIZE_LIGHT(lightInput, lightIndex);
half3 lightDirection;
half atten = ComputeLightAttenuationVertex(lightInput, lwWNormal, lwWorldPos, lightDirection);
o.fogCoord.yzw += LightingLambert(diffuse, lightDirection, lwWNormal, atten);
}
#endif
#if defined(_LIGHT_PROBES_ON) && !defined(LIGHTMAP_ON)
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(lwWNormal, 1)));
#endif
UNITY_TRANSFER_FOG(o, o.position);
return o;
}
fixed4 frag (GraphVertexOutput IN) : SV_Target
{
${LocalPixelShader}
SurfaceInputs surfaceInput = (SurfaceInputs)0;
${SurfaceInputs}
SurfaceDescription surf = PopulateSurfaceData(surfaceInput);
float3 Albedo = float3(0.5, 0.5, 0.5);
float3 Specular = float3(0, 0, 0);
float Metallic = 0;
float3 Normal = float3(0, 0, 1);
float3 Emission = 0;
float Smoothness = 0.5;
float Occlusion = 1;
float Alpha = 1;
${SurfaceOutputRemap}
#if defined(UNITY_COLORSPACE_GAMMA)
Albedo = Albedo * Albedo;
Emission = Emission * Emission;
#endif
half4 result = LightweightFragmentPBR(
IN.lwCustom,
WorldSpacePosition,
WorldSpaceNormal,
WorldSpaceTangent,
WorldSpaceBiTangent,
WorldSpaceViewDirection,
IN.fogCoord.x,
Albedo,
Metallic,
Specular,
Smoothness,
Normal,
Occlusion,
Emission,
Alpha);
#if _AlphaOut
result.a = Alpha;
#else
result.a = 1;
#endif
#if _AlphaClip
clip(Alpha - 0.01);
#endif
return result;
}
ENDCG
}

37
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ColorMaskNode.cs


using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Artistic/Mask/Color Mask")]
public class ColorMaskNode : CodeFunctionNode
{
public ColorMaskNode()
{
name = "Color Mask";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_ColorMask", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_ColorMask(
[Slot(0, Binding.None)] Vector3 In,
[Slot(1, Binding.None)] Color MaskColor,
[Slot(2, Binding.None)] Vector1 Range,
[Slot(3, Binding.None)] out Vector3 Out)
{
Out = Vector3.zero;
return
@"
{
{precision}3 col = {precision}3(0, 0, 0);
{precision} Distance = distance(MaskColor, In);
if(Distance <= Range)
col = {precision}3(1, 1, 1);
Out = col;
}";
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ColorMaskNode.cs.meta


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

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


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

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


using System.Reflection;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
namespace UnityEditor.ShaderGraph
{
public enum TextureChannel
{
Red,
Green,
Blue,
Alpha
}
[Title("Artistic/Mask/Channel Mask")]
public class ChannelMaskNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction
{
public ChannelMaskNode()
{
name = "Channel Mask";
UpdateNodeAfterDeserialization();
}
const int InputSlotId = 0;
const int OutputSlotId = 1;
const string kInputSlotName = "In";
const string kOutputSlotName = "Out";
public override bool hasPreview
{
get { return true; }
}
string GetFunctionName()
{
return string.Format("Unity_ChannelMask_{0}_{1}", channel, precision);
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new DynamicVectorMaterialSlot(InputSlotId, kInputSlotName, kInputSlotName, SlotType.Input, Vector3.zero));
AddSlot(new DynamicVectorMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector3.zero));
RemoveSlotsNameNotMatching(new[] { InputSlotId, OutputSlotId });
}
[SerializeField]
private TextureChannel m_Channel = TextureChannel.Red;
[ChannelEnumControl("Channel")]
public TextureChannel channel
{
get { return m_Channel; }
set
{
if (m_Channel == value)
return;
m_Channel = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
void ValidateChannelCount()
{
int channelCount = (int)SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(InputSlotId).concreteValueType);
if ((int)channel >= channelCount)
channel = TextureChannel.Red;
}
string GetFunctionPrototype(string argIn, string argOut)
{
return string.Format("void {0} ({1} {2}, out {3} {4})", GetFunctionName(),
ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<DynamicVectorMaterialSlot>(InputSlotId).concreteValueType), argIn,
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<DynamicVectorMaterialSlot>(OutputSlotId).concreteValueType), argOut);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
ValidateChannelCount();
string inputValue = GetSlotValue(InputSlotId, generationMode);
string outputValue = GetSlotValue(OutputSlotId, generationMode);
visitor.AddShaderChunk(string.Format("{0} {1};", ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType), GetVariableNameForSlot(OutputSlotId)), true);
visitor.AddShaderChunk(GetFunctionCallBody(inputValue, outputValue), true);
}
string GetFunctionCallBody(string inputValue, string outputValue)
{
return GetFunctionName() + " (" + inputValue + ", " + outputValue + ");";
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
ValidateChannelCount();
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("In", "Out"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
switch(channel)
{
case TextureChannel.Green:
outputString.AddShaderChunk("Out = In.yyyy;", false);
break;
case TextureChannel.Blue:
outputString.AddShaderChunk("Out = In.zzzz;", false);
break;
case TextureChannel.Alpha:
outputString.AddShaderChunk("Out = In.wwww;", false);
break;
default:
outputString.AddShaderChunk("Out = In.xxxx;", false);
break;
}
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/AbstractLightweightPBRMasterNode.cs.meta


fileFormatVersion: 2
guid: 1ac259924f8784a4aa1eb5e9e556b08d
timeCreated: 1478188276
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightMetallicMasterNode.cs.meta


fileFormatVersion: 2
guid: 4b9e77744d433de4186738959cadcfca
timeCreated: 1478188276
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightSpecularMasterNode.cs.meta


fileFormatVersion: 2
guid: bf358c1c084b6cf4b9a4a9cc49cd61f0
timeCreated: 1478188276
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightUnlitMasterNode.cs.meta


fileFormatVersion: 2
guid: 19892eaf92f3e2e49928945f6da04525
timeCreated: 1478188276
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/AbstractLightweightMasterNode.cs.meta


fileFormatVersion: 2
guid: 72c867ea29464bec864ea612e2bcecd3
timeCreated: 1505799928

57
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/AbstractLightweightPBRMasterNode.cs


using System;
using System.Linq;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public abstract class AbstractLightweightPBRMasterNode : AbstractLightweightMasterNode
{
public const string AlbedoSlotName = "Albedo";
public const string NormalSlotName = "Normal";
public const string EmissionSlotName = "Emission";
public const string SmoothnessSlotName = "Smoothness";
public const string OcclusionSlotName = "Occlusion";
public const string AlphaSlotName = "Alpha";
public const string VertexOffsetName = "VertexPosition";
public const int AlbedoSlotId = 0;
public const int NormalSlotId = 1;
public const int EmissionSlotId = 3;
public const int SmoothnessSlotId = 4;
public const int OcclusionSlotId = 5;
public const int AlphaSlotId = 6;
protected override void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap, MasterRemapGraph remapper)
{
base.GetLightweightDefinesAndRemap(defines, surfaceOutputRemap, remapper);
defines.AddShaderChunk("#define _GLOSSYREFLECTIONS_ON", true);
defines.AddShaderChunk("#define _SPECULARHIGHLIGHTS_ON", true);
if(IsNormalMapConnected())
defines.AddShaderChunk("#define _NORMALMAP 1", true);
}
protected override int GetInterpolatorStartIndex()
{
return 2;
}
public override ShaderGraphRequirements GetNodeSpecificRequirements()
{
var reqs = ShaderGraphRequirements.none;
reqs.requiresNormal |= NeededCoordinateSpace.World;
reqs.requiresTangent |= NeededCoordinateSpace.World;
reqs.requiresBitangent |= NeededCoordinateSpace.World;
reqs.requiresPosition |= NeededCoordinateSpace.World;
reqs.requiresViewDir |= NeededCoordinateSpace.World;
return base.GetNodeSpecificRequirements().Union(reqs);
}
bool IsNormalMapConnected()
{
var nm = FindSlot<MaterialSlot>(NormalSlotId);
return owner.GetEdges(nm.slotReference).Any();
}
}
}

84
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightMetallicMasterNode.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
[Title("Master/Lightweight/PBR Metallic")]
public class LightweightMetallicMasterNode : AbstractLightweightPBRMasterNode
{
public const string MetallicSlotName = "Metallic";
public const int MetallicSlotId = 2;
public LightweightMetallicMasterNode()
{
name = "LightweightMetallicMasterNode";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector3MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, new Vector4(0.5f, 0.5f, 0.5f), ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, new Vector3(0,0,1), ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, Vector3.zero, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(MetallicSlotId, MetallicSlotName, MetallicSlotName, SlotType.Input,0, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, 0.5f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, 1f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 0, ShaderStage.Fragment));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
MetallicSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId
});
}
protected override string GetTemplateName()
{
return "lightweightSubshaderPBR.template";
}
protected override void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap, MasterRemapGraph remapper)
{
base.GetLightweightDefinesAndRemap(defines, surfaceOutputRemap, remapper);
defines.AddShaderChunk("#define _METALLIC_SETUP 1", true);
}
protected override IEnumerable<int> masterSurfaceInputs
{
get
{
return new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
MetallicSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId,
};
}
}
protected override IEnumerable<int> masterVertexInputs
{
get
{
return new int[]
{
};
}
}
}
}

79
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightSpecularMasterNode.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
[Title("Master/Lightweight/PBR Specular")]
public class LightweightSpecularMasterNode : AbstractLightweightPBRMasterNode
{
public const string SpecularSlotName = "Specular";
public const int SpecularSlotId = 2;
public const string WorkflowName = "Specular";
public LightweightSpecularMasterNode()
{
name = "LightweightSpecularMasterNode";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector3MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, new Vector3(0.5f, 0.5f, 0.5f), ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, new Vector3(0,0,1), ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, Vector3.zero, ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, Vector3.zero, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, 0, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input,0, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 1, ShaderStage.Fragment));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
SpecularSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId
});
}
protected override string GetTemplateName()
{
return "lightweightSubshaderPBR.template";
}
protected override IEnumerable<int> masterSurfaceInputs
{
get
{
return new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
SpecularSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId,
};
}
}
protected override IEnumerable<int> masterVertexInputs
{
get
{
return new int[]
{
};
}
}
}
}

71
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightweightUnlitMasterNode.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
[Title("Master/Lightweight/Unlit")]
public class LightweightUnlitMasterNode : AbstractLightweightMasterNode
{
public const string ColorSlotName = "Color";
public const string AlphaSlotName = "Alpha";
public const int ColorSlotId = 0;
public const int AlphaSlotId = 1;
public LightweightUnlitMasterNode()
{
name = "LightweightUnlitMasterNode";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector3MaterialSlot(ColorSlotId, ColorSlotName, ColorSlotName, SlotType.Input, Vector3.zero, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 0, ShaderStage.Fragment));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
ColorSlotId,
AlphaSlotId
});
}
protected override IEnumerable<int> masterSurfaceInputs
{
get
{
return new[]
{
ColorSlotId,
AlphaSlotId,
};
}
}
protected override IEnumerable<int> masterVertexInputs
{
get
{
return new int[]
{
};
}
}
protected override string GetTemplateName()
{
return "lightweightSubshaderUnlit.template";
}
protected override int GetInterpolatorStartIndex()
{
return 0;
}
}
}

126
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/AbstractLightweightMasterNode.cs


using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public abstract class AbstractLightweightMasterNode : MasterNode
{
private const int kMaxInterpolators = 8;
protected abstract IEnumerable<int> masterSurfaceInputs { get; }
protected abstract IEnumerable<int> masterVertexInputs { get; }
protected abstract string GetTemplateName();
protected virtual void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap, MasterRemapGraph remapper)
{
// Step 1: no remapper, working with raw master node..
if (remapper == null)
{
foreach (var slot in GetInputSlots<MaterialSlot>())
{
surfaceOutputRemap.AddShaderChunk(slot.shaderOutputName
+ " = surf."
+ slot.shaderOutputName + ";", true);
}
}
// Step 2: remapper present... complex workflow time
else
{
surfaceOutputRemap.AddShaderChunk("{", false);
surfaceOutputRemap.Indent();
foreach (var prop in remapper.properties)
{
surfaceOutputRemap.AddShaderChunk(prop.GetInlinePropertyDeclarationString(), true);
surfaceOutputRemap.AddShaderChunk(string.Format("{0} = surf.{0};", prop.referenceName), true);
}
List<INode> nodes = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, NodeUtils.IncludeSelf.Exclude);
foreach (var activeNode in nodes.OfType<AbstractMaterialNode>())
{
if (activeNode is IGeneratesBodyCode)
(activeNode as IGeneratesBodyCode).GenerateNodeCode(surfaceOutputRemap, GenerationMode.ForReals);
}
foreach (var input in GetInputSlots<MaterialSlot>())
{
foreach (var edge in owner.GetEdges(input.slotReference))
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
surfaceOutputRemap.AddShaderChunk(
string.Format("{0} = {1};", input.shaderOutputName,
fromNode.GetVariableNameForSlot(outputRef.slotId)), true);
}
}
surfaceOutputRemap.Deindent();
surfaceOutputRemap.AddShaderChunk("}", false);
}
}
public override IEnumerable<string> GetSubshader(ShaderGraphRequirements graphRequirements, MasterRemapGraph remapper)
{
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
m_MaterialOptions.GetTags(tagsVisitor);
m_MaterialOptions.GetBlend(blendingVisitor);
m_MaterialOptions.GetCull(cullingVisitor);
m_MaterialOptions.GetDepthTest(zTestVisitor);
m_MaterialOptions.GetDepthWrite(zWriteVisitor);
var interpolators = new ShaderGenerator();
var vertexShader = new ShaderGenerator();
var localPixelShader = new ShaderGenerator();
var surfaceInputs = new ShaderGenerator();
ShaderGenerator.GenerateStandardTransforms(
GetInterpolatorStartIndex(),
10,
interpolators,
vertexShader,
localPixelShader,
surfaceInputs,
graphRequirements,
GetNodeSpecificRequirements(),
CoordinateSpace.World);
ShaderGenerator defines = new ShaderGenerator();
ShaderGenerator surfaceOutputRemap = new ShaderGenerator();
GetLightweightDefinesAndRemap(defines, surfaceOutputRemap, remapper);
var templateLocation = ShaderGenerator.GetTemplatePath(GetTemplateName());
if (!File.Exists(templateLocation))
return new string[] {};
var subShaderTemplate = File.ReadAllText(templateLocation);
var resultShader = subShaderTemplate.Replace("${Defines}", defines.GetShaderString(3));
resultShader = resultShader.Replace("${Interpolators}", interpolators.GetShaderString(3));
resultShader = resultShader.Replace("${VertexShader}", vertexShader.GetShaderString(3));
resultShader = resultShader.Replace("${LocalPixelShader}", localPixelShader.GetShaderString(3));
resultShader = resultShader.Replace("${SurfaceInputs}", surfaceInputs.GetShaderString(3));
resultShader = resultShader.Replace("${SurfaceOutputRemap}", surfaceOutputRemap.GetShaderString(3));
resultShader = resultShader.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${LOD}", "" + m_MaterialOptions.lod);
return new[] {resultShader};
}
protected abstract int GetInterpolatorStartIndex();
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/MasterRemapNode.cs.meta


fileFormatVersion: 2
guid: 861139fc6f60415cae2eb9a9625aaeb8
timeCreated: 1506873607

101
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/MasterRemapNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Master/Remap-Node")]
public class MasterRemapNode : AbstractSubGraphNode, IMasterNode
{
[SerializeField]
protected SurfaceMaterialOptions m_MaterialOptions = new SurfaceMaterialOptions();
public SurfaceMaterialOptions options
{
get { return m_MaterialOptions; }
}
public override PreviewMode previewMode
{
get { return PreviewMode.Preview3D; }
}
[SerializeField]
private string m_SerializedRemapGraph = string.Empty;
[Serializable]
private class RemapGraphHelper
{
public MasterRemapGraphAsset remapGraph;
}
protected override AbstractSubGraph referencedGraph
{
get
{
if (remapGraphAsset == null)
return null;
return remapGraphAsset.remapGraph;
}
}
#if UNITY_EDITOR
[ObjectControl("Remap")]
public MasterRemapGraphAsset remapGraphAsset
{
get
{
if (string.IsNullOrEmpty(m_SerializedRemapGraph))
return null;
var helper = new RemapGraphHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedRemapGraph, helper);
return helper.remapGraph;
}
set
{
if (remapGraphAsset == value)
return;
var helper = new RemapGraphHelper();
helper.remapGraph = value;
m_SerializedRemapGraph = EditorJsonUtility.ToJson(helper, true);
UpdateSlots();
if (onModified != null)
onModified(this, ModificationScope.Topological);
}
}
#else
public MaterialSubGraphAsset subGraphAsset {get; set; }
#endif
public MasterRemapNode()
{
name = "RemapMaster";
}
public IEnumerable<string> GetSubshader(ShaderGraphRequirements graphRequirements, MasterRemapGraph remapper)
{
if (referencedGraph == null)
return new string[]{};
var masterNodes = referencedGraph.activeNodes.OfType<IMasterNode>().ToList();
if (masterNodes.Count == 0)
return new string[]{};
var results = new List<string>();
foreach (var master in masterNodes)
results.AddRange(master.GetSubshader(graphRequirements, referencedGraph as MasterRemapGraph));
return results;
}
}
}

180
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template


SubShader
{
Tags{"RenderType" = "Opaque" "RenderPipeline" = "LightweightPipeline"}
LOD ${LOD}
Pass
{
Tags{"LightMode" = "LightweightForward"}
${Tags}
${Blending}
${Culling}
${ZTest}
${ZWrite}
CGPROGRAM
#pragma target 3.0
#pragma multi_compile _MAIN_DIRECTIONAL_LIGHT _MAIN_SPOT_LIGHT _MAIN_POINT_LIGHT
#pragma multi_compile _ _ADDITIONAL_LIGHTS
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile _ LIGHTMAP_ON
#pragma multi_compile _ _HARD_SHADOWS _SOFT_SHADOWS _HARD_SHADOWS_CASCADES _SOFT_SHADOWS_CASCADES
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma vertex vert
#pragma fragment frag
#pragma glsl
#pragma debug
${Defines}
#include "LightweightLighting.cginc"
struct GraphVertexOutput
{
float4 position : POSITION;
float4 lwCustom : TEXCOORD0;
float4 fogCoord : TEXCOORD1; // x: fogCoord, yzw: vertexColor
${Interpolators}
UNITY_VERTEX_OUTPUT_STEREO
};
GraphVertexOutput vert (GraphVertexInput v)
{
v = PopulateVertexData(v);
UNITY_SETUP_INSTANCE_ID(v);
GraphVertexOutput o = (GraphVertexOutput)0;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.position = UnityObjectToClipPos(v.vertex);
${VertexShader}
#ifdef LIGHTMAP_ON
o.lwCustom.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
float3 lwWNormal = normalize(UnityObjectToWorldNormal(v.normal));
float3 lwWorldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
// TODO: change to only support point lights per vertex. This will greatly simplify shader ALU
#if defined(_VERTEX_LIGHTS) && defined(_MULTIPLE_LIGHTS)
half3 diffuse = half3(1.0, 1.0, 1.0);
// pixel lights shaded = min(pixelLights, perObjectLights)
// vertex lights shaded = min(vertexLights, perObjectLights) - pixel lights shaded
// Therefore vertexStartIndex = pixelLightCount; vertexEndIndex = min(vertexLights, perObjectLights)
int vertexLightStart = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int vertexLightEnd = min(globalLightCount.y, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = vertexLightStart; lightIter < vertexLightEnd; ++lightIter)
{
int lightIndex = unity_4LightIndices0[lightIter];
LightInput lightInput;
INITIALIZE_LIGHT(lightInput, lightIndex);
half3 lightDirection;
half atten = ComputeLightAttenuationVertex(lightInput, lwWNormal, lwWorldPos, lightDirection);
o.fogCoord.yzw += LightingLambert(diffuse, lightDirection, lwWNormal, atten);
}
#endif
#if defined(_LIGHT_PROBES_ON) && !defined(LIGHTMAP_ON)
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(lwWNormal, 1)));
#endif
UNITY_TRANSFER_FOG(o, o.position);
return o;
}
fixed4 frag (GraphVertexOutput IN) : SV_Target
{
${LocalPixelShader}
SurfaceInputs surfaceInput = (SurfaceInputs)0;
${SurfaceInputs}
SurfaceDescription surf = PopulateSurfaceData(surfaceInput);
float3 Albedo = float3(0.5, 0.5, 0.5);
float3 Specular = float3(0, 0, 0);
float Metallic = 0;
float3 Normal = float3(0, 0, 1);
float3 Emission = 0;
float Smoothness = 0.5;
float Occlusion = 1;
float Alpha = 1;
${SurfaceOutputRemap}
#if defined(UNITY_COLORSPACE_GAMMA)
Albedo = Albedo * Albedo;
Emission = Emission * Emission;
#endif
return LightweightFragmentPBR(
IN.lwCustom,
WorldSpacePosition,
WorldSpaceNormal,
WorldSpaceTangent,
WorldSpaceBiTangent,
WorldSpaceViewDirection,
IN.fogCoord.x,
Albedo,
Metallic,
Specular,
Smoothness,
Normal,
Occlusion,
Emission,
Alpha);
}
ENDCG
}
Pass
{
Tags{"Lightmode" = "ShadowCaster"}
ZWrite On ZTest LEqual
CGPROGRAM
#pragma target 2.0
#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment
#include "UnityCG.cginc"
#include "LightweightPassShadow.cginc"
ENDCG
}
Pass
{
Tags{"Lightmode" = "DepthOnly"}
ZWrite On
CGPROGRAM
#pragma target 2.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 vert(float4 pos : POSITION) : SV_POSITION
{
return UnityObjectToClipPos(pos);
}
half4 frag() : SV_TARGET
{
return 0;
}
ENDCG
}
}

20
MaterialGraphProject/ProjectSettings/EditorSettings.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!159 &1
EditorSettings:
m_ObjectHideFlags: 0
serializedVersion: 5
m_ExternalVersionControlSupport: Visible Meta Files
m_SerializationMode: 2
m_DefaultBehaviorMode: 0
m_SpritePackerMode: 2
m_SpritePackerPaddingPower: 1
m_EtcTextureCompressorBehavior: 0
m_EtcTextureFastCompressor: 0
m_EtcTextureNormalCompressor: 2
m_EtcTextureBestCompressor: 5
m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd;uss
m_ProjectGenerationRootNamespace:
m_UserGeneratedProjectSuffix:
m_CollabEditorSettings:
inProgressEnabled: 1

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderUnlit.template.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightUnlitPass.template.meta

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderUnlit.template → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightUnlitPass.template

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRForwardPass.template.meta

正在加载...
取消
保存