浏览代码

Merge branch 'master' of github.com:Unity-Technologies/ShaderGraph into graphview-styling-updates

/main
mathieum-unity 7 年前
当前提交
bd9683ea
共有 9 个文件被更改,包括 240 次插入234 次删除
  1. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/NodeUtils.cs
  2. 1
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/INode.cs
  3. 196
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  4. 234
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/LayeredShaderGraph.cs
  5. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/IControlAttribute.cs
  6. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs
  7. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs
  8. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/INodeModificationListener.cs
  9. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/INodeModificationListener.cs.meta

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


nodeList.Add(node);
}
static Stack<INode> s_NodeStack = new Stack<INode>();
var nodeStack = new Stack<INode>();
nodeStack.Push(initialNode);
while (nodeStack.Any() && shaderStage == ShaderStage.Dynamic)
s_NodeStack.Clear();
s_NodeStack.Push(initialNode);
while (s_NodeStack.Any() && shaderStage == ShaderStage.Dynamic)
var node = nodeStack.Pop();
var node = s_NodeStack.Pop();
foreach (var slot in goingBackwards ? node.GetInputSlots<MaterialSlot>() : node.GetOutputSlots<MaterialSlot>())
{
if (shaderStage != ShaderStage.Dynamic)

var connectedNode = node.owner.GetNodeFromGuid(goingBackwards ? edge.outputSlot.nodeGuid : edge.inputSlot.nodeGuid);
var connectedSlot = goingBackwards ? connectedNode.FindOutputSlot<MaterialSlot>(edge.outputSlot.slotId) : connectedNode.FindInputSlot<MaterialSlot>(edge.inputSlot.slotId);
if (connectedSlot.shaderStage == ShaderStage.Dynamic)
nodeStack.Push(connectedNode);
s_NodeStack.Push(connectedNode);
else
{
shaderStage = connectedSlot.shaderStage;

1
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/INode.cs


}
public delegate void OnNodeModified(INode node, ModificationScope scope);
public delegate void OnNodeReplaced(INode previous, INode current);
public interface INode
{

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


using System.Linq;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.Graphing.Util;
namespace UnityEditor.ShaderGraph
{

base.OnAfterDeserialize();
}
protected static ShaderGraphRequirements GetRequierments(AbstractMaterialNode nodeForRequirements)
protected static ShaderGraphRequirements GetRequirements(List<INode> nodes)
if (nodeForRequirements == null)
return ShaderGraphRequirements.none;
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, nodeForRequirements);
NeededCoordinateSpace requiresNormal = activeNodeList.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal());
NeededCoordinateSpace requiresBitangent = activeNodeList.OfType<IMayRequireBitangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresBitangent());
NeededCoordinateSpace requiresTangent = activeNodeList.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresTangent());
NeededCoordinateSpace requiresViewDir = activeNodeList.OfType<IMayRequireViewDirection>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresViewDirection());
NeededCoordinateSpace requiresPosition = activeNodeList.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition());
bool requiresScreenPosition = activeNodeList.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
bool requiresVertexColor = activeNodeList.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
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());
NeededCoordinateSpace requiresTangent = nodes.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresTangent());
NeededCoordinateSpace requiresViewDir = nodes.OfType<IMayRequireViewDirection>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresViewDirection());
NeededCoordinateSpace requiresPosition = nodes.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition());
bool requiresScreenPosition = nodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
bool requiresVertexColor = nodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
if (activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
if (nodes.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
meshUV.Add(channel);
}

requiresMeshUVs = meshUV
};
ListPool<INode>.Release(activeNodeList);
return reqs;
}

return GetShader(node, GenerationMode.Preview, string.Format("hidden/preview/{0}", node.GetVariableNameForNode()), out configuredTextures, out previewMode);
}
protected static void GenerateSurfaceDescriptionStruct(ShaderGenerator surfaceDescriptionStruct, AbstractMaterialNode node, bool isMasterNode)
protected static void GenerateSurfaceDescriptionStruct(ShaderGenerator surfaceDescriptionStruct, List<MaterialSlot> slots)
if (isMasterNode)
foreach (var slot in slots)
foreach (var slot in node.GetInputSlots<MaterialSlot>())
if (slot.isInputSlot)
}
else
{
foreach (var slot in node.GetOutputSlots<MaterialSlot>())
surfaceDescriptionStruct.AddShaderChunk(string.Format("{0} {1};", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType), node.GetVariableNameForSlot(slot.id)), false);
else
surfaceDescriptionStruct.AddShaderChunk(string.Format("{0} {1};", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType), ((AbstractMaterialNode) slot.owner).GetVariableNameForSlot(slot.id)), false);
}
surfaceDescriptionStruct.Deindent();
surfaceDescriptionStruct.AddShaderChunk("};", false);

if (isMasterNode)
foreach (var slot in slots)
foreach (var slot in node.GetInputSlots<MaterialSlot>())
surfaceDescriptionStruct.AddShaderChunk( string.Format("surface.{0} = scale * surface.{0};", slot.shaderOutputName), false);
}
else
{
foreach (var slot in node.GetOutputSlots<MaterialSlot>())
surfaceDescriptionStruct.AddShaderChunk(string.Format("surface.{0} = scale * surface.{0};", node.GetVariableNameForSlot(slot.id)), false);
if (slot.isInputSlot)
surfaceDescriptionStruct.AddShaderChunk(string.Format("surface.{0} = scale * surface.{0};", slot.shaderOutputName), false);
else
surfaceDescriptionStruct.AddShaderChunk(string.Format("surface.{0} = scale * surface.{0};", ((AbstractMaterialNode) slot.owner).GetVariableNameForSlot(slot.id)), false);
}
surfaceDescriptionStruct.Deindent();
surfaceDescriptionStruct.AddShaderChunk("};", false);

if (isMasterNode)
{
foreach (var slot in node.GetInputSlots<MaterialSlot>())
{
var str = string.Format("base.{0} = base.{0} + add.{0};", slot.shaderOutputName);
surfaceDescriptionStruct.AddShaderChunk(str, false);
}
}
else
foreach (var slot in slots)
foreach (var slot in node.GetOutputSlots<MaterialSlot>())
{
var str = string.Format("base.{0} = base.{0} + add.{0};", node.GetVariableNameForSlot(slot.id));
surfaceDescriptionStruct.AddShaderChunk(str, false);
}
if (slot.isInputSlot)
surfaceDescriptionStruct.AddShaderChunk(string.Format("base.{0} = base.{0} + add.{0};", slot.shaderOutputName), false);
else
surfaceDescriptionStruct.AddShaderChunk(string.Format("base.{0} = base.{0} + add.{0};", ((AbstractMaterialNode) slot.owner).GetVariableNameForSlot(slot.id)), false);
}
surfaceDescriptionStruct.Deindent();
surfaceDescriptionStruct.AddShaderChunk("};", false);

List<INode> activeNodeList,
AbstractMaterialNode node,
ShaderGenerator surfaceDescriptionFunction,
ShaderGenerator shaderFunctionVisitor,

surfaceDescriptionFunction.AddShaderChunk(string.Format("{0} {1}(SurfaceInputs IN) {{", surfaceDescriptionName, functionName), false);
surfaceDescriptionFunction.Indent();
if ((requirements.requiresNormal & NeededCoordinateSpace.Object) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Object.ToVariableName(InterpolatorType.Normal)), false);
if ((requirements.requiresNormal & NeededCoordinateSpace.View) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.View.ToVariableName(InterpolatorType.Normal)), false);
if ((requirements.requiresNormal & NeededCoordinateSpace.World) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.World.ToVariableName(InterpolatorType.Normal)), false);
if ((requirements.requiresNormal & NeededCoordinateSpace.Tangent) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Tangent.ToVariableName(InterpolatorType.Normal)), false);
if ((requirements.requiresTangent & NeededCoordinateSpace.Object) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Object.ToVariableName(InterpolatorType.Tangent)), false);
if ((requirements.requiresTangent & NeededCoordinateSpace.View) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.View.ToVariableName(InterpolatorType.Tangent)), false);
if ((requirements.requiresTangent & NeededCoordinateSpace.World) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.World.ToVariableName(InterpolatorType.Tangent)), false);
if ((requirements.requiresTangent & NeededCoordinateSpace.Tangent) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Tangent.ToVariableName(InterpolatorType.Tangent)), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.Object) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Object.ToVariableName(InterpolatorType.BiTangent)), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.View) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.View.ToVariableName(InterpolatorType.BiTangent)), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.World) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.World.ToVariableName(InterpolatorType.BiTangent)), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.Tangent) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Tangent.ToVariableName(InterpolatorType.BiTangent)), false);
if ((requirements.requiresViewDir & NeededCoordinateSpace.Object) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Object.ToVariableName(InterpolatorType.ViewDirection)), false);
if ((requirements.requiresViewDir & NeededCoordinateSpace.View) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.View.ToVariableName(InterpolatorType.ViewDirection)), false);
if ((requirements.requiresViewDir & NeededCoordinateSpace.World) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.World.ToVariableName(InterpolatorType.ViewDirection)), false);
if ((requirements.requiresViewDir & NeededCoordinateSpace.Tangent) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Tangent.ToVariableName(InterpolatorType.ViewDirection)), false);
if ((requirements.requiresPosition & NeededCoordinateSpace.Object) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Object.ToVariableName(InterpolatorType.Position)), false);
if ((requirements.requiresPosition & NeededCoordinateSpace.View) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.View.ToVariableName(InterpolatorType.Position)), false);
if ((requirements.requiresPosition & NeededCoordinateSpace.World) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.World.ToVariableName(InterpolatorType.Position)), false);
if ((requirements.requiresPosition & NeededCoordinateSpace.Tangent) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", CoordinateSpace.Tangent.ToVariableName(InterpolatorType.Position)), false);
foreach (CoordinateSpace space in Enum.GetValues(typeof(CoordinateSpace)))
{
var neededCoordinateSpace = space.ToNeededCoordinateSpace();
if ((requirements.requiresNormal & neededCoordinateSpace) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", space.ToVariableName(InterpolatorType.Normal)), false);
if ((requirements.requiresTangent & neededCoordinateSpace) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", space.ToVariableName(InterpolatorType.Tangent)), false);
if ((requirements.requiresBitangent & neededCoordinateSpace) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", space.ToVariableName(InterpolatorType.BiTangent)), false);
if ((requirements.requiresViewDir & neededCoordinateSpace) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", space.ToVariableName(InterpolatorType.ViewDirection)), false);
if ((requirements.requiresPosition & neededCoordinateSpace) > 0)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float3 {0} = IN.{0};", space.ToVariableName(InterpolatorType.Position)), false);
}
if (requirements.requiresScreenPosition)
surfaceDescriptionFunction.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.ScreenPosition), false);

graph.CollectShaderProperties(shaderProperties, mode);
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, node);
foreach (var activeNode in activeNodeList.OfType<AbstractMaterialNode>())
{
if (activeNode is IGeneratesFunction)

surfaceDescriptionFunction.AddShaderChunk("return surface;", false);
surfaceDescriptionFunction.Deindent();
surfaceDescriptionFunction.AddShaderChunk("}", false);
ListPool<INode>.Release(activeNodeList);
public string GetShader(AbstractMaterialNode node, GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures, out PreviewMode previewMode)
static void Visit(List<INode> outputList, Dictionary<Guid, INode> unmarkedNodes, INode node)
{
if (!unmarkedNodes.ContainsKey(node.guid))
return;
foreach (var slot in node.GetInputSlots<ISlot>())
{
foreach (var edge in node.owner.GetEdges(slot.slotReference))
{
var inputNode = node.owner.GetNodeFromGuid(edge.outputSlot.nodeGuid);
Visit(outputList, unmarkedNodes, inputNode);
}
}
unmarkedNodes.Remove(node.guid);
outputList.Add(node);
}
public string GetShader(AbstractMaterialNode node, GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures, out PreviewMode previewMode, bool isUber = false)
{
if (node == null)
throw new ArgumentNullException("node");

surfaceInputs.AddShaderChunk("struct SurfaceInputs{", false);
surfaceInputs.Indent();
var requirements = GetRequierments(node);
var activeNodeList = ListPool<INode>.Get();
if (isUber)
{
var unmarkedNodes = node.owner.GetNodes<INode>().ToDictionary(x => x.guid);
while (unmarkedNodes.Any())
{
var unmarkedNode = unmarkedNodes.FirstOrDefault();
Visit(activeNodeList, unmarkedNodes, unmarkedNode.Value);
}
}
else
{
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, node);
}
var requirements = GetRequirements(activeNodeList);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresNormal, InterpolatorType.Normal, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresTangent, InterpolatorType.Tangent, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresBitangent, InterpolatorType.BiTangent, surfaceInputs);

if (requirements.requiresScreenPosition)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.ScreenPosition), false);
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, node);
previewMode = PreviewMode.Preview2D;
foreach (var pNode in activeNodeList.OfType<AbstractMaterialNode>())
previewMode = PreviewMode.Preview3D;
if (!isUber)
if (pNode.previewMode == PreviewMode.Preview3D)
foreach (var pNode in activeNodeList.OfType<AbstractMaterialNode>())
previewMode = PreviewMode.Preview3D;
break;
if (pNode.previewMode == PreviewMode.Preview3D)
{
previewMode = PreviewMode.Preview3D;
break;
}
ListPool<INode>.Release(activeNodeList);
foreach (var channel in requirements.requiresMeshUVs.Distinct())
surfaceInputs.AddShaderChunk(string.Format("half4 {0};", channel.GetUVName()), false);

vertexShader.Deindent();
vertexShader.AddShaderChunk("}", false);
GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, node, node is IMasterNode);
var slots = new List<MaterialSlot>();
foreach (var activeNode in isUber ? activeNodeList.Where(n => ((AbstractMaterialNode)n).hasPreview) : ((INode)node).ToEnumerable())
{
if (activeNode is IMasterNode)
slots.AddRange(activeNode.GetInputSlots<MaterialSlot>());
else
slots.AddRange(activeNode.GetOutputSlots<MaterialSlot>());
}
GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, slots);
activeNodeList,
node,
surfaceDescriptionFunction,
shaderFunctionVisitor,

node is IMasterNode);
ListPool<INode>.Release(activeNodeList);
var finalShader = new ShaderGenerator();
finalShader.AddShaderChunk(string.Format(@"Shader ""{0}""", name), false);

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


[SerializeField]
private Shader m_Shader;
public Layer()
{}
public Layer() { }
public Guid guid
{

if (outputNode != null)
outputNode.onModified(outputNode, ModificationScope.Graph);
}
}
public override void OnBeforeSerialize()

public string GetShader(string name, GenerationMode mode, out List<PropertyCollector.TextureInfo> configuredTextures)
{
if (outputNode == null)
if (outputNode == null)
throw new InvalidOperationException();
var layerMap = new Dictionary<Guid, MaterialGraph>();

surfaceInputs.AddShaderChunk("struct SurfaceInputs{", false);
surfaceInputs.Indent();
var requirements = ShaderGraphRequirements.none;
foreach (var layer in layerMap)
requirements = requirements.Union(GetRequierments(layer.Value.masterNode as AbstractMaterialNode));
using (var activeNodesDisposable = ListPool<INode>.GetDisposable())
{
var activeNodes = activeNodesDisposable.value;
foreach (var layer in layerMap)
{
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, layer.Value.masterNode as AbstractMaterialNode);
}
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, outputNode);
var requirements = GetRequirements(activeNodes);
requirements = requirements.Union(GetRequierments(outputNode));
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);
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.requiresVertexColor)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.VertexColor), false);
if (requirements.requiresScreenPosition)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.ScreenPosition), 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);
foreach (var channel in requirements.requiresMeshUVs.Distinct())
surfaceInputs.AddShaderChunk(string.Format("half4 {0};", channel.GetUVName()), false);
vertexShader.AddShaderChunk("GraphVertexInput PopulateVertexData(GraphVertexInput v){", false);
vertexShader.Indent();
vertexShader.AddShaderChunk("return v;", false);
vertexShader.Deindent();
vertexShader.AddShaderChunk("}", false);
surfaceInputs.Deindent();
surfaceInputs.AddShaderChunk("};", false);
var shaderProperties = new PropertyCollector();
var baseGraph = layerMap.Values.FirstOrDefault();
if (baseGraph == null)
{
configuredTextures = new List<PropertyCollector.TextureInfo>();
return string.Empty;
}
var masterNode = baseGraph.masterNode;
GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, ((AbstractMaterialNode) masterNode).GetInputSlots<MaterialSlot>().ToList());
vertexShader.AddShaderChunk("GraphVertexInput PopulateVertexData(GraphVertexInput v){", false);
vertexShader.Indent();
vertexShader.AddShaderChunk("return v;", false);
vertexShader.Deindent();
vertexShader.AddShaderChunk("}", false);
foreach (var layer in layerMap)
{
activeNodes.Clear();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, layer.Value.masterNode as AbstractMaterialNode);
GenerateSurfaceDescription(
activeNodes,
layer.Value.masterNode as AbstractMaterialNode,
surfaceDescriptionFunction,
shaderFunctionVisitor,
shaderProperties,
requirements,
mode,
true,
LayerToFunctionName(layer.Key));
}
var shaderProperties = new PropertyCollector();
surfaceDescriptionStruct.AddShaderChunk("struct WeightsSurfaceDescription{", false);
surfaceDescriptionStruct.Indent();
var baseGraph = layerMap.Values.FirstOrDefault();
if (baseGraph == null)
{
configuredTextures = new List<PropertyCollector.TextureInfo>();
return string.Empty;
}
foreach (var slot in outputNode.GetInputSlots<MaterialSlot>())
surfaceDescriptionStruct.AddShaderChunk(AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType) + " " + slot.shaderOutputName + ";", false);
var masterNode = baseGraph.masterNode;
GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, masterNode as AbstractMaterialNode, true);
surfaceDescriptionStruct.Deindent();
surfaceDescriptionStruct.AddShaderChunk("};", false);
foreach (var layer in layerMap)
{
activeNodes.Clear();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, outputNode);
layer.Value.masterNode as AbstractMaterialNode,
activeNodes,
outputNode,
surfaceDescriptionFunction,
shaderFunctionVisitor,
shaderProperties,

LayerToFunctionName(layer.Key));
}
surfaceDescriptionStruct.AddShaderChunk("struct WeightsSurfaceDescription{", false);
surfaceDescriptionStruct.Indent();
foreach (var slot in outputNode.GetInputSlots<MaterialSlot>())
surfaceDescriptionStruct.AddShaderChunk(AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType) + " " + slot.shaderOutputName + ";", false);
surfaceDescriptionStruct.Deindent();
surfaceDescriptionStruct.AddShaderChunk("};", false);
GenerateSurfaceDescription(
outputNode,
surfaceDescriptionFunction,
shaderFunctionVisitor,
shaderProperties,
requirements,
mode,
true,
"PopulateWeightsGraph",
"WeightsSurfaceDescription");
"PopulateWeightsGraph",
"WeightsSurfaceDescription");
string functionName = "PopulateSurfaceData";
string surfaceDescriptionName = "SurfaceDescription";
layerShaders.AddShaderChunk(string.Format("{0} {1}(SurfaceInputs IN) {{", surfaceDescriptionName, functionName), false);
layerShaders.Indent();
string functionName = "PopulateSurfaceData";
string surfaceDescriptionName = "SurfaceDescription";
layerShaders.AddShaderChunk(string.Format("{0} {1}(SurfaceInputs IN) {{", surfaceDescriptionName, functionName), false);
layerShaders.Indent();
layerShaders.AddShaderChunk("WeightsSurfaceDescription weights = PopulateWeightsGraph(IN);", false);
layerShaders.AddShaderChunk("SurfaceDescription result = (SurfaceDescription)0;", false);
layerShaders.AddShaderChunk("WeightsSurfaceDescription weights = PopulateWeightsGraph(IN);", false);
layerShaders.AddShaderChunk("SurfaceDescription result = (SurfaceDescription)0;", false);
foreach (var layer in layerMap)
{
layerShaders.AddShaderChunk(
string.Format(
"{0} {1} = {2}({3});",
surfaceDescriptionName,
LayerToFunctionName(layer.Key) + "_surface",
LayerToFunctionName(layer.Key),
"IN"), false);
foreach (var layer in layerMap)
{
layerShaders.AddShaderChunk(
string.Format(
"{0} {1} = {2}({3});",
surfaceDescriptionName,
LayerToFunctionName(layer.Key) + "_surface",
LayerToFunctionName(layer.Key),
"IN"), false);
layerShaders.AddShaderChunk(
string.Format("ScaleSurfaceDescription({0}_surface, weights.{0});", LayerToFunctionName(layer.Key)), false);
layerShaders.AddShaderChunk(
string.Format("ScaleSurfaceDescription({0}_surface, weights.{0});", LayerToFunctionName(layer.Key)), false);
layerShaders.AddShaderChunk(string.Format("AddSurfaceDescription(result, {0}_surface);", LayerToFunctionName(layer.Key)), false);
}
layerShaders.AddShaderChunk("return result;", false);
layerShaders.Deindent();
layerShaders.AddShaderChunk("}", false);
layerShaders.AddShaderChunk(string.Format("AddSurfaceDescription(result, {0}_surface);", LayerToFunctionName(layer.Key)), false);
}
layerShaders.AddShaderChunk("return result;", false);
var finalShader = new ShaderGenerator();
finalShader.AddShaderChunk(string.Format(@"Shader ""{0}""", name), false);
finalShader.AddShaderChunk("{", false);
finalShader.Indent();
layerShaders.Deindent();
layerShaders.AddShaderChunk("}", false);
finalShader.AddShaderChunk("Properties", false);
finalShader.AddShaderChunk("{", false);
finalShader.Indent();
finalShader.AddShaderChunk(shaderProperties.GetPropertiesBlock(2), false);
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);
var finalShader = new ShaderGenerator();
finalShader.AddShaderChunk(string.Format(@"Shader ""{0}""", name), 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);
finalShader.AddShaderChunk("CGINCLUDE", false);
finalShader.AddShaderChunk("#include \"UnityCG.cginc\"", false);
finalShader.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
finalShader.AddShaderChunk(graphVertexInput, false);
finalShader.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
finalShader.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);
finalShader.AddShaderChunk(shaderProperties.GetPropertiesDeclaration(2), false);
finalShader.AddShaderChunk(vertexShader.GetShaderString(2), false);
finalShader.AddShaderChunk(surfaceDescriptionFunction.GetShaderString(2), false);
finalShader.AddShaderChunk(layerShaders.GetShaderString(2), false);
finalShader.AddShaderChunk("ENDCG", false);
finalShader.AddShaderChunk("CGINCLUDE", false);
finalShader.AddShaderChunk("#include \"UnityCG.cginc\"", false);
finalShader.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
finalShader.AddShaderChunk(graphVertexInput, false);
finalShader.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
finalShader.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);
finalShader.AddShaderChunk(shaderProperties.GetPropertiesDeclaration(2), false);
finalShader.AddShaderChunk(vertexShader.GetShaderString(2), false);
finalShader.AddShaderChunk(surfaceDescriptionFunction.GetShaderString(2), false);
finalShader.AddShaderChunk(layerShaders.GetShaderString(2), false);
finalShader.AddShaderChunk("ENDCG", false);
if (masterNode != null)
{
var subShaders = masterNode.GetSubshader(requirements, null);
foreach (var ss in subShaders)
finalShader.AddShaderChunk(ss, false);
}
if (masterNode != null)
{
var subShaders = masterNode.GetSubshader(requirements, null);
foreach (var ss in subShaders)
finalShader.AddShaderChunk(ss, false);
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);
configuredTextures = shaderProperties.GetConfiguredTexutres();
return finalShader.GetShaderString(0);
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);
configuredTextures = shaderProperties.GetConfiguredTexutres();
return finalShader.GetShaderString(0);
}
public void LoadedFromDisk()

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/IControlAttribute.cs


using System.Reflection;
using UnityEngine.Experimental.UIElements;
using UnityEditor.ShaderGraph;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{

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


if (!m_Previews.TryGetValue(nodeGuid, out previewData))
return;
if (node is IMasterNode)
{
var materialGraph = (AbstractMaterialGraph) node.owner;
List<PropertyCollector.TextureInfo> defaultTextures;
PreviewMode pmode;
previewData.shaderString = materialGraph.GetShader(node, GenerationMode.Preview, node.guid + "_preview", out defaultTextures, out pmode);
previewData.previewMode = node.previewMode;
}
else if (!node.hasPreview || NodeUtils.FindEffectiveShaderStage(node, true) == ShaderStage.Vertex)
if (!(node is IMasterNode) && (!node.hasPreview || NodeUtils.FindEffectiveShaderStage(node, true) == ShaderStage.Vertex))
{
previewData.shaderString = null;
}

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


}
UpdatePortInputVisibilities();
foreach (var control in m_ControlViews)
{
var listener = control as INodeModificationListener;
if (listener != null)
listener.OnNodeModified(scope);
}
}
void AddSlots(IEnumerable<MaterialSlot> slots)

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/INodeModificationListener.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph.Drawing
{
public interface INodeModificationListener
{
void OnNodeModified(ModificationScope scope);
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/INodeModificationListener.cs.meta


fileFormatVersion: 2
guid: 22f268830a014848b3a8bfdd5142a2d0
timeCreated: 1510751718
正在加载...
取消
保存