浏览代码

Merge pull request #262 from Unity-Technologies/rebuild-all-dependant-shaders-from-subgraph

When saving a sub graph, we now go through all the shader graphs and …
/main
GitHub 7 年前
当前提交
3de3ae25
共有 6 个文件被更改,包括 77 次插入18 次删除
  1. 7
      com.unity.shadergraph/Editor/Data/Nodes/Utility/SubGraphNode.cs
  2. 10
      com.unity.shadergraph/Editor/Data/SubGraph/SubGraph.cs
  3. 9
      com.unity.shadergraph/Editor/Data/SubGraph/SubGraphOutputNode.cs
  4. 35
      com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  5. 19
      com.unity.shadergraph/Editor/Drawing/SearchWindowProvider.cs
  6. 15
      com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs

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


public class SubGraphNode : AbstractMaterialNode
, IGeneratesBodyCode
, IOnAssetEnabled
, IGeneratesFunction
, IGeneratesFunction
, IMayRequireNormal
, IMayRequireTangent
, IMayRequireBitangent

public SubGraphNode()
{
name = "Sub-graph";
}
public override bool allowedInSubGraph
{
get { return false; }
}
public override string documentationURL

10
com.unity.shadergraph/Editor/Data/SubGraph/SubGraph.cs


public override void AddNode(INode node)
{
if (outputNode != null && node is SubGraphOutputNode)
{
Debug.LogWarning("Attempting to add second SubGraphOutputNode to SubGraph. This is not allowed.");
return;
}
var materialNode = node as AbstractMaterialNode;
if (materialNode != null)
{

return outputNode != null ? outputNode.graphOutputs : new List<MaterialSlot>();
}
}
public void GenerateSubGraphFunction(string functionName, FunctionRegistry registry, ShaderGraphRequirements reqs, GenerationMode generationMode)
{
registry.ProvideFunction(functionName, s =>

// Now generate outputs
foreach (var slot in graphOutputs)
arguments.Add(string.Format("out {0} {1}", slot.concreteValueType.ToString(outputNode.precision), slot.shaderOutputName));
// Create the function protoype from the arguments
s.AppendLine("void {0}({1})"
, functionName

9
com.unity.shadergraph/Editor/Data/SubGraph/SubGraphOutputNode.cs


void OnRemove()
{
m_Node.RemoveSlot();
// tell the user that they might cchange things up.
if (EditorUtility.DisplayDialog("Sub Graph Will Change", "If you remove a slot and save the sub graph, you might change other graphs that are using this sub graph.\n\nDo you want to continue?", "Yes", "No"))
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Removing Slot");
m_Node.RemoveSlot();
}
}
}

visitor.AddShaderChunk(string.Format("{0} = {1};", slot.shaderOutputName, GetSlotValue(slot.id, generationMode)), true);
}
public IEnumerable<MaterialSlot> graphOutputs
{
get

}
}
}

35
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)

return;
UpdateShaderGraphOnDisk(path, graph);
}
static void UpdateShaderGraphOnDisk(string path, IShaderGraph graph)
{
var shaderImporter = AssetImporter.GetAtPath(path) as ShaderGraphImporter;
if (shaderImporter == null)
return;

AssetDatabase.ImportAsset(path);
}
private void Rebuild()
{

19
com.unity.shadergraph/Editor/Drawing/SearchWindowProvider.cs


{
foreach (var type in assembly.GetTypesOrNothing())
{
if (type.IsClass && !type.IsAbstract && (type.IsSubclassOf(typeof(AbstractMaterialNode))) && type != typeof(PropertyNode))
if (type.IsClass && !type.IsAbstract && (type.IsSubclassOf(typeof(AbstractMaterialNode)))
&& type != typeof(PropertyNode)
&& type != typeof(SubGraphNode))
{
var attrs = type.GetCustomAttributes(typeof(TitleAttribute), false) as TitleAttribute[];
if (attrs != null && attrs.Length > 0)

}
}
foreach (var guid in AssetDatabase.FindAssets(string.Format("t:{0}", typeof(MaterialSubGraphAsset))))
if (!(m_Graph is SubGraph))
var asset = AssetDatabase.LoadAssetAtPath<MaterialSubGraphAsset>(AssetDatabase.GUIDToAssetPath(guid));
var node = new SubGraphNode { subGraphAsset = asset };
AddEntries(node, new [] { "Sub-graph Assets", asset.name }, nodeEntries);
foreach (var guid in AssetDatabase.FindAssets(string.Format("t:{0}", typeof(MaterialSubGraphAsset))))
{
var asset = AssetDatabase.LoadAssetAtPath<MaterialSubGraphAsset>(AssetDatabase.GUIDToAssetPath(guid));
var node = new SubGraphNode { subGraphAsset = asset };
AddEntries(node, new[] { "Sub-graph Assets", asset.name }, nodeEntries);
}
}
foreach (var property in m_Graph.properties)

void AddEntries(AbstractMaterialNode node, string[] title, List<NodeEntry> nodeEntries)
{
if (m_Graph is SubGraph && !node.allowedInSubGraph)
return;
if (m_Graph is MaterialGraph && !node.allowedInMainGraph)
return;
if (connectedPort == null)
{
nodeEntries.Add(new NodeEntry

15
com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs


void DeleteSelectionImplementation(string operationName, GraphView.AskUser askUser)
{
foreach (var selectable in selection)
{
var field = selectable as BlackboardField;
if (field != null && field.userData != null)
{
if (EditorUtility.DisplayDialog("Sub Graph Will Change", "If you remove a property and save the sub graph, you might change other graphs that are using this sub graph.\n\nDo you want to continue?", "Yes", "No"))
break;
return;
}
}
graph.RemoveElements(selection.OfType<MaterialNodeView>().Select(x => (INode)x.node), selection.OfType<Edge>().Select(x => x.userData).OfType<IEdge>());
graph.RemoveElements(selection.OfType<MaterialNodeView>().Where(v => !(v.node is SubGraphOutputNode)).Select(x => (INode)x.node), selection.OfType<Edge>().Select(x => x.userData).OfType<IEdge>());
foreach (var selectable in selection)
{
var field = selectable as BlackboardField;

graph.RemoveShaderProperty(property.guid);
}
}
selection.Clear();
}

正在加载...
取消
保存