浏览代码

Added name to abstract material graph. This one is the name from the materialgraph on disk. Added a write file and open file in graph utils. Changed the converttoshader functionality to return a string so it could be re-used in implementation for writing a file.

/main
Peter Bay Bastian 7 年前
当前提交
3eef43d8
共有 5 个文件被更改,包括 71 次插入105 次删除
  1. 4
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 36
      ShaderGraph/com.unity.shadergraph/Editor/Data/Util/GraphUtil.cs
  3. 84
      ShaderGraph/com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  4. 11
      ShaderGraph/com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs
  5. 41
      ShaderGraph/com.unity.shadergraph/Editor/Drawing/Views/MaterialNodeView.cs

4
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs


set { m_PreviewData = value; }
}
[NonSerialized]
string m_Name;
public string name { get; set; }
[SerializeField]
string m_Path;

36
ShaderGraph/com.unity.shadergraph/Editor/Data/Util/GraphUtil.cs


using System;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditorInternal;
using Debug = UnityEngine.Debug;
namespace UnityEditor.ShaderGraph
{

}
return string.Format(duplicateFormat, name, duplicateNumber);
}
public static bool WriteToFile(string path, string content)
{
try
{
File.WriteAllText(path, content);
return true;
}
catch (Exception e)
{
Debug.LogError(e);
return false;
}
}
public static void OpenFile(string path)
{
if (!File.Exists(Path.GetFullPath(path)))
{
Debug.LogError(string.Format("Path {0} doesn't exists", path));
return;
}
string file = Path.GetFullPath(path);
ProcessStartInfo pi = new ProcessStartInfo(file);
pi.Arguments = Path.GetFileName(file);
pi.UseShellExecute = true;
pi.WorkingDirectory = Path.GetDirectoryName(file);
pi.FileName = ScriptEditorUtility.GetExternalScriptEditor();
pi.Verb = "OPEN";
Process.Start(pi);
}
}
}

84
ShaderGraph/com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs


m_GraphEditorView.saveRequested += UpdateAsset;
m_GraphEditorView.convertToSubgraphRequested += ToSubGraph;
m_GraphEditorView.showInProjectRequested += PingAsset;
m_GraphEditorView.showGeneratedCode += ShowGeneratedCode;
this.GetRootVisualContainer().Add(graphEditorView);
}
}

materialGraphEditWindow.Rebuild();
}
}
}
public void ShowGeneratedCode()
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(selectedGuid));
Debug.Log(asset.name);
string path = String.Format("Temp/GeneratedFromGraph-{0}.shader", asset.name);
//Debug.Log(path);
WriteStringToFile(path);
/*
if (!WriteStringToFile(compiledShader, path, kNotAtomic, kFileFlagDontIndex | kFileFlagTemporary))
return;
path = PathToAbsolutePath(path);
if (!OpenScriptFile(path, 0))
WarningStringWithoutStacktrace("Unable to open " + path + ": Check external editor in preferences");
*/
//core::string path = Format("Temp/GeneratedFromSurface-%s.shader", shader->GetOutputFilename().c_str());
}
private void WriteStringToFile(string path)
{
List<PropertyCollector.TextureInfo> textureInfo;
var graphView = graphEditorView.graphView;
foreach (AbstractMaterialNode node in graphView.graph.GetNodes<AbstractMaterialNode>())
{
var masterNode = node as IMasterNode;
if (masterNode != null)
{
Debug.Log(path);
var shader = masterNode.GetShader(GenerationMode.ForReals, node.name, out textureInfo);
Debug.Log(shader);
//GUIUtility.systemCopyBuffer = shader;
File.WriteAllText(path, shader);
Debug.Log(ScriptEditorUtility.GetExternalScriptEditor());
string file = Path.GetFullPath(path);
ProcessStartInfo pi = new ProcessStartInfo(file);
pi.Arguments = Path.GetFileName(file);
pi.UseShellExecute = true;
pi.WorkingDirectory = Path.GetDirectoryName(file);
pi.FileName = ScriptEditorUtility.GetExternalScriptEditor();
pi.Verb = "OPEN";
Process.Start(pi);
}
}
// var nodes = graphView.selection.OfType<MaterialNodeView>().Where(x => (x.node is AbstractMaterialNode));
// Debug.Log(nodes.Length);
// foreach (var node in nodes)
// {
// var masterNode = node as IMasterNode;
// if (masterNode != null)
// {
// Debug.Log(path);
// var shader = masterNode.GetShader(GenerationMode.ForReals, node.name, out textureInfo);
// Debug.Log(shader);
// //GUIUtility.systemCopyBuffer = shader;
// File.WriteAllText(path, shader);
// //}
// }
//File tmpFile = new File;
// else if (atomicMode == kNotAtomic)
// {
// DeleteFile(path);
// tmpFilePath = path;
// }
}
public void ToSubGraph()

11
ShaderGraph/com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs


public Action showInProjectRequested { get; set; }
public Action showGeneratedCode { get; set; }
public MaterialGraphView graphView
{
get { return m_GraphView; }

if (showInProjectRequested != null)
showInProjectRequested();
}
GUILayout.Space(6);
if (GUILayout.Button("Show Generated Code", EditorStyles.toolbarButton))
{
if (showGeneratedCode != null)
showGeneratedCode();
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
graph.name = assetName;
m_GraphView = new MaterialGraphView(graph) { name = "GraphView", persistenceKey = "MaterialGraphView" };
m_GraphView.SetupZoom(0.05f, ContentZoomer.DefaultMaxScale);
m_GraphView.AddManipulator(new ContentDragger());

41
ShaderGraph/com.unity.shadergraph/Editor/Drawing/Views/MaterialNodeView.cs


public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
if (evt.target is Node)
evt.menu.AppendAction("Copy shader", ConvertToShader, node.hasPreview ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Hidden);
{
evt.menu.AppendAction("Copy shader", CopyToClipboard, node.hasPreview ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Hidden);
evt.menu.AppendAction("Show Generated Code", ShowGeneratedCode, node.hasPreview ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Hidden);
}
void ConvertToShader()
void CopyToClipboard()
{
GUIUtility.systemCopyBuffer = ConvertToShader();
}
public string SanitizeName(string name)
{
return new string(name.Where( c => !Char.IsWhiteSpace(c) ).ToArray());
}
public void ShowGeneratedCode()
{
var graph = (AbstractMaterialGraph)node.owner;
string path = String.Format("Temp/GeneratedFromGraph-{0}-{1}-{2}.shader", SanitizeName(graph.name), SanitizeName(node.name), node.guid );
if( GraphUtil.WriteToFile(path, ConvertToShader()) )
GraphUtil.OpenFile(path);
}
string ConvertToShader()
{
var shader = masterNode.GetShader(GenerationMode.ForReals, node.name, out textureInfo);
GUIUtility.systemCopyBuffer = shader;
}
else
{
var graph = (AbstractMaterialGraph)node.owner;
GUIUtility.systemCopyBuffer = graph.GetShader(node, GenerationMode.ForReals, node.name).shader;
}
return masterNode.GetShader(GenerationMode.ForReals, node.name, out textureInfo);
var graph = (AbstractMaterialGraph)node.owner;
return graph.GetShader(node, GenerationMode.ForReals, node.name).shader;
}
void UpdateSettingsExpandedState()

正在加载...
取消
保存