浏览代码

Change IGeneratesFunction such that a name is provided to a registry, which only allows 1 implementation per name. Includes a validation mode that checks that multiple given functions with the same name are actually the same.

/main
Peter Bay Bastian 7 年前
当前提交
41e06942
共有 20 个文件被更改,包括 249 次插入129 次删除
  1. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IGeneratesFunction.cs
  2. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs
  3. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs
  4. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Adjustment/ChannelMixerNode.cs
  5. 51
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Mask/ChannelMaskNode.cs
  6. 28
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Normal/NormalCreateNode.cs
  7. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/FlipNode.cs
  8. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  9. 72
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Scene/FogNode.cs
  10. 19
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Matrix/MatrixMultiplyByVectorNode.cs
  11. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Matrix/MatrixMultiplyNode.cs
  12. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Procedural/Noise/SimpleNoiseNode.cs
  13. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Procedural/Noise/VoronoiNode.cs
  14. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraph.cs
  15. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs
  16. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs
  17. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGenerator.cs
  18. 36
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderStringBuilder.cs
  19. 47
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/FunctionRegistry.cs
  20. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/FunctionRegistry.cs.meta

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/IGeneratesFunction.cs


{
public interface IGeneratesFunction
{
void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode);
void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode);
}
}

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


var surfaceVertexShader = new ShaderGenerator();
var surfaceDescriptionFunction = new ShaderGenerator();
var surfaceDescriptionStruct = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var functionRegistry = new FunctionRegistry(2);
var surfaceInputs = new ShaderGenerator();
var shaderProperties = new PropertyCollector();

masterNode,
masterNode.owner as AbstractMaterialGraph,
surfaceDescriptionFunction,
shaderFunctionVisitor,
functionRegistry,
shaderProperties,
requirements,
mode,

usedSlots);
var graph = new ShaderGenerator();
graph.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
graph.AddShaderChunk(functionRegistry.ToString(), false);
graph.AddShaderChunk(vertexInputs.GetShaderString(2), false);
graph.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
graph.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);

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


var surfaceVertexShader = new ShaderGenerator();
var surfaceDescriptionFunction = new ShaderGenerator();
var surfaceDescriptionStruct = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var functionRegistry = new FunctionRegistry(2);
var surfaceInputs = new ShaderGenerator();
var shaderProperties = new PropertyCollector();

masterNode,
masterNode.owner as AbstractMaterialGraph,
surfaceDescriptionFunction,
shaderFunctionVisitor,
functionRegistry,
shaderProperties,
requirements,
mode,

usedSlots);
var graph = new ShaderGenerator();
graph.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
graph.AddShaderChunk(functionRegistry.ToString(), false);
graph.AddShaderChunk(vertexInputs.GetShaderString(2), false);
graph.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
graph.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);

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


});
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
var sb = new ShaderStringBuilder();
sb.AppendLine("void {0} ({1} In, {2}3 Red, {2}3 Green, {2}3 Blue, out {3} Out)",
GetFunctionName(),
FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType.ToString(precision),
precision,
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision));
using (sb.BlockScope())
registry.ProvideFunction(GetFunctionName(), s =>
sb.AppendLine("Out = {0}(dot(In, Red), dot(In, Green), dot(In, Blue));",
s.AppendLine("void {0} ({1} In, {2}3 Red, {2}3 Green, {2}3 Blue, out {3} Out)",
GetFunctionName(),
FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType.ToString(precision),
precision,
}
visitor.AddShaderChunk(sb.ToString(), true);
using (s.BlockScope())
{
s.AppendLine("Out = {0}(dot(In, Red), dot(In, Green), dot(In, Blue));",
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision));
}
});
}
}
}

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


using System.Reflection;
using System;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;

return GetFunctionName() + " (" + inputValue + ", " + outputValue + ");";
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
ValidateChannelCount();
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("In", "Out"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
switch (channel)
registry.ProvideFunction(GetFunctionName(), s =>
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);
s.AppendLine(GetFunctionPrototype("In", "Out"));
using (s.BlockScope())
{
switch (channel)
{
case TextureChannel.Green:
s.AppendLine("Out = In.yyyy;");
break;
case TextureChannel.Blue:
s.AppendLine("Out = In.zzzz;");
break;
case TextureChannel.Alpha:
s.AppendLine("Out = In.wwww;");
break;
case TextureChannel.Red:
s.AppendLine("Out = In.xxxx;");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
});
}
}
}

28
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Normal/NormalCreateNode.cs


visitor.AddShaderChunk(sb.ToString(), true);
}
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
registry.ProvideFunction(GetFunctionName(), s =>
{
s.AppendLine("void {0}({1} Texture, {2} Sampler, {3} UV, {4} Offset, {5} Strength, out {6} Out)", GetFunctionName(),
FindInputSlot<MaterialSlot>(TextureInputId).concreteValueType.ToString(precision),
FindInputSlot<MaterialSlot>(SamplerInputId).concreteValueType.ToString(precision),
FindInputSlot<MaterialSlot>(UVInputId).concreteValueType.ToString(precision),
FindInputSlot<MaterialSlot>(OffsetInputId).concreteValueType.ToString(precision),
FindInputSlot<MaterialSlot>(StrengthInputId).concreteValueType.ToString(precision),
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision));
using (s.BlockScope())
{
s.AppendLine("Offset = pow(Offset, 3) * 0.1;");
s.AppendLine("{0}2 offsetU = float2(UV.x + Offset, UV.y);", precision);
s.AppendLine("{0}2 offsetV = float2(UV.x, UV.y + Offset);", precision);
s.AppendLine("{0} normalSample = Texture.Sample(Sampler, UV);", precision);
s.AppendLine("{0} uSample = Texture.Sample(Sampler, offsetU);", precision);
s.AppendLine("{0} vSample = Texture.Sample(Sampler, offsetV);", precision);
s.AppendLine("{0}3 va = float3(1, 0, (uSample - normalSample) * Strength);", precision);
s.AppendLine("{0}3 vb = float3(0, 1, (vSample - normalSample) * Strength);", precision);
s.AppendLine("Out = normalize(cross(va, vb));");
}
});
}
public bool RequiresMeshUV(UVChannel channel)
{
foreach (var slot in this.GetInputSlots<MaterialSlot>().OfType<IMayRequireMeshUV>())

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


}
visitor.AddShaderChunk(sb.ToString(), true);
}
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
registry.ProvideFunction(GetFunctionName(), s =>
{
s.AppendLine("void {0}({1} In, {2} Flip, out {3} Out)",
GetFunctionName(),
FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType.ToString(precision),
FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType.ToString(precision),
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision));
using (s.BlockScope())
{
s.AppendLine("Out = abs(Flip - In);");
}
});
}
}
}

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


private string GetFunctionName()
{
var function = GetFunctionToConvert();
return function.Name + "_" + (function.IsStatic ? string.Empty : GuidEncoder.Encode(guid) + "_") + precision;
return function.Name + "_" + (function.IsStatic ? string.Empty : GuidEncoder.Encode(guid) + "_") + precision + (this.GetSlots<DynamicVectorMaterialSlot>().Select(s => GetSlotDimension(s.concreteValueType)).FirstOrDefault() ?? "");
}
private string GetFunctionHeader()

return result;
}
public virtual void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public virtual void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
string function = GetFunctionHeader() + GetFunctionBody(GetFunctionToConvert());
visitor.AddShaderChunk(function, true);
registry.ProvideFunction(GetFunctionName(), s =>
{
s.AppendLine(GetFunctionHeader());
s.AppendLines(GetFunctionBody(GetFunctionToConvert()).Trim('\r', '\n', '\t', ' '));
});
}
private static SlotAttribute GetSlotAttribute([NotNull] ParameterInfo info)

72
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Scene/FogNode.cs


visitor.AddShaderChunk(string.Format("{0}(IN.{1}, {2}, {3});", GetFunctionName(), CoordinateSpace.Object.ToVariableName(InterpolatorType.Position), colorValue, densityValue), false);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
var sg = new ShaderStringBuilder();
sg.AppendLine("void {0}({1}3 ObjectSpacePosition, out {2} Color, out {3} Density)",
GetFunctionName(),
precision,
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision),
FindOutputSlot<MaterialSlot>(OutputSlot1Id).concreteValueType.ToString(precision));
using (sg.BlockScope())
registry.ProvideFunction(GetFunctionName(), s =>
sg.AppendLine("Color = unity_FogColor;");
sg.AppendLine("{0} clipZ_01 = UNITY_Z_0_FAR_FROM_CLIPSPACE(UnityObjectToClipPos(ObjectSpacePosition).z);", precision);
sg.AppendLine("#if defined(FOG_LINEAR)");
using (sg.IndentScope())
{
sg.AppendLine("{0} fogFactor = saturate(clipZ_01 * unity_FogParams.z + unity_FogParams.w);", precision);
sg.AppendLine("Density = fogFactor;");
}
sg.AppendLine("#elif defined(FOG_EXP)");
using (sg.IndentScope())
{
sg.AppendLine("{0} fogFactor = unity_FogParams.y * clipZ_01;", precision);
sg.AppendLine("Density = saturate(exp2(-fogFactor));");
}
sg.AppendLine("#elif defined(FOG_EXP2)");
using (sg.IndentScope())
{
sg.AppendLine("{0} fogFactor = unity_FogParams.x * clipZ_01;", precision);
sg.AppendLine("Density = saturate(exp2(-fogFactor*fogFactor));");
}
sg.AppendLine("#else");
using (sg.IndentScope())
s.AppendLine("void {0}({1}3 ObjectSpacePosition, out {2} Color, out {3} Density)",
GetFunctionName(),
precision,
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision),
FindOutputSlot<MaterialSlot>(OutputSlot1Id).concreteValueType.ToString(precision));
using (s.BlockScope())
sg.AppendLine("Density = 0.0h;");
}
sg.AppendLine("#endif");
}
s.AppendLine("Color = unity_FogColor;");
visitor.AddShaderChunk(sg.ToString(), true);
s.AppendLine("{0} clipZ_01 = UNITY_Z_0_FAR_FROM_CLIPSPACE(UnityObjectToClipPos(ObjectSpacePosition).z);", precision);
s.AppendLine("#if defined(FOG_LINEAR)");
using (s.IndentScope())
{
s.AppendLine("{0} fogFactor = saturate(clipZ_01 * unity_FogParams.z + unity_FogParams.w);", precision);
s.AppendLine("Density = fogFactor;");
}
s.AppendLine("#elif defined(FOG_EXP)");
using (s.IndentScope())
{
s.AppendLine("{0} fogFactor = unity_FogParams.y * clipZ_01;", precision);
s.AppendLine("Density = saturate(exp2(-fogFactor));");
}
s.AppendLine("#elif defined(FOG_EXP2)");
using (s.IndentScope())
{
s.AppendLine("{0} fogFactor = unity_FogParams.x * clipZ_01;", precision);
s.AppendLine("Density = saturate(exp2(-fogFactor*fogFactor));");
}
s.AppendLine("#else");
using (s.IndentScope())
{
s.AppendLine("Density = 0.0h;");
}
s.AppendLine("#endif");
}
});
}
public NeededCoordinateSpace RequiresPosition()

19
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Matrix/MatrixMultiplyByVectorNode.cs


return string.Format("{0} ({1}, {2})", GetFunctionName(), input1Value, input2Value);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return mul(arg1, arg2);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
registry.ProvideFunction(GetFunctionName(), s =>
{
s.AppendLine(GetFunctionPrototype("arg1", "arg2"));
using (s.BlockScope())
{
s.AppendLine("return mul(arg1, arg2);");
}
});
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Matrix/MatrixMultiplyNode.cs


visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
registry.ProvideFunction(GetFunctionName(), s =>
{
s.AppendLine(GetFunctionPrototype("arg1", "arg2"));
using (s.BlockScope())
{
s.AppendLine("return mul(arg1, arg2);");
}
});
}
}
}

15
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Procedural/Noise/SimpleNoiseNode.cs


";
}
public override void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public override void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
string functionPreamble = @"
registry.ProvideFunction("unity_noise_randomValue", s => s.Append(@"
}
}"));
registry.ProvideFunction("unity_noise_interpolate", s => s.Append(@"
"));
registry.ProvideFunction("unity_valueNoise", s => s.Append(@"
inline float unity_valueNoise (float2 uv)
{
float2 i = floor(uv);

float topOfGrid = unity_noise_interpolate(r2, r3, f.x);
float t = unity_noise_interpolate(bottomOfGrid, topOfGrid, f.y);
return t;
}";
visitor.AddShaderChunk(functionPreamble, true);
base.GenerateNodeFunction(visitor, generationMode);
}"));
base.GenerateNodeFunction(registry, generationMode);
}
}
}

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Procedural/Noise/VoronoiNode.cs


";
}
public override void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public override void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
var preamble = @"
registry.ProvideFunction("unity_voronoi_noise_randomVector", s => s.Append(@"
inline float2 unity_voronoi_noise_randomVector (float2 UV, float offset)
{
float2x2 m = float2x2(15.27, 47.63, 99.41, 89.98);

";
visitor.AddShaderChunk(preamble, true);
base.GenerateNodeFunction(visitor, generationMode);
"));
base.GenerateNodeFunction(registry, generationMode);
}
}
}

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


}
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
(node as IGeneratesFunction).GenerateNodeFunction(visitor, generationMode);
(node as IGeneratesFunction).GenerateNodeFunction(registry, generationMode);
}
}

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


properties.AddRange(referencedGraph.GetPreviewProperties());
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
referencedGraph.GenerateNodeFunction(visitor, GenerationMode.ForReals);
referencedGraph.GenerateNodeFunction(registry, GenerationMode.ForReals);
}
public NeededCoordinateSpace RequiresNormal()

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


var vertexShader = new ShaderGenerator();
var surfaceDescriptionFunction = new ShaderGenerator();
var surfaceDescriptionStruct = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var functionRegistry = new FunctionRegistry(2);
var surfaceInputs = new ShaderGenerator();
surfaceInputs.AddShaderChunk("struct SurfaceInputs{", false);

node,
graph,
surfaceDescriptionFunction,
shaderFunctionVisitor,
functionRegistry,
shaderProperties,
requirements,
mode,

finalShader.AddShaderChunk("CGINCLUDE", false);
finalShader.AddShaderChunk("#include \"UnityCG.cginc\"", false);
finalShader.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
finalShader.AddShaderChunk(functionRegistry.ToString(), false);
finalShader.AddShaderChunk(vertexInputs.GetShaderString(2), false);
finalShader.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
finalShader.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);

AbstractMaterialNode masterNode,
AbstractMaterialGraph graph,
ShaderGenerator surfaceDescriptionFunction,
ShaderGenerator shaderFunctionVisitor,
FunctionRegistry functionRegistry,
PropertyCollector shaderProperties,
ShaderGraphRequirements requirements,
GenerationMode mode,

foreach (var activeNode in activeNodeList.OfType<AbstractMaterialNode>())
{
if (activeNode is IGeneratesFunction)
(activeNode as IGeneratesFunction).GenerateNodeFunction(shaderFunctionVisitor, mode);
(activeNode as IGeneratesFunction).GenerateNodeFunction(functionRegistry, mode);
if (activeNode is IGeneratesBodyCode)
(activeNode as IGeneratesBodyCode).GenerateNodeCode(surfaceDescriptionFunction, mode);
if (masterNode == null && activeNode.hasPreview)

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


var sb = new StringBuilder();
foreach (var shaderChunk in m_ShaderChunks)
{
var lines = shaderChunk.chunkString.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var lines = Regex.Split(shaderChunk.chunkString, Environment.NewLine);
for (int index = 0; index < lines.Length; index++)
{
var line = lines[index];

36
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderStringBuilder.cs


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
namespace UnityEditor.ShaderGraph

public void AppendNewLine()
{
m_StringBuilder.Append(Environment.NewLine);
m_StringBuilder.AppendLine();
AppendNewLine();
AppendNewLine();
AppendNewLine();
AppendNewLine();
}
public void AppendLines(string lines)
{
foreach (var line in Regex.Split(lines, Environment.NewLine))
AppendLine(line);
}
public void Append(string value)

public IDisposable IndentScope()
{
m_ScopeStack.Push(ScopeType.Indent);
m_IndentationLevel++;
IncreaseIndent();
return this;
}

m_IndentationLevel++;
IncreaseIndent();
public void IncreaseIndent()
{
m_IndentationLevel++;
}
public void DecreaseIndent()
{
m_IndentationLevel--;
}
m_IndentationLevel--;
DecreaseIndent();
m_IndentationLevel--;
DecreaseIndent();
AppendLine("}");
break;
}

{
return m_StringBuilder.ToString();
}
public string ToString(int startIndex, int length)
{
return m_StringBuilder.ToString(startIndex, length);
}
}
}

47
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/FunctionRegistry.cs


using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
public class FunctionRegistry
{
Dictionary<string, string> m_Functions = new Dictionary<string, string>();
ShaderStringBuilder m_StringBuilder = new ShaderStringBuilder();
bool m_ValidationEnabled = false;
public FunctionRegistry(int indentLevel = 0)
{
for (var i = 0; i < indentLevel; i++)
m_StringBuilder.IncreaseIndent();
}
public bool ProvideFunction(string name, Action<ShaderStringBuilder> generator)
{
string functionSource = string.Empty;
if (m_ValidationEnabled)
{
var ssb = new ShaderStringBuilder();
generator(ssb);
functionSource = ssb.ToString();
}
string existingFunctionSource;
if (m_Functions.TryGetValue(name, out existingFunctionSource))
{
if (m_ValidationEnabled && functionSource != existingFunctionSource)
Debug.LogErrorFormat(@"Function `{0}` has varying implementations:{1}{1}{2}{1}{1}{3}", name, Environment.NewLine, functionSource, existingFunctionSource);
return false;
}
generator(m_StringBuilder);
m_Functions.Add(name, functionSource);
m_StringBuilder.AppendNewLine();
return true;
}
public override string ToString()
{
return m_StringBuilder.ToString();
}
}
}

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


fileFormatVersion: 2
guid: 7e4fc6f4e30c408f9c0304cbed39a076
timeCreated: 1513609614
正在加载...
取消
保存