浏览代码

Merge pull request #1435 from Unity-Technologies/sg/import-dependencies

Import dependencies
/main
GitHub 7 年前
当前提交
58fd779d
共有 14 个文件被更改,包括 131 次插入89 次删除
  1. 37
      com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDPBRSubShader.cs
  2. 22
      com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDUnlitSubShader.cs
  3. 14
      com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/LightWeightPBRSubShader.cs
  4. 28
      com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/LightWeightUnlitSubShader.cs
  5. 2
      com.unity.shadergraph/Editor/Data/Graphs/IShaderGraph.cs
  6. 4
      com.unity.shadergraph/Editor/Data/Graphs/MaterialGraph.cs
  7. 5
      com.unity.shadergraph/Editor/Data/MasterNodes/ISubShader.cs
  8. 3
      com.unity.shadergraph/Editor/Data/Nodes/AbstractMaterialNode.cs
  9. 2
      com.unity.shadergraph/Editor/Data/Nodes/IMasterNode.cs
  10. 4
      com.unity.shadergraph/Editor/Data/Nodes/MasterNode.cs
  11. 12
      com.unity.shadergraph/Editor/Data/Nodes/Utility/SubGraphNode.cs
  12. 29
      com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  13. 46
      com.unity.shadergraph/Editor/Importers/ShaderGraphImporter.cs
  14. 12
      com.unity.shadergraph/Editor/Importers/ShaderSubGraphImporter.cs

37
com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDPBRSubShader.cs


return defines.GetShaderString(2);
}
private static bool GenerateShaderPass(PBRMasterNode masterNode, Pass pass, GenerationMode mode, SurfaceMaterialOptions materialOptions, ShaderGenerator result)
private static bool GenerateShaderPass(PBRMasterNode masterNode, Pass pass, GenerationMode mode, SurfaceMaterialOptions materialOptions, ShaderGenerator result, List<string> sourceAssetDependencyPaths)
{
var templateLocation = Path.Combine(Path.Combine(Path.Combine(HDEditorUtils.GetHDRenderPipelinePath(), "Editor"), "ShaderGraph"), pass.TemplateName);
if (!File.Exists(templateLocation))

}
if (sourceAssetDependencyPaths != null)
sourceAssetDependencyPaths.Add(templateLocation);
// grab all of the active nodes
var activeNodeList = ListPool<INode>.Get();

return true;
}
public string GetSubshader(IMasterNode iMasterNode, GenerationMode mode)
public string GetSubshader(IMasterNode iMasterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
if (sourceAssetDependencyPaths != null)
{
// HDPBRSubShader.cs
sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("c4e8610eb7ce19747bb637c68acc55cd"));
// HDSubShaderUtilities.cs
sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("713ced4e6eef4a44799a4dd59041484b"));
}
var masterNode = iMasterNode as PBRMasterNode;
var subShader = new ShaderGenerator();
subShader.AddShaderChunk("SubShader", true);

if (opaque)
{
GenerateShaderPass(masterNode, m_PassGBuffer, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassGBufferWithPrepass, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassGBuffer, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassGBufferWithPrepass, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassMETA, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassShadowCaster, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassMETA, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassShadowCaster, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassDepthOnly, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassMotionVectors, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassDepthOnly, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassMotionVectors, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassDistortion, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassDistortion, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassTransparentDepthPrepass, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassTransparentDepthPrepass, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassTransparentBackface, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassTransparentBackface, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassForward, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassForward, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassTransparentDepthPostpass, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassTransparentDepthPostpass, mode, materialOptions, subShader, sourceAssetDependencyPaths);
}
}
subShader.Deindent();

22
com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDUnlitSubShader.cs


return defines.GetShaderString(2);
}
private static bool GenerateShaderPass(UnlitMasterNode masterNode, Pass pass, GenerationMode mode, SurfaceMaterialOptions materialOptions, ShaderGenerator result)
private static bool GenerateShaderPass(UnlitMasterNode masterNode, Pass pass, GenerationMode mode, SurfaceMaterialOptions materialOptions, ShaderGenerator result, List<string> sourceAssetDependencyPaths)
{
var templateLocation = Path.Combine(Path.Combine(Path.Combine(HDEditorUtils.GetHDRenderPipelinePath(), "Editor"), "ShaderGraph"), pass.TemplateName);
if (!File.Exists(templateLocation))

}
sourceAssetDependencyPaths.Add(templateLocation);
// grab all of the active nodes
var activeNodeList = ListPool<INode>.Get();

return true;
}
public string GetSubshader(IMasterNode inMasterNode, GenerationMode mode)
public string GetSubshader(IMasterNode inMasterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
if (sourceAssetDependencyPaths != null)
{
// HDUnlitSubShader.cs
sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("292c6a3c80161fa4cb49a9d11d35cbe9"));
// HDSubShaderUtilities.cs
sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("713ced4e6eef4a44799a4dd59041484b"));
}
var masterNode = inMasterNode as UnlitMasterNode;
var subShader = new ShaderGenerator();
subShader.AddShaderChunk("SubShader", true);

// bool transparent = (masterNode.surfaceType != SurfaceType.Opaque);
bool distortionActive = false;
GenerateShaderPass(masterNode, m_PassDepthOnly, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassForward, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassMETA, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassDepthOnly, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassForward, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassMETA, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPass(masterNode, m_PassDistortion, mode, materialOptions, subShader);
GenerateShaderPass(masterNode, m_PassDistortion, mode, materialOptions, subShader, sourceAssetDependencyPaths);
}
}
subShader.Deindent();

14
com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/LightWeightPBRSubShader.cs


}
};
public string GetSubshader(IMasterNode inMasterNode, GenerationMode mode)
public string GetSubshader(IMasterNode inMasterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
if (sourceAssetDependencyPaths != null)
{
// LightWeightPBRSubShader.cs
sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("ca91dbeb78daa054c9bbe15fef76361c"));
}
if (sourceAssetDependencyPaths != null)
{
sourceAssetDependencyPaths.Add(templatePath);
sourceAssetDependencyPaths.Add(extraPassesTemplatePath);
}
string forwardTemplate = File.ReadAllText(templatePath);
string extraTemplate = File.ReadAllText(extraPassesTemplatePath);

28
com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/LightWeightUnlitSubShader.cs


using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Experimental.Rendering.LightweightPipeline;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph;

}
};
public string GetSubshader(IMasterNode inMasterNode, GenerationMode mode)
public string GetSubshader(IMasterNode inMasterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
if (sourceAssetDependencyPaths != null)
{
// LightWeightUnlitSubShader.cs
sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("3ef30c5c1d5fc412f88511ef5818b654"));
}
if (sourceAssetDependencyPaths != null)
{
sourceAssetDependencyPaths.Add(templatePath);
sourceAssetDependencyPaths.Add(extraPassesTemplatePath);
}
string forwardTemplate = File.ReadAllText(templatePath);
string extraTemplate = File.ReadAllText(extraPassesTemplatePath);

static string GetTemplatePath(string templateName)
{
string relativeTemplatePath = Path.Combine("LWRP", Path.Combine("Editor", Path.Combine("ShaderGraph", templateName)));
foreach (var path in LightweightIncludePaths.GetPaths())
{
var templatePath = Path.Combine(path, relativeTemplatePath);
if (File.Exists(templatePath))
return templatePath;
}
throw new FileNotFoundException(string.Format(@"Cannot find a template with name ""{0}"".", templateName));
var pathSegments = new[] { "Packages", "com.unity.render-pipelines.lightweight", "LWRP", "Editor", "ShaderGraph", templateName };
var path = pathSegments.Aggregate("", Path.Combine);
if (!File.Exists(path))
throw new FileNotFoundException(string.Format(@"Cannot find a template with name ""{0}"".", templateName));
return path;
}
static string GetShaderPassFromTemplate(string template, UnlitMasterNode masterNode, Pass pass, GenerationMode mode, SurfaceMaterialOptions materialOptions)

2
com.unity.shadergraph/Editor/Data/Graphs/IShaderGraph.cs


{
public interface IShaderGraph
{
string GetShader(string name, GenerationMode mode, out List<PropertyCollector.TextureInfo> configuredTextures);
string GetShader(string name, GenerationMode mode, out List<PropertyCollector.TextureInfo> configuredTextures, List<string> sourceAssetDependencyPaths = null);
void LoadedFromDisk();
}
}

4
com.unity.shadergraph/Editor/Data/Graphs/MaterialGraph.cs


get { return GetNodes<INode>().OfType<IMasterNode>().FirstOrDefault(); }
}
public string GetShader(string name, GenerationMode mode, out List<PropertyCollector.TextureInfo> configuredTextures)
public string GetShader(string name, GenerationMode mode, out List<PropertyCollector.TextureInfo> configuredTextures, List<string> sourceAssetDependencyPaths = null)
return masterNode.GetShader(mode, name, out configuredTextures);
return masterNode.GetShader(mode, name, out configuredTextures, sourceAssetDependencyPaths);
}
public void LoadedFromDisk()

5
com.unity.shadergraph/Editor/Data/MasterNodes/ISubShader.cs


using System;
using UnityEditor.Graphing;
using UnityEngine.Experimental.UIElements;
using System.Collections.Generic;
string GetSubshader(IMasterNode masterNode, GenerationMode mode);
string GetSubshader(IMasterNode masterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null);
}
}

3
com.unity.shadergraph/Editor/Data/Nodes/AbstractMaterialNode.cs


var slot = FindSlot<MaterialSlot>(slotId);
return slot != null && owner.GetEdges(slot.slotReference).Any();
}
public virtual void GetSourceAssetDependencies(List<string> paths)
{}
}
}

2
com.unity.shadergraph/Editor/Data/Nodes/IMasterNode.cs


{
public interface IMasterNode : INode
{
string GetShader(GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures);
string GetShader(GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures, List<string> sourceAssetDependencyPaths = null);
bool IsPipelineCompatible(IRenderPipeline renderPipeline);
}
}

4
com.unity.shadergraph/Editor/Data/Nodes/MasterNode.cs


Dirty(ModificationScope.Graph);
}
public string GetShader(GenerationMode mode, string outputName, out List<PropertyCollector.TextureInfo> configuredTextures)
public string GetShader(GenerationMode mode, string outputName, out List<PropertyCollector.TextureInfo> configuredTextures, List<string> sourceAssetDependencyPaths = null)
{
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);

}
foreach (var subShader in m_SubShaders)
finalShader.AppendLines(subShader.GetSubshader(this, mode));
finalShader.AppendLines(subShader.GetSubshader(this, mode, sourceAssetDependencyPaths));
finalShader.AppendLine(@"FallBack ""Hidden/InternalErrorShader""");
}

12
com.unity.shadergraph/Editor/Data/Nodes/Utility/SubGraphNode.cs


return referencedGraph.activeNodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor(stageCapability));
}
public override void GetSourceAssetDependencies(List<string> paths)
{
base.GetSourceAssetDependencies(paths);
if (subGraphAsset != null)
{
var assetPath = AssetDatabase.GetAssetPath(subGraphAsset);
paths.Add(assetPath);
foreach (var dependencyPath in AssetDatabase.GetDependencies(assetPath))
paths.Add(dependencyPath);
}
}
}
}

29
com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs


graphEditorView = null;
}
void UpdateDependantGraphs()
{
string[] lookFor = new string[] {"Assets"};
var guids = AssetDatabase.FindAssets("t:shader", lookFor);
foreach (string guid in guids)
{
if (AssetDatabase.GUIDToAssetPath(guid).ToLower().EndsWith(ShaderGraphImporter.ShaderGraphExtension))
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var textGraph = File.ReadAllText(path, Encoding.UTF8);
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph);
graph.LoadedFromDisk();
foreach (SubGraphNode graphNode in graph.GetNodes<SubGraphNode>())
{
var subpath = AssetDatabase.GetAssetPath(graphNode.subGraphAsset);
var subguid = AssetDatabase.AssetPathToGUID(subpath);
if (subguid == selectedGuid)
{
UpdateShaderGraphOnDisk(path, graph);
}
}
}
}
}
public void PingAsset()
{
if (selectedGuid != null)

File.WriteAllText(path, EditorJsonUtility.ToJson(graph, true));
AssetDatabase.ImportAsset(path);
UpdateDependantGraphs();
}
void UpdateShaderGraphOnDisk(string path)

46
com.unity.shadergraph/Editor/Importers/ShaderGraphImporter.cs


using UnityEditor.Experimental.AssetImporters;
using UnityEditor.ShaderGraph.Drawing;
[ScriptedImporter(14, ShaderGraphImporter.ShaderGraphExtension)]
[ScriptedImporter(15, ShaderGraphExtension)]
public class ShaderGraphImporter : ScriptedImporter
{
public const string ShaderGraphExtension = "shadergraph";

ShaderUtil.ClearShaderErrors(oldShader);
List<PropertyCollector.TextureInfo> configuredTextures;
var text = GetShaderText(ctx.assetPath, out configuredTextures);
var shader = ShaderUtil.CreateShaderAsset(text);
EditorMaterialUtility.SetShaderDefaults(
shader,
configuredTextures.Where(x => x.modifiable).Select(x => x.name).ToArray(),
configuredTextures.Where(x => x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());
EditorMaterialUtility.SetShaderNonModifiableDefaults(
shader,
configuredTextures.Where(x => !x.modifiable).Select(x => x.name).ToArray(),
configuredTextures.Where(x => !x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());
ctx.AddObjectToAsset("MainAsset", shader);
ctx.SetMainObject(shader);
}
internal static string GetShaderText(string path, out List<PropertyCollector.TextureInfo> configuredTextures)
{
string path = ctx.assetPath;
var sourceAssetDependencyPaths = new List<string>();
var shaderName = Path.GetFileNameWithoutExtension(path);
try
{

if (!string.IsNullOrEmpty(graph.path))
shaderName = graph.path + "/" + shaderName;
shaderString = graph.GetShader(shaderName, GenerationMode.ForReals, out configuredTextures);
shaderString = graph.GetShader(shaderName, GenerationMode.ForReals, out configuredTextures, sourceAssetDependencyPaths);
foreach (var node in graph.GetNodes<AbstractMaterialNode>())
node.GetSourceAssetDependencies(sourceAssetDependencyPaths);
}
catch (Exception)
{

return shaderString ?? k_ErrorShader.Replace("Hidden/GraphErrorShader2", shaderName);
var text = shaderString ?? k_ErrorShader.Replace("Hidden/GraphErrorShader2", shaderName);
var shader = ShaderUtil.CreateShaderAsset(text);
EditorMaterialUtility.SetShaderDefaults(
shader,
configuredTextures.Where(x => x.modifiable).Select(x => x.name).ToArray(),
configuredTextures.Where(x => x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());
EditorMaterialUtility.SetShaderNonModifiableDefaults(
shader,
configuredTextures.Where(x => !x.modifiable).Select(x => x.name).ToArray(),
configuredTextures.Where(x => !x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());
ctx.AddObjectToAsset("MainAsset", shader);
ctx.SetMainObject(shader);
foreach (var sourceAssetDependencyPath in sourceAssetDependencyPaths.Distinct())
ctx.DependsOnSourceAsset(sourceAssetDependencyPath);
}
}

12
com.unity.shadergraph/Editor/Importers/ShaderSubGraphImporter.cs


using System.Collections.Generic;
using System.Linq;
[ScriptedImporter(1, "ShaderSubGraph")]
[ScriptedImporter(2, "ShaderSubGraph")]
public class ShaderSubGraphImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)

if (graph == null)
return;
var sourceAssetDependencyPaths = new List<string>();
foreach (var node in graph.GetNodes<AbstractMaterialNode>())
node.GetSourceAssetDependencies(sourceAssetDependencyPaths);
foreach (var sourceAssetDependencyPath in sourceAssetDependencyPaths.Distinct())
ctx.DependsOnSourceAsset(sourceAssetDependencyPath);
}
}
正在加载...
取消
保存