浏览代码

first pass.

/main
Tim Cooper 7 年前
当前提交
0b56c45b
共有 24 个文件被更改,包括 165 次插入445 次删除
  1. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderSubGraph.cs
  2. 86
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs
  4. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractShaderProperty.cs
  5. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/ColorShaderProperty.cs
  6. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/FloatShaderProperty.cs
  7. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/IShaderProperty.cs
  8. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/SamplerStateShaderProperty.cs
  9. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/TextureShaderProperty.cs
  10. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector2ShaderProperty.cs
  11. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector3ShaderProperty.cs
  12. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector4ShaderProperty.cs
  13. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightMetallicMasterNode.cs
  14. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightSpecularMasterNode.cs
  15. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  16. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Geometry/ViewDirectionNode.cs
  17. 174
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MasterNode.cs
  18. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraph.cs
  19. 61
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs
  20. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs
  21. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/SubgraphInputNodeInspector.cs.meta
  22. 85
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/SubgraphInputNodeInspector.cs
  23. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphInputNode.cs.meta
  24. 80
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphInputNode.cs

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderSubGraph.cs


public override void Action(int instanceId, string pathName, string resourceFile)
{
var graph = new SubGraph();
graph.AddNode(new SubGraphInputNode());
graph.AddNode(new SubGraphOutputNode());
File.WriteAllText(pathName, EditorJsonUtility.ToJson(graph));
AssetDatabase.Refresh();

86
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs


return;
var graphPresenter = graphEditorView.presenter.graphPresenter;
var selected = graphPresenter.elements.Where(e => e.selected).ToArray();
var deserialized = MaterialGraphPresenter.DeserializeCopyBuffer(JsonUtility.ToJson(MaterialGraphPresenter.CreateCopyPasteGraph(selected)));
var selected = graphPresenter.elements.Where(e => e.selected);
var filtered = new List<GraphElementPresenter>();
foreach (var presenter in selected)
{
var nodePresenter = presenter as MaterialNodePresenter;
if (nodePresenter != null)
{
if (!(nodePresenter.node is PropertyNode))
filtered.Add(nodePresenter);
}
else
{
filtered.Add(presenter);
}
}
var deserialized = MaterialGraphPresenter.DeserializeCopyBuffer(JsonUtility.ToJson(MaterialGraphPresenter.CreateCopyPasteGraph(filtered)));
graph.AddNode(new SubGraphInputNode());
graph.AddNode(new SubGraphOutputNode());
var nodeGuidMap = new Dictionary<Guid, Guid>();

}
// remap outputs to the subgraph
var inputEdgeNeedsRemap = new List<IEdge>();
var outputEdgeNeedsRemap = new List<IEdge>();
var onlyInputInternallyConnected = new List<IEdge>();
var onlyOutputInternallyConnected = new List<IEdge>();
foreach (var edge in deserialized.edges)
{
var outputSlot = edge.outputSlot;

// one edge needs to go to outside world
else if (outputRemapExists)
{
inputEdgeNeedsRemap.Add(edge);
onlyOutputInternallyConnected.Add(edge);
outputEdgeNeedsRemap.Add(edge);
onlyInputInternallyConnected.Add(edge);
// we do a grouping here as the same output can
// point to multiple inputs
var uniqueOutputs = outputEdgeNeedsRemap.GroupBy(edge => edge.outputSlot);
var uniqueInputEdges = onlyOutputInternallyConnected.GroupBy(
edge => edge.outputSlot,
edge => edge,
(key, edges) => new {slotRef = key, edges = edges.ToList()});
foreach (var group in uniqueOutputs)
foreach (var group in uniqueInputEdges)
var inputNode = graph.inputNode;
var slotId = inputNode.AddSlot();
var inputNode = new PropertyNode();
var outputSlotRef = new SlotReference(inputNode.guid, slotId);
var sr = group.slotRef;
var fromNode = graphPresenter.graph.GetNodeFromGuid(sr.nodeGuid);
var fromSlot = fromNode.FindOutputSlot<MaterialSlot>(sr.slotId);
foreach (var edge in group)
switch (fromSlot.concreteValueType)
var newEdge = graph.Connect(outputSlotRef, new SlotReference(nodeGuidMap[edge.inputSlot.nodeGuid], edge.inputSlot.slotId));
inputsNeedingConnection.Add(new KeyValuePair<IEdge, IEdge>(edge, newEdge));
case ConcreteSlotValueType.SamplerState:
break;
case ConcreteSlotValueType.Matrix4:
break;
case ConcreteSlotValueType.Matrix3:
break;
case ConcreteSlotValueType.Matrix2:
break;
case ConcreteSlotValueType.Texture2D:
break;
case ConcreteSlotValueType.Vector4:
break;
case ConcreteSlotValueType.Vector3:
break;
case ConcreteSlotValueType.Vector2:
break;
case ConcreteSlotValueType.Vector1:
break;
case ConcreteSlotValueType.Error:
break;
default:
throw new ArgumentOutOfRangeException();
var uniqueInputs = inputEdgeNeedsRemap.GroupBy(edge => edge.inputSlot);
var uniqueOutputEdges = onlyInputInternallyConnected.GroupBy(
edge => edge.inputSlot,
edge => edge,
(key, edges) => new {slot = key, edges = edges.ToList()});
foreach (var group in uniqueInputs)
foreach (var group in uniqueOutputEdges)
{
var outputNode = graph.outputNode;
var slotId = outputNode.AddSlot();

foreach (var edge in group)
foreach (var edge in group.edges)
{
var newEdge = graph.Connect(new SlotReference(nodeGuidMap[edge.outputSlot.nodeGuid], edge.outputSlot.slotId), inputSlotRef);
outputsNeedingConnection.Add(new KeyValuePair<IEdge, IEdge>(edge, newEdge));

graphPresenter.AddNode(subGraphNode);
subGraphNode.subGraphAsset = subGraph;
foreach (var edgeMap in inputsNeedingConnection)
/* foreach (var edgeMap in inputsNeedingConnection)
}
}*/
foreach (var edgeMap in outputsNeedingConnection)
{

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs


}
}
public void CollectShaderProperties(PropertyCollector collector, GenerationMode generationMode)
public virtual void CollectShaderProperties(PropertyCollector collector, GenerationMode generationMode)
{
foreach (var prop in properties)
collector.AddShaderProperty(prop);

1
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractShaderProperty.cs


set { m_GeneratePropertyBlock = value; }
}
public abstract Vector4 defaultValue { get; }
public abstract string GetPropertyBlockString();
public abstract string GetPropertyDeclarationString();
public abstract PreviewProperty GetPreviewMaterialProperty();

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/ColorShaderProperty.cs


get { return PropertyType.Color; }
}
public override Vector4 defaultValue
{
get { return new Vector4(value.r, value.g, value.b, value.a); }
}
public override string GetPropertyBlockString()
{
if (!generatePropertyBlock)

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/FloatShaderProperty.cs


get { return PropertyType.Float; }
}
public override Vector4 defaultValue
{
get { return new Vector4(value, value, value, value);}
}
public override string GetPropertyBlockString()
{
var result = new StringBuilder();

1
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/IShaderProperty.cs


PropertyType propertyType { get; }
Guid guid { get; }
bool generatePropertyBlock { get; set; }
Vector4 defaultValue { get; }
string GetPropertyBlockString();
string GetPropertyDeclarationString();

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/SamplerStateShaderProperty.cs


get { return PropertyType.SamplerState; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override string GetPropertyBlockString()
{
return string.Empty;

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/TextureShaderProperty.cs


set { m_Modifiable = value; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override string GetPropertyBlockString()
{
var result = new StringBuilder();

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector2ShaderProperty.cs


get { return PropertyType.Vector2; }
}
public override Vector4 defaultValue
{
get { return new Vector4(value.x, value.y, 0, 0); }
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector3ShaderProperty.cs


get { return PropertyType.Vector3; }
}
public override Vector4 defaultValue
{
get { return new Vector4(value.x, value.y, value.z, 0); }
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector4ShaderProperty.cs


get { return PropertyType.Vector4; }
}
public override Vector4 defaultValue
{
get { return value; }
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()

3
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightMetallicMasterNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph

{
public const string MetallicSlotName = "Metallic";
public const int MetallicSlotId = 2;
public const string WorkflowName = "Metallic";
public LightweightMetallicMasterNode()
{

18
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightSpecularMasterNode.cs


using System;
using System.Collections.Generic;
/* [Serializable]
[Serializable]
[Title("Master/Lightweight/PBR Specular")]
public class LightweightSpecularMasterNode : AbstractLightweightPBRMasterNode
{

VertexOffsetId
});
}
protected override string GetTemplateName()
{
return "lightweightSubshaderPBR.template";
}
protected override int[] surfaceInputs
protected override IEnumerable<int> masterSurfaceInputs
{
get
{

}
}
protected override int[] vertexInputs
protected override IEnumerable<int> masterVertexInputs
{
get
{

};
}
}
public override string GetWorkflowName()
{
return WorkflowName;
}
}*/
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs


get { return true; }
}
protected virtual bool generateDefaultInputs
{
get { return true; }
}
public override bool hasError
{
get { return m_HasError; }

public virtual void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
if (!generateDefaultInputs)
return;
if (!generationMode.IsPreview())
return;

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Geometry/ViewDirectionNode.cs


public NeededCoordinateSpace RequiresViewDirection()
{
return NeededCoordinateSpace.Object;
return NeededCoordinateSpace.World;
}
}
}

174
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MasterNode.cs


UpdateNodeAfterDeserialization();
}
public override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(0, "Test", "Test", SlotType.Input, SlotValueType.Vector4, Vector4.one));
RemoveSlotsNameNotMatching(new[] { 0 });
}
protected override bool generateDefaultInputs { get { return false; } }
public override IEnumerable<ISlot> GetInputsWithNoConnection()
{
return new List<ISlot>();

}
public abstract string GetSubShader(ShaderGraphRequirements shaderGraphRequirements);
/*{
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 activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
var interpolators = new ShaderGenerator();
var vertexShader = new ShaderGenerator();
var pixelShader = new ShaderGenerator();
// bitangent needs normal for x product
if (shaderGraphRequirements.requiresNormal > 0 || shaderGraphRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : NORMAL;", ShaderGeneratorNames.ObjectSpaceNormal), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.normal;", ShaderGeneratorNames.ObjectSpaceNormal), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", ShaderGeneratorNames.ObjectSpaceNormal), false);
}
if (shaderGraphRequirements.requiresTangent > 0 || shaderGraphRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TANGENT;", ShaderGeneratorNames.ObjectSpaceTangent), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.tangent;", ShaderGeneratorNames.ObjectSpaceTangent), false);
pixelShader.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpaceTangent), false);
pixelShader.AddShaderChunk(string.Format("float4 {0} = normalize(cross(normalize(IN.{1}), normalize(IN.{2}.xyz)) * IN.{2}.w);",
ShaderGeneratorNames.ObjectSpaceBiTangent,
ShaderGeneratorNames.ObjectSpaceTangent,
ShaderGeneratorNames.ObjectSpaceNormal), false);
}
int interpolatorIndex = 0;
if (shaderGraphRequirements.requiresViewDir > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ObjectSpaceViewDirection, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = ObjSpaceViewDir(v.vertex);", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
pixelShader.AddShaderChunk(string.Format("float4 {0} = normalize(IN.{0});", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
interpolatorIndex++;
}
if (shaderGraphRequirements.requiresPosition > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ObjectSpacePosition, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.vertex;", ShaderGeneratorNames.ObjectSpacePosition), false);
pixelShader.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpacePosition), false);
interpolatorIndex++;
}
if (shaderGraphRequirements.NeedsTangentSpace())
{
pixelShader.AddShaderChunk(string.Format("float3x3 tangentSpaceTransform = float3x3({0},{1},{2});",
ShaderGeneratorNames.ObjectSpaceTangent, ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ObjectSpaceNormal), false);
}
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresNormal, pixelShader,
ShaderGeneratorNames.ObjectSpaceNormal, ShaderGeneratorNames.ViewSpaceNormal,
ShaderGeneratorNames.WorldSpaceNormal, ShaderGeneratorNames.TangentSpaceNormal);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresTangent, pixelShader,
ShaderGeneratorNames.ObjectSpaceTangent, ShaderGeneratorNames.ViewSpaceTangent,
ShaderGeneratorNames.WorldSpaceTangent, ShaderGeneratorNames.TangentSpaceTangent);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresBitangent, pixelShader,
ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ViewSpaceBiTangent,
ShaderGeneratorNames.WorldSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresViewDir, pixelShader,
ShaderGeneratorNames.ObjectSpaceViewDirection, ShaderGeneratorNames.ViewSpaceViewDirection,
ShaderGeneratorNames.WorldSpaceViewDirection, ShaderGeneratorNames.TangentSpaceViewDirection);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresPosition, pixelShader,
ShaderGeneratorNames.ObjectSpacePosition, ShaderGeneratorNames.ViewSpacePosition,
ShaderGeneratorNames.WorldSpacePosition, ShaderGeneratorNames.TangentSpacePosition);
if (shaderGraphRequirements.requiresVertexColor)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : COLOR;", ShaderGeneratorNames.VertexColor), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = color", ShaderGeneratorNames.VertexColor), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", ShaderGeneratorNames.VertexColor), false);
}
if (shaderGraphRequirements.requiresScreenPosition)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ScreenPosition, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = ComputeScreenPos(UnityObjectToClipPos(v.vertex)", ShaderGeneratorNames.ScreenPosition), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", ShaderGeneratorNames.ScreenPosition), false);
interpolatorIndex++;
}
foreach (var channel in shaderGraphRequirements.requiresMeshUVs.Distinct())
{
interpolators.AddShaderChunk(string.Format("half4 {0} : TEXCOORD{1};", channel.GetUVName(), interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.texcoord{1};", channel.GetUVName(), interpolatorIndex == 0 ? "" : interpolatorIndex.ToString()), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", channel.GetUVName()), false);
interpolatorIndex++;
}
var outputs = new ShaderGenerator();
var slot = FindSlot<MaterialSlot>(0);
var result = string.Format("surf.{0}", slot.shaderOutputName);
outputs.AddShaderChunk(string.Format("return {0};", ShaderGenerator.AdaptNodeOutputForPreview(this, 0, result)), true);
var resultShader = subShaderTemplate.Replace("{0}", interpolators.GetShaderString(0));
resultShader = resultShader.Replace("{1}", vertexShader.GetShaderString(0));
resultShader = resultShader.Replace("{2}", pixelShader.GetShaderString(0));
resultShader = resultShader.Replace("{3}", outputs.GetShaderString(0));
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 resultShader;
}
private const string subShaderTemplate = @"
SubShader
{
Tags { ""RenderType""=""Opaque"" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include ""UnityCG.cginc""
struct GraphVertexOutput
{
float4 position : POSITION;
{0}
};
GraphVertexOutput vert (GraphVertexInput v)
{
v = PopulateVertexData(v);
GraphVertexOutput o;
o.position = UnityObjectToClipPos(v.vertex);
{1}
return o;
}
fixed4 frag (GraphVertexOutput IN) : SV_Target
{
SurfaceInputs surfaceInput;
{2}
SurfaceDescription surf = PopulateSurfaceData(surfaceInput);
{3}
}
ENDCG
}
}";*/
}
}

25
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraph.cs


public class SubGraph : AbstractMaterialGraph
, IGeneratesBodyCode
, IGeneratesFunction
, IGenerateProperties
[NonSerialized]
private SubGraphInputNode m_InputNode;
public SubGraphInputNode inputNode
{
get
{
// find existing node
if (m_InputNode == null)
m_InputNode = GetNodes<SubGraphInputNode>().FirstOrDefault();
return m_InputNode;
}
}
public SubGraphOutputNode outputNode
{
get

public override void OnAfterDeserialize()
{
base.OnAfterDeserialize();
m_InputNode = null;
if (inputNode != null && node is SubGraphInputNode)
{
Debug.LogWarning("Attempting to add second SubGraphInputNode to SubGraph. This is not allowed.");
return;
}
if (outputNode != null && node is SubGraphOutputNode)
{
Debug.LogWarning("Attempting to add second SubGraphOutputNode to SubGraph. This is not allowed.");

}
}
public void CollectShaderProperties(PropertyCollector visitor, GenerationMode generationMode)
public override void CollectShaderProperties(PropertyCollector visitor, GenerationMode generationMode)
{
foreach (var node in usedNodes)
{

61
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs


return;
}
var subGraphInputNode = subGraphAsset.subGraph.inputNode;
foreach (var slot in subGraphInputNode.GetOutputSlots<MaterialSlot>())
var props = subGraph.properties;
foreach (var prop in props)
AddSlot(new MaterialSlot(slot.id, slot.displayName, slot.shaderOutputName, SlotType.Input, slot.valueType, slot.defaultValue));
validNames.Add(slot.id);
var propType = prop.propertyType;
SlotValueType slotType;
switch (propType)
{
case PropertyType.Color:
slotType = SlotValueType.Vector4;
break;
case PropertyType.Texture:
slotType = SlotValueType.Texture2D;
break;
case PropertyType.Float:
slotType = SlotValueType.Vector1;
break;
case PropertyType.Vector2:
slotType = SlotValueType.Vector2;
break;
case PropertyType.Vector3:
slotType = SlotValueType.Vector3;
break;
case PropertyType.Vector4:
slotType = SlotValueType.Vector4;
break;
case PropertyType.Matrix2:
slotType = SlotValueType.Matrix2;
break;
case PropertyType.Matrix3:
slotType = SlotValueType.Matrix3;
break;
case PropertyType.Matrix4:
slotType = SlotValueType.Matrix4;
break;
default:
throw new ArgumentOutOfRangeException();
}
var id = prop.guid.GetHashCode();
AddSlot(new MaterialSlot(id, prop.name, prop.name, SlotType.Input, slotType, prop.defaultValue));
validNames.Add(id);
}
var subGraphOutputNode = subGraphAsset.subGraph.outputNode;

// Step 3...
// For each input that is used and connects through we want to generate code.
// First we assign the input variables to the subgraph
var subGraphInputNode = subGraphAsset.subGraph.inputNode;
var subGraphInputs = subGraphAsset.subGraph.properties;
foreach (var slot in GetInputSlots<MaterialSlot>())
var propertyGen = new PropertyCollector();
subGraphAsset.subGraph.CollectShaderProperties(propertyGen, GenerationMode.ForReals);
foreach (var prop in subGraphInputs)
var varName = subGraphInputNode.GetVariableNameForSlot(slot.id);
var varValue = GetSlotValue(slot.id, generationMode);
var varName = prop.name;
var slotId = prop.guid.GetHashCode();
var slot = FindInputSlot<MaterialSlot>(slotId);
var varValue = GetSlotValue(slotId, generationMode);
var outDimension = ConvertConcreteSlotValueTypeToString(slot.concreteValueType);
"float"
+ outDimension
ConvertConcreteSlotValueTypeToString(precision, slot.concreteValueType)
+ " "
+ varName
+ " = "

4
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs


int interpolatorIndex = 0;
if (shaderGraphRequirements.requiresViewDir > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ObjectSpaceViewDirection, interpolatorIndex), false);
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", ShaderGeneratorNames.ObjectSpaceViewDirection, interpolatorIndex), false);
pixelShader.AddShaderChunk(string.Format("float4 {0} = normalize(IN.{0});", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
interpolatorIndex++;
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/SubgraphInputNodeInspector.cs.meta


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

85
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/SubgraphInputNodeInspector.cs


using System.Linq;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Drawing
{
public class SubgraphInputNodeInspector : BasicNodeInspector
{
protected enum UISlotValueType
{
Vector4,
Vector3,
Vector2,
Vector1
}
private static UISlotValueType ToUISlot(SlotValueType slot)
{
switch (slot)
{
case SlotValueType.Vector1:
return UISlotValueType.Vector1;
case SlotValueType.Vector2:
return UISlotValueType.Vector2;
case SlotValueType.Vector3:
return UISlotValueType.Vector3;
case SlotValueType.Vector4:
return UISlotValueType.Vector4;
}
return UISlotValueType.Vector4;
}
private static SlotValueType ToSlotValueType(UISlotValueType slot)
{
switch (slot)
{
case UISlotValueType.Vector1:
return SlotValueType.Vector1;
case UISlotValueType.Vector2:
return SlotValueType.Vector2;
case UISlotValueType.Vector3:
return SlotValueType.Vector3;
case UISlotValueType.Vector4:
return SlotValueType.Vector4;
}
return SlotValueType.Vector4;
}
protected override ModificationScope DoSlotsUI()
{
var slots = node.GetSlots<MaterialSlot>();
if (!slots.Any())
return ModificationScope.Node;
GUILayout.Label("Default Slot Values", EditorStyles.boldLabel);
bool valueChanged = false;
bool typeChanged = false;
foreach (var slot in node.GetSlots<MaterialSlot>())
{
EditorGUI.BeginChangeCheck();
GUILayout.Label(slot.displayName);
slot.currentValue = EditorGUILayout.Vector4Field("", slot.currentValue);
if (EditorGUI.EndChangeCheck())
valueChanged |= true;
EditorGUI.BeginChangeCheck();
var result = (UISlotValueType)EditorGUILayout.EnumPopup(ToUISlot(slot.valueType));
slot.valueType = ToSlotValueType(result);
if (EditorGUI.EndChangeCheck())
typeChanged |= true;
}
GUILayout.Space(10);
if (typeChanged)
return ModificationScope.Topological;
if (valueChanged)
return ModificationScope.Node;
return ModificationScope.Node;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphInputNode.cs.meta


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

80
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphInputNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
public class SubGraphInputNode : AbstractSubGraphIONode
{
public SubGraphInputNode()
{
name = "SubGraphInputs";
}
public override int AddSlot()
{
var nextSlotId = GetOutputSlots<ISlot>().Count() + 1;
AddSlot(new MaterialSlot(-nextSlotId, "Input " + nextSlotId, "Input" + nextSlotId, SlotType.Output, SlotValueType.Vector4, Vector4.zero));
return -nextSlotId;
}
public override void RemoveSlot()
{
var lastSlotId = GetOutputSlots<ISlot>().Count();
if (lastSlotId == 0)
return;
RemoveSlot(-lastSlotId);
}
public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
foreach (var slot in GetOutputSlots<MaterialSlot>())
{
IShaderProperty property;
switch (slot.concreteValueType)
{
case ConcreteSlotValueType.Vector4:
property = new Vector4ShaderProperty();
break;
case ConcreteSlotValueType.Vector3:
property = new Vector3ShaderProperty();
break;
case ConcreteSlotValueType.Vector2:
property = new Vector2ShaderProperty();
break;
case ConcreteSlotValueType.Vector1:
property = new FloatShaderProperty();
break;
default:
throw new ArgumentOutOfRangeException();
}
property.generatePropertyBlock = false;
properties.AddShaderProperty(property);
}
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
base.CollectPreviewMaterialProperties(properties);
foreach (var s in GetOutputSlots<MaterialSlot>())
{
properties.Add
(
new PreviewProperty
{
m_Name = GetVariableNameForSlot(s.id),
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(s.concreteValueType),
m_Vector4 = s.currentValue,
m_Float = s.currentValue.x,
m_Color = s.currentValue
}
);
}
}
}
}
正在加载...
取消
保存