浏览代码

Merge pull request #302 from Unity-Technologies/path-for-shaders

Change path for graphs
/main
GitHub 7 年前
当前提交
23cc01bd
共有 4 个文件被更改,包括 130 次插入19 次删除
  1. 15
      com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 103
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardProvider.cs
  3. 3
      com.unity.shadergraph/Editor/Drawing/SearchWindowProvider.cs
  4. 28
      com.unity.shadergraph/Editor/Importers/ShaderGraphImporter.cs

15
com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs


set { m_PreviewData = value; }
}
[SerializeField]
string m_Path;
public string path
{
get { return m_Path; }
set
{
if (m_Path == value)
return;
m_Path = value;
owner.RegisterCompleteObjectUndo("Change Path");
}
}
public void ClearChanges()
{
m_AddedNodes.Clear();

103
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardProvider.cs


using UnityEditor.Graphing;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;
using UnityEngine.Experimental.UIElements.StyleSheets;
namespace UnityEditor.ShaderGraph.Drawing
{

//WindowDraggable m_WindowDraggable;
//ResizeBorderFrame m_ResizeBorderFrame;
public Blackboard blackboard { get; private set; }
Label m_PathLabel;
TextField m_PathLabelTextField;
bool m_EditPathCancelled = false;
//public Action onDragFinished
//{

blackboard = new Blackboard()
{
scrollable = true,
title = assetName,
title = assetName.Split('/').Last(),
subTitle = FormatPath(graph.path),
m_PathLabel = blackboard.shadow.ElementAt(0).Q<Label>("subTitleLabel");
m_PathLabel.RegisterCallback<MouseDownEvent>(OnMouseDownEvent);
m_PathLabelTextField = new TextField { visible = false };
m_PathLabelTextField.RegisterCallback<FocusOutEvent>(e => { OnEditPathTextFinished();});
m_PathLabelTextField.RegisterCallback<KeyDownEvent>(OnPathTextFieldKeyPressed);
blackboard.shadow.Add(m_PathLabelTextField);
// m_WindowDraggable = new WindowDraggable(blackboard.shadow.Children().First().Q("header"));
// blackboard.AddManipulator(m_WindowDraggable);

foreach (var property in graph.properties)
AddProperty(property);
blackboard.Add(m_Section);
}
void OnMouseDownEvent(MouseDownEvent evt)
{
if (evt.clickCount == 2 && evt.button == (int)MouseButton.LeftMouse)
{
StartEditingPath();
evt.PreventDefault();
}
}
void StartEditingPath()
{
m_PathLabelTextField.visible = true;
m_PathLabelTextField.value = m_PathLabel.text;
m_PathLabelTextField.style.positionType = PositionType.Absolute;
var rect = m_PathLabel.ChangeCoordinatesTo(blackboard, new Rect(Vector2.zero, m_PathLabel.layout.size));
m_PathLabelTextField.style.positionLeft = rect.xMin;
m_PathLabelTextField.style.positionTop = rect.yMin;
m_PathLabelTextField.style.width = rect.width;
m_PathLabelTextField.style.fontSize = 11;
m_PathLabelTextField.style.marginLeft = 0;
m_PathLabelTextField.style.marginRight = 0;
m_PathLabelTextField.style.marginTop = 0;
m_PathLabelTextField.style.marginBottom = 0;
m_PathLabel.visible = false;
m_PathLabelTextField.Focus();
m_PathLabelTextField.SelectAll();
}
void OnPathTextFieldKeyPressed(KeyDownEvent evt)
{
switch (evt.keyCode)
{
case KeyCode.Escape:
m_EditPathCancelled = true;
m_PathLabelTextField.Blur();
break;
case KeyCode.Return:
case KeyCode.KeypadEnter:
m_PathLabelTextField.Blur();
break;
default:
break;
}
}
void OnEditPathTextFinished()
{
m_PathLabel.visible = true;
m_PathLabelTextField.visible = false;
var newPath = m_PathLabelTextField.text;
if (!m_EditPathCancelled && (newPath != m_PathLabel.text))
{
newPath = SanitizePath(newPath);
}
m_Graph.path = newPath;
m_PathLabel.text = FormatPath(newPath);
m_EditPathCancelled = false;
}
static string FormatPath(string path)
{
if (string.IsNullOrEmpty(path))
return "—";
return path;
}
static string SanitizePath(string path)
{
var splitString = path.Split('/');
List<string> newStrings = new List<string>();
foreach (string s in splitString)
{
if (!string.IsNullOrWhiteSpace(s))
{
newStrings.Add(s.Trim());
}
}
return string.Join("/", newStrings.ToArray());
}
void MoveItemRequested(Blackboard blackboard, int newIndex, VisualElement visualElement)

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


foreach (var guid in AssetDatabase.FindAssets(string.Format("t:{0}", typeof(MaterialSubGraphAsset))))
{
var asset = AssetDatabase.LoadAssetAtPath<MaterialSubGraphAsset>(AssetDatabase.GUIDToAssetPath(guid));
var title = asset.subGraph.path.Split('/').Append(asset.name).ToArray();
AddEntries(node, new[] { "Sub-graph Assets", asset.name }, nodeEntries);
AddEntries(node, title, nodeEntries);
}
}

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


using UnityEditor.Experimental.AssetImporters;
using UnityEditor.ShaderGraph.Drawing;
[ScriptedImporter(12, ShaderGraphImporter.ShaderGraphExtension)]
[ScriptedImporter(13, ShaderGraphImporter.ShaderGraphExtension)]
private string errorShader = @"
const string k_ErrorShader = @"
Shader ""Hidden/GraphErrorShader2""
{
SubShader

ShaderUtil.ClearShaderErrors(oldShader);
List<PropertyCollector.TextureInfo> configuredTextures;
var text = GetShaderText<MaterialGraph>(ctx.assetPath, out configuredTextures);
if (text == null)
text = errorShader;
var name = Path.GetFileNameWithoutExtension(ctx.assetPath);
string shaderName = string.Format("graphs/{0}", name);
text = text.Replace("Hidden/GraphErrorShader2", shaderName);
var text = GetShaderText(ctx.assetPath, out configuredTextures);
var shader = ShaderUtil.CreateShaderAsset(text);
EditorMaterialUtility.SetShaderDefaults(

ctx.SetMainObject(shader);
}
private static string GetShaderText<T>(string path, out List<PropertyCollector.TextureInfo> configuredTextures) where T : IShaderGraph
static string GetShaderText(string path, out List<PropertyCollector.TextureInfo> configuredTextures)
string shaderString = null;
var shaderName = Path.GetFileNameWithoutExtension(path);
var graph = JsonUtility.FromJson<T>(textGraph);
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph);
var name = Path.GetFileNameWithoutExtension(path);
var shaderString = graph.GetShader(string.Format("graphs/{0}", name), GenerationMode.ForReals, out configuredTextures);
//Debug.Log(shaderString);
return shaderString;
if (!string.IsNullOrEmpty(graph.path))
shaderName = graph.path + "/" + shaderName;
shaderString = graph.GetShader(shaderName, GenerationMode.ForReals, out configuredTextures);
}
catch (Exception)
{

return null;
return shaderString ?? k_ErrorShader.Replace("Hidden/GraphErrorShader2", shaderName);
}
}

正在加载...
取消
保存