浏览代码

When saving a sub graph, we now go through all the shader graphs and updated them if they are using this sub graph.

Not allowed to have sub graph inside a sub graph
Removed sub graph from searh window when in subgraph
Prompting the user when removing slots and properties from the sub graph.
/main
Martin Thorzen 7 年前
当前提交
0a72fe97
共有 6 个文件被更改,包括 82 次插入21 次删除
  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. 37
      com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  5. 19
      com.unity.shadergraph/Editor/Drawing/SearchWindowProvider.cs
  6. 21
      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 void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

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

}
}
}

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


node.Dirty(ModificationScope.Node);
forceRedrawPreviews = false;
}
graphEditorView.HandleGraphChanges();
graphObject.graph.ClearChanges();
}

}
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()

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.GetTypes())
{
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

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


void DeleteSelectionImplementation(string operationName, GraphView.AskUser askUser)
{
graph.owner.RegisterCompleteObjectUndo(operationName);
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>());
bool userNotified = false;
var property = (IShaderProperty)field.userData;
graph.RemoveShaderProperty(property.guid);
if (!userNotified)
{
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"))
{
userNotified = true;
}
else
{
return;
}
}
if (userNotified)
{
var property = (IShaderProperty)field.userData;
graph.RemoveShaderProperty(property.guid);
}
}
}
selection.Clear();

正在加载...
取消
保存