浏览代码

Merge master

/main
Jens Holm 7 年前
当前提交
e1ebec49
共有 17 个文件被更改,包括 351 次插入164 次删除
  1. 47
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialGraphAsset.cs
  2. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs
  3. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs
  4. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  5. 57
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/FunctionRegistry.cs
  6. 90
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs
  7. 88
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderStringBuilder.cs
  8. 65
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  9. 39
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs
  10. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  11. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs
  12. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss
  13. 2
      MaterialGraphProject/Assets/UnityShaderEditor/package.json
  14. 21
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GenerationResults.cs
  15. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GenerationResults.cs.meta
  16. 64
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderSourceMap.cs
  17. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderSourceMap.cs.meta

47
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialGraphAsset.cs


using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEditor.Graphing;
public class MaterialGraphAsset
static class MaterialGraphAsset
{
public static bool ShaderHasError(Shader shader)
{

}
public struct ShaderError
{
public string message;
public string messageDetails;
public string platform;
public string file;
public int line;
public int warning;
}
static MethodInfo s_GetErrorsCall = typeof(ShaderUtil).GetMethod("GetShaderErrors", BindingFlags.Static | BindingFlags.NonPublic);
static Type s_ShaderErrorType = typeof(ShaderUtil).Assembly.GetType("UnityEditor.ShaderError");
static FieldInfo s_ShaderErrorMessageField = s_ShaderErrorType.GetField("message", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorMessageDetailsField = s_ShaderErrorType.GetField("messageDetails", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorPlatformField = s_ShaderErrorType.GetField("platform", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorFileField = s_ShaderErrorType.GetField("file", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorLineField = s_ShaderErrorType.GetField("line", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorWarningField = s_ShaderErrorType.GetField("warning", BindingFlags.Instance | BindingFlags.Public);
public static ShaderError[] GetShaderErrors(Shader shader)
{
var invoke = s_GetErrorsCall.Invoke(null, new object[] { shader });
var objects = (Array)invoke;
var errors = new ShaderError[objects.Length];
for (var i = 0; i < objects.Length; i++)
{
var obj = objects.GetValue(i);
errors[i] = new ShaderError
{
message = (string)s_ShaderErrorMessageField.GetValue(obj),
messageDetails = (string)s_ShaderErrorMessageDetailsField.GetValue(obj),
platform = (string)s_ShaderErrorPlatformField.GetValue(obj),
file = (string)s_ShaderErrorFileField.GetValue(obj),
line = (int)s_ShaderErrorLineField.GetValue(obj),
warning = (int)s_ShaderErrorWarningField.GetValue(obj),
};
}
return errors;
}
}
}

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


private static string GetShaderPassFromTemplate(string template, PBRMasterNode masterNode, Pass pass, GenerationMode mode, SurfaceMaterialOptions materialOptions)
{
var builder = new ShaderStringBuilder();
builder.IncreaseIndent();
builder.IncreaseIndent();
var functionRegistry = new FunctionRegistry(2);
var functionRegistry = new FunctionRegistry(builder);
var surfaceInputs = new ShaderGenerator();
var shaderProperties = new PropertyCollector();

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

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


private static string GetShaderPassFromTemplate(string template, UnlitMasterNode masterNode, Pass pass, GenerationMode mode)
{
var builder = new ShaderStringBuilder();
builder.IncreaseIndent();
builder.IncreaseIndent();
var functionRegistry = new FunctionRegistry(2);
var functionRegistry = new FunctionRegistry(builder);
var surfaceInputs = new ShaderGenerator();
var shaderProperties = new PropertyCollector();

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

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


registry.ProvideFunction(GetFunctionName(), s =>
{
s.AppendLine(GetFunctionHeader());
s.AppendLines(GetFunctionBody(GetFunctionToConvert()).Trim('\r', '\n', '\t', ' '));
var functionBody = GetFunctionBody(GetFunctionToConvert());
var lines = functionBody.Trim('\r', '\n', '\t', ' ');
s.AppendLines(lines);
});
}

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


{
public class FunctionRegistry
{
Dictionary<string, string> m_Functions = new Dictionary<string, string>();
ShaderStringBuilder m_StringBuilder = new ShaderStringBuilder();
bool m_ValidationEnabled = false;
Dictionary<string, string> m_Sources = new Dictionary<string, string>();
bool m_Validate = false;
ShaderStringBuilder m_Builder;
public FunctionRegistry(ShaderStringBuilder builder)
{
m_Builder = builder;
}
public FunctionRegistry(int indentLevel = 0)
internal ShaderStringBuilder builder
for (var i = 0; i < indentLevel; i++)
m_StringBuilder.IncreaseIndent();
get { return m_Builder; }
public bool ProvideFunction(string name, Action<ShaderStringBuilder> generator)
public void ProvideFunction(string name, Action<ShaderStringBuilder> generator)
string functionSource = string.Empty;
if (m_ValidationEnabled)
string existingSource;
if (m_Sources.TryGetValue(name, out existingSource))
var ssb = new ShaderStringBuilder();
generator(ssb);
functionSource = ssb.ToString();
if (m_Validate)
{
var startIndex = builder.length;
generator(builder);
var length = builder.length - startIndex;
var source = builder.ToString(startIndex, length);
builder.length -= length;
if (source != existingSource)
Debug.LogErrorFormat(@"Function `{0}` has varying implementations:{1}{1}{2}{1}{1}{3}", name, Environment.NewLine, source, existingSource);
}
return;
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;
builder.AppendNewLine();
var startIndex = builder.length;
generator(builder);
var length = builder.length - startIndex;
var source = m_Validate ? builder.ToString(startIndex, length) : string.Empty;
m_Sources.Add(name, source);
generator(m_StringBuilder);
m_Functions.Add(name, functionSource);
m_StringBuilder.AppendNewLine();
return true;
}
public override string ToString()
{
return m_StringBuilder.ToString();
}
}
}

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


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

outputList.Add(node);
}
public static string GetShader(this AbstractMaterialGraph graph, AbstractMaterialNode node, GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures, out PreviewMode previewMode, out FloatShaderProperty outputIdProperty, Dictionary<Guid, int> ids = null)
public static GenerationResults GetShader(this AbstractMaterialGraph graph, AbstractMaterialNode node, GenerationMode mode, string name)
var results = new GenerationResults();
bool isUber = node == null;
var vertexInputs = new ShaderGenerator();

var functionRegistry = new FunctionRegistry(2);
var functionBuilder = new ShaderStringBuilder();
var functionRegistry = new FunctionRegistry(functionBuilder);
var surfaceInputs = new ShaderGenerator();
surfaceInputs.AddShaderChunk("struct SurfaceInputs{", false);

if (requirements.requiresScreenPosition)
surfaceInputs.AddShaderChunk(String.Format("float4 {0};", ShaderGeneratorNames.ScreenPosition), false);
previewMode = PreviewMode.Preview3D;
results.previewMode = PreviewMode.Preview3D;
if (!isUber)
{
foreach (var pNode in activeNodeList.OfType<AbstractMaterialNode>())

previewMode = PreviewMode.Preview3D;
results.previewMode = PreviewMode.Preview3D;
break;
}
}

GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, slots, !isUber);
var shaderProperties = new PropertyCollector();
outputIdProperty = new FloatShaderProperty
results.outputIdProperty = new FloatShaderProperty
{
displayName = "OutputId",
generatePropertyBlock = false,

shaderProperties.AddShaderProperty(outputIdProperty);
shaderProperties.AddShaderProperty(results.outputIdProperty);
GenerateSurfaceDescription(
activeNodeList,

shaderProperties,
requirements,
mode,
outputIdProperty: outputIdProperty,
ids: ids);
outputIdProperty: results.outputIdProperty,
ids: results.ids);
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(functionRegistry.ToString(), false);
finalShader.AddShaderChunk(vertexInputs.GetShaderString(2), 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("ENDCG", false);
var finalBuilder = new ShaderStringBuilder();
finalBuilder.AppendLine(@"Shader ""{0}""", name);
using (finalBuilder.BlockScope())
{
finalBuilder.AppendLine("Properties");
using (finalBuilder.BlockScope())
{
finalBuilder.AppendLines(shaderProperties.GetPropertiesBlock(0));
}
finalShader.AddShaderChunk(ShaderGenerator.GetPreviewSubShader(node, requirements), false);
finalBuilder.AppendLine(@"CGINCLUDE");
finalBuilder.AppendLine(@"#include ""UnityCG.cginc""");
finalBuilder.Concat(functionBuilder);
finalBuilder.AppendLines(vertexInputs.GetShaderString(0));
finalBuilder.AppendLines(surfaceInputs.GetShaderString(0));
finalBuilder.AppendLines(surfaceDescriptionStruct.GetShaderString(0));
finalBuilder.AppendLines(shaderProperties.GetPropertiesDeclaration(0));
finalBuilder.AppendLines(vertexShader.GetShaderString(0));
finalBuilder.AppendLines(surfaceDescriptionFunction.GetShaderString(0));
finalBuilder.AppendLine(@"ENDCG");
ListPool<INode>.Release(activeNodeList);
finalBuilder.AppendLines(ShaderGenerator.GetPreviewSubShader(node, requirements));
ListPool<INode>.Release(activeNodeList);
}
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);
configuredTextures = shaderProperties.GetConfiguredTexutres();
return finalShader.GetShaderString(0);
results.configuredTextures = shaderProperties.GetConfiguredTexutres();
ShaderSourceMap sourceMap;
results.shader = finalBuilder.ToString(out sourceMap);
results.sourceMap = sourceMap;
return results;
}
internal static void GenerateSurfaceDescriptionStruct(ShaderGenerator surfaceDescriptionStruct, List<MaterialSlot> slots, bool isMaster)

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

activeNode.CollectShaderProperties(shaderProperties, mode);
}
functionRegistry.builder.currentNode = null;
if (masterNode != null)
{

surfaceDescriptionFunction.AddShaderChunk("}", false);
}
public static string GetPreviewShader(this AbstractMaterialGraph graph, AbstractMaterialNode node, out PreviewMode previewMode)
public static GenerationResults GetPreviewShader(this AbstractMaterialGraph graph, AbstractMaterialNode node)
List<PropertyCollector.TextureInfo> configuredTextures;
FloatShaderProperty outputIdProperty;
return graph.GetShader(node, GenerationMode.Preview, String.Format("hidden/preview/{0}", node.GetVariableNameForNode()), out configuredTextures, out previewMode, out outputIdProperty);
return graph.GetShader(node, GenerationMode.Preview, String.Format("hidden/preview/{0}", node.GetVariableNameForNode()));
public static string GetUberPreviewShader(this AbstractMaterialGraph graph, Dictionary<Guid, int> ids, out FloatShaderProperty outputIdProperty)
public static GenerationResults GetUberPreviewShader(this AbstractMaterialGraph graph)
List<PropertyCollector.TextureInfo> configuredTextures;
PreviewMode previewMode;
return graph.GetShader(null, GenerationMode.Preview, "hidden/preview", out configuredTextures, out previewMode, out outputIdProperty, ids);
return graph.GetShader(null, GenerationMode.Preview, "hidden/preview");
}
static Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo> s_LegacyTypeRemapping;

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


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor.Graphing;
struct ShaderStringMapping
{
public INode node { get; set; }
public int startIndex { get; set; }
public int count { get; set; }
}
public class ShaderStringBuilder : IDisposable
{
enum ScopeType

StringBuilder m_StringBuilder;
Stack<ScopeType> m_ScopeStack;
int m_IndentationLevel;
ShaderStringMapping m_CurrentMapping;
List<ShaderStringMapping> m_Mappings;
internal INode currentNode
{
get { return m_CurrentMapping.node; }
set
{
m_CurrentMapping.count = m_StringBuilder.Length - m_CurrentMapping.startIndex;
if (m_CurrentMapping.count > 0)
m_Mappings.Add(m_CurrentMapping);
m_CurrentMapping.node = value;
m_CurrentMapping.startIndex = m_StringBuilder.Length;
m_CurrentMapping.count = 0;
}
}
m_Mappings = new List<ShaderStringMapping>();
m_CurrentMapping = new ShaderStringMapping();
}
public void AppendNewLine()

public void AppendLine(string value)
{
AppendIndentation();
m_StringBuilder.Append(value);
if (!string.IsNullOrEmpty(value))
{
AppendIndentation();
m_StringBuilder.Append(value);
}
AppendNewLine();
}

public void AppendLines(string lines)
{
foreach (var line in Regex.Split(lines, Environment.NewLine))
AppendLine(line);
if (string.IsNullOrEmpty(lines))
return;
var splitLines = lines.Split('\n');
var lineCount = splitLines.Length;
var lastLine = splitLines[lineCount - 1];
if (string.IsNullOrEmpty(lastLine) || lastLine == "\r")
lineCount--;
for (var i = 0; i < lineCount; i++)
AppendLine(splitLines[i].Trim('\r'));
}
public void Append(string value)

}
}
public void Concat(ShaderStringBuilder other)
{
// First re-add all the mappings from `other`, such that their mappings are transformed.
foreach (var mapping in other.m_Mappings)
{
currentNode = mapping.node;
// Use `AppendLines` to indent according to the current indentation.
AppendLines(other.ToString(mapping.startIndex, mapping.count));
}
currentNode = other.currentNode;
AppendLines(other.ToString(other.m_CurrentMapping.startIndex, other.length - other.m_CurrentMapping.startIndex));
}
}
public string ToString(out ShaderSourceMap sourceMap)
{
m_CurrentMapping.count = m_StringBuilder.Length - m_CurrentMapping.startIndex;
if (m_CurrentMapping.count > 0)
m_Mappings.Add(m_CurrentMapping);
var source = m_StringBuilder.ToString();
sourceMap = new ShaderSourceMap(source, m_Mappings);
m_Mappings.RemoveAt(m_Mappings.Count - 1);
m_CurrentMapping.count = 0;
return source;
}
public string ToString(int startIndex, int length)
{
return m_StringBuilder.ToString(startIndex, length);
}
internal void Clear()
{
m_StringBuilder.Length = 0;
}
internal int length
{
get { return m_StringBuilder.Length; }
set { m_StringBuilder.Length = value; }
}
}
}

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


using System;
using System.Collections.Generic;
using UnityEditor.Graphing.Util;
using UnityEditor.Graphing;
using UnityEditor.Graphing;
using Object = UnityEngine.Object;
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{

VisualElement m_PropertyItems;
VisualElement m_LayerItems;
VisualElement m_ContentContainer;
TypeMapper m_TypeMapper;
List<INode> m_SelectedNodes;
Vector2 m_PreviewScrollPosition;

public GraphInspectorView(string assetName, PreviewManager previewManager, AbstractMaterialGraph graph)
{
m_Graph = graph;
m_SelectedNodes = new List<INode>();
AddStyleSheetPath("Styles/MaterialGraph");

}
topContainer.Add(headerContainer);
m_ContentContainer = new VisualElement {name = "content"};
topContainer.Add(m_ContentContainer);
}
Add(topContainer);
var bottomContainer = new VisualElement {name = "bottom"};
{
var propertiesContainer = new VisualElement {name = "properties"};
{
var header = new VisualElement {name = "header"};

m_PropertyItems = new VisualContainer {name = "items"};
propertiesContainer.Add(m_PropertyItems);
}
bottomContainer.Add(propertiesContainer);
topContainer.Add(propertiesContainer);
}
Add(topContainer);
var bottomContainer = new VisualElement {name = "bottom"};
{
m_PreviewTextureView = new PreviewTextureView { name = "preview", image = Texture2D.blackTexture };
m_PreviewTextureView.AddManipulator(new Draggable(OnMouseDrag, true));
bottomContainer.Add(m_PreviewTextureView);

m_PreviewMeshPicker = new ObjectField() { objectType = typeof(Mesh) };
m_PreviewMeshPicker = new ObjectField { objectType = typeof(Mesh) };
m_PreviewMeshPicker.OnValueChanged(OnPreviewMeshChanged);
bottomContainer.Add(m_PreviewMeshPicker);

Add(new ResizeSideHandle(this, ResizeHandleAnchor.Right, new string[] {"resize", "horizontal", "right"}));
Add(new ResizeSideHandle(this, ResizeHandleAnchor.Bottom, new string[] {"resize", "vertical", "bottom"}));
Add(new ResizeSideHandle(this, ResizeHandleAnchor.Left, new string[] {"resize", "horizontal", "left"}));
// Nodes missing custom editors:
// - PropertyNode
// - SubGraphInputNode
// - SubGraphOutputNode
m_TypeMapper = new TypeMapper(typeof(INode), typeof(AbstractNodeEditorView), typeof(StandardNodeEditorView))
{
// { typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeEditorView) }
};
}
void OnResize(Vector2 resizeDelta, ResizeDirection direction, bool moveWhileResize)

m_PreviewTextureView.Dirty(ChangeType.Repaint);
}
void OnPreviewMeshChanged(ChangeEvent<UnityEngine.Object> changeEvent)
void OnPreviewMeshChanged(ChangeEvent<Object> changeEvent)
{
Mesh changedMesh = changeEvent.newValue as Mesh;

}
m_Graph.previewData.serializedMesh.mesh = changedMesh;
}
public void UpdateSelection(IEnumerable<INode> nodes)
{
m_SelectedNodes.Clear();
m_SelectedNodes.AddRange(nodes);
var selectionHash = UIUtilities.GetHashCode(m_SelectedNodes.Count,
m_SelectedNodes != null ? m_SelectedNodes.FirstOrDefault() : null);
if (selectionHash != m_SelectionHash)
{
m_SelectionHash = selectionHash;
m_ContentContainer.Clear();
if (m_SelectedNodes.Count > 1)
{
var element = new Label(string.Format("{0} nodes selected.", m_SelectedNodes.Count))
{
name = "selectionCount"
};
m_ContentContainer.Add(element);
}
else if (m_SelectedNodes.Count == 1)
{
var node = m_SelectedNodes.First();
var view = (AbstractNodeEditorView)Activator.CreateInstance(m_TypeMapper.MapType(node.GetType()));
view.node = node;
m_ContentContainer.Add(view);
}
}
}
public void HandleGraphChanges()

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


using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEditor.Graphing.Util;
using UnityEditor.ShaderGraph;
using UnityEngine.Rendering;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;

if (uberNodes.Count > 0)
{
m_UberShaderIds.Clear();
m_UberShaderString = m_Graph.GetUberPreviewShader(m_UberShaderIds, out m_OutputIdProperty);
var results = m_Graph.GetUberPreviewShader();
m_UberShaderString = results.shader;
m_OutputIdProperty = results.outputIdProperty;
m_UberShaderIds = results.ids;
var message = "RecreateUberShader: " + Environment.NewLine + m_UberShaderString;
Debug.LogWarning(message);
var errors = MaterialGraphAsset.GetShaderErrors(m_UberShader);
var message = new ShaderStringBuilder();
message.AppendLine(@"Preview shader for graph has {0} error{1}:", errors.Length, errors.Length != 1 ? "s" : "");
foreach (var error in errors)
{
INode node = null;
try
{
node = results.sourceMap.FindNode(error.line);
}
catch (Exception)
{
Debug.LogWarning("ERROR");
continue;
}
message.AppendLine("{0} in {3} at line {1} (on {2})", error.message, error.line, error.platform, node != null ? string.Format("node {0} ({1})", node.name, node.guid) : "graph");
message.AppendLine(error.messageDetails);
message.AppendNewLine();
}
Debug.LogWarning(message.ToString());
ShaderUtil.ClearShaderErrors(m_UberShader);
ShaderUtil.UpdateShaderAsset(m_UberShader, k_EmptyShader);
uberShaderHasError = true;

}
else
{
PreviewMode mode;
if (node is IMasterNode)
var masterNode = node as IMasterNode;
if (masterNode != null)
shaderData.shaderString = ((IMasterNode)node).GetShader(GenerationMode.Preview, node.name, out configuredTextures);
shaderData.shaderString = masterNode.GetShader(GenerationMode.Preview, node.name, out configuredTextures);
shaderData.shaderString = m_Graph.GetPreviewShader(node, out mode);
shaderData.shaderString = m_Graph.GetPreviewShader(node).shader;
}
File.WriteAllText(Application.dataPath + "/../GeneratedShader.shader", (shaderData.shaderString ?? "null").Replace("UnityEngine.MaterialGraph", "Generated"));

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


m_GraphInspectorView = new GraphInspectorView(assetName, previewManager, graph) { name = "inspector" };
m_GraphInspectorView.AddManipulator(new Draggable(OnMouseDrag, true));
m_GraphView.RegisterCallback<PostLayoutEvent>(OnPostLayout);
m_GraphView.onSelectionChanged += m_GraphInspectorView.UpdateSelection;
m_GraphInspectorView.RegisterCallback<PostLayoutEvent>(OnPostLayout);
m_GraphView.Add(m_GraphInspectorView);
m_GraphView.graphViewChanged = GraphViewChanged;

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


}
else
{
PreviewMode previewMode;
FloatShaderProperty outputIdProperty;
var shader = graph.GetShader(node, GenerationMode.ForReals, node.name, out textureInfo, out previewMode, out outputIdProperty);
GUIUtility.systemCopyBuffer = shader;
GUIUtility.systemCopyBuffer = graph.GetShader(node, GenerationMode.ForReals, node.name).shader;
}
}

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


background-color: rgb(79, 79, 79);
}
GraphInspectorView > #bottom > #properties {
GraphInspectorView > #top > #properties {
border-color: rgb(41, 41, 41);
border-top-width: 1;
border-bottom-width: 1;

margin-right: 8;
}
GraphInspectorView > #bottom > #properties > #header {
GraphInspectorView > #top > #properties > #header {
border-color: rgb(41, 41, 41);
border-bottom-width: 1;
flex-direction: row;

GraphInspectorView > #bottom > #properties > #header > #title {
GraphInspectorView > #top > #properties > #header > #title {
text-color: rgb(180, 180, 180);
font-style: bold;
padding-top: 8;

}
GraphInspectorView > #bottom > #properties > #header > #addButton {
GraphInspectorView > #top > #properties > #header > #addButton {
height: 24;
margin-top: 0;
margin-bottom: 0;

GraphInspectorView > #bottom > #properties > #items {
GraphInspectorView > #top > #properties > #items {
padding-bottom: 4;
}

2
MaterialGraphProject/Assets/UnityShaderEditor/package.json


{
"name": "com.unity.shadergraph",
"description": "Shader Graph",
"version": "0.1.9",
"version": "0.1.10",
"unity": "2018.1",
"dependencies": {
}

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


using System;
using System.Collections.Generic;
namespace UnityEditor.ShaderGraph
{
public class GenerationResults
{
public string shader { get; set; }
public List<PropertyCollector.TextureInfo> configuredTextures;
public PreviewMode previewMode { get; set; }
public FloatShaderProperty outputIdProperty { get; set; }
public Dictionary<Guid, int> ids { get; set; }
public ShaderSourceMap sourceMap { get; set; }
public GenerationResults()
{
configuredTextures = new List<PropertyCollector.TextureInfo>();
ids = new Dictionary<Guid, int>();
}
}
}

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


fileFormatVersion: 2
guid: c922293e622b43f4a1cd25a1246a17dc
timeCreated: 1513866299

64
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderSourceMap.cs


using System;
using System.Collections.Generic;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public class ShaderSourceMap
{
// Indicates where a new node begins
List<int> m_LineStarts;
List<INode> m_Nodes;
int m_LineCount;
internal ShaderSourceMap(string source, List<ShaderStringMapping> mappings)
{
m_LineStarts = new List<int>();
m_Nodes = new List<INode>();
var line = 0;
var currentIndex = 0;
foreach (var mapping in mappings)
{
var stopIndex = mapping.startIndex + mapping.count;
if (currentIndex >= stopIndex)
continue;
m_LineStarts.Add(line);
m_Nodes.Add(mapping.node);
while (currentIndex < stopIndex && currentIndex != -1)
{
currentIndex = source.IndexOf('\n', currentIndex+1);
line++;
}
if (currentIndex == -1)
break;
}
m_LineCount = line;
}
public INode FindNode(int line)
{
if (line >= m_LineCount || line < 0)
return null;
var l = 0;
var r = m_LineStarts.Count - 1;
while (l <= r)
{
var m = (l + r) / 2;
var lineStart = m_LineStarts[m];
var lineStop = m == m_LineStarts.Count ? m_LineCount : m_LineStarts[m + 1];
if (line >= lineStop)
l = m + 1;
else if (line < lineStart)
r = m - 1;
else
return m_Nodes[m];
}
throw new Exception("Something went wrong in binary search");
}
}
}

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


fileFormatVersion: 2
guid: f6c222989d9946e8a85c01ba541f4b41
timeCreated: 1513867574
正在加载...
取消
保存