浏览代码

Merge branch 'new-importer-workflow' into hackweek2017

/main
Tim Cooper 8 年前
当前提交
3094ee75
共有 14 个文件被更改,包括 344 次插入136 次删除
  1. 128
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphEditWindow.cs
  2. 2
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphInspector.cs
  3. 8
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphPresenter.cs
  4. 2
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphEditWindow.cs
  5. 88
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Presenters/TitleBarPresenter.cs
  6. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderGraph.cs
  7. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  8. 67
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialGraphAsset.cs
  9. 58
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/HelperShader.shader
  10. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/HelperShader.shader.meta
  11. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers.meta
  12. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs.meta
  13. 86
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs

128
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphEditWindow.cs


using System;
using System.IO;
using RMGUI.GraphView;
using UnityEngine;
using UnityEngine.Graphing;

{
// TODO JOCE Derive from GraphViewEditorWindow
public abstract class AbstractGraphEditWindow<T> : EditorWindow, ISerializationCallbackReceiver where T : class, IGraphAsset
public abstract class AbstractGraphEditWindow : EditorWindow, ISerializationCallbackReceiver
public RenderTexture rt;
[NonSerialized]
private IGraphAsset m_Selected;
[NonSerialized]
private T m_LastSelection;
[NonSerialized]
private IGraphAsset m_InMemoryAsset;
[SerializeField]
private ScriptableObject m_LastSelectedGraphSerialized;
[SerializeField]
private ScriptableObject m_ToLoad;
return m_LastSelection != null && m_LastSelection.shouldRepaint;
return m_InMemoryAsset != null && m_InMemoryAsset.shouldRepaint;
}
}

void OnEnable()
{
var source = CreateDataSource();
source.Initialize(m_LastSelection, this);
source.Initialize(m_InMemoryAsset, this);
m_GraphEditorDrawer = new GraphEditorDrawer(CreateGraphView(), source);
rootVisualContainer.AddChild(m_GraphEditorDrawer);

void Update()
{
if (m_ToLoad)
{
ChangeSelction (m_ToLoad as IGraphAsset);
m_ToLoad = null;
}
if (shouldRepaint)
Repaint();
}

focused = true;
}
void OnSelectionChange()
{
if (Selection.activeObject == null || !EditorUtility.IsPersistent(Selection.activeObject))
return;
public void PingAsset()
{
if (m_Selected != null)
EditorGUIUtility.PingObject(m_Selected.GetScriptableObject());
}
if (Selection.activeObject is ScriptableObject)
{
var selection = Selection.activeObject as T;
if (selection!= null && selection != m_LastSelection)
{
var graph = selection.graph;
graph.OnEnable();
graph.ValidateGraph();
m_LastSelection = selection;
public void UpdateAsset()
{
if (m_Selected != null && m_Selected is IGraphAsset)
{
var path = AssetDatabase.GetAssetPath (m_Selected.GetScriptableObject());
if (!string.IsNullOrEmpty(path) && m_InMemoryAsset != null)
{
File.WriteAllText (path, EditorJsonUtility.ToJson (m_InMemoryAsset.graph as object));
AssetDatabase.ImportAsset (path);
var source = CreateDataSource();
source.Initialize(m_LastSelection, this);
m_GraphEditorDrawer.presenter = source;
//m_GraphView.StretchToParentSize();
Repaint();
focused = false;
m_GraphEditorDrawer.graphView.Schedule(Focus).StartingIn(1).Until(() => focused);
}
}
}
var asset = AssetDatabase.LoadAssetAtPath<ScriptableObject> (path) as IGraphAsset;
if (asset != null) {
m_Selected = null;
ChangeSelction (asset, false);
}
}
}
}
public void ChangeSelction(IGraphAsset newSelection, bool refocus = true)
{
if (!(newSelection is ScriptableObject))
return;
var newGraph = (ScriptableObject)newSelection;
if (!EditorUtility.IsPersistent (newGraph))
return;
if (m_Selected == newSelection)
return;
if (m_Selected != null) {
if (EditorUtility.DisplayDialog ("Save Old Graph?", "Save Old Graph?", "yes!", "no")) {
UpdateAsset ();
}
}
m_Selected = newSelection;
m_InMemoryAsset = UnityEngine.Object.Instantiate (newGraph) as IGraphAsset;
var graph = m_InMemoryAsset.graph;
graph.OnEnable ();
graph.ValidateGraph ();
var source = CreateDataSource ();
source.Initialize (m_InMemoryAsset, this) ;
m_GraphEditorDrawer.presenter = source;
//m_GraphView.StretchToParentSize();
Repaint ();
if (refocus)
{
focused = false;
m_GraphEditorDrawer.graphView.Schedule (Focus).StartingIn (1).Until (() => focused);
}
}
public void OnBeforeSerialize()
{
m_ToLoad = m_Selected as ScriptableObject;
}
public void OnAfterDeserialize()
{}
/*
private void ConvertSelectionToSubGraph()

m_Canvas.Repaint();
}*/
public void OnBeforeSerialize()
{
var o = m_LastSelection as ScriptableObject;
if (o != null)
m_LastSelectedGraphSerialized = o;
}
public void OnAfterDeserialize()
{
if (m_LastSelectedGraphSerialized != null)
m_LastSelection = m_LastSelectedGraphSerialized as T;
m_LastSelectedGraphSerialized = null;
}
}
}

2
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphInspector.cs


protected IGraphAsset graphAsset
{
get { return target as IGraphAsset; }
get { return target as IGraphAsset; }
}
protected AbstractGraphInspector(IEnumerable<TypeMapping> typeMappings)

8
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphPresenter.cs


public IGraphAsset graphAsset { get; private set; }
[SerializeField]
private TitleBarPresenter m_TitleBar;
private TitleBarPresenter m_TitleBar;
public TitleBarPresenter titleBar
public TitleBarPresenter titleBar
{
get { return m_TitleBar; }
}

m_Elements.AddRange(drawableEdges.OfType<GraphElementPresenter>());
}
public virtual void Initialize(IGraphAsset graphAsset, EditorWindow container)
public virtual void Initialize(IGraphAsset graphAsset, AbstractGraphEditWindow container)
m_TitleBar.Initialize(graphAsset);
m_TitleBar.Initialize(container);
if (graphAsset == null)
return;

2
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphEditWindow.cs


namespace UnityEditor.Graphing.Drawing
{
public class GraphEditWindow : AbstractGraphEditWindow<IGraphAsset>
public class GraphEditWindow : AbstractGraphEditWindow
{
[MenuItem("Window/Graph Editor")]
public static void OpenMenu()

88
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Presenters/TitleBarPresenter.cs


using UnityEngine.Graphing;
using System.Collections.Generic;
using UnityEngine.MaterialGraph;
using System.IO;
using System.Linq;
public class TitleBarPresenter : ScriptableObject
public class TitleBarPresenter : ScriptableObject
IGraphAsset m_graphAsset;
AbstractGraphEditWindow m_Owner;
public IEnumerable<TitleBarButtonPresenter> leftItems
{

}
protected TitleBarPresenter()
{
}
{}
public void Initialize(IGraphAsset graphAsset)
public void Initialize(AbstractGraphEditWindow graphWindow)
m_graphAsset = graphAsset;
m_Owner = graphWindow;
currentGraphItem.text = graphAsset != null ? graphAsset.GetScriptableObject().name : "";
currentGraphItem.text = "Put name here";
m_leftItems.Add(currentGraphItem);
var updateAsset = CreateInstance<TitleBarButtonPresenter>();

var showInProjectItem = CreateInstance<TitleBarButtonPresenter>();
showInProjectItem.text = "Show in project";
showInProjectItem.onClick += OnShowInProjectClick;
m_leftItems.Add(showInProjectItem);
var showInProjectItem = CreateInstance<TitleBarButtonPresenter>();
showInProjectItem.text = "Show in project";
showInProjectItem.onClick += OnShowInProjectClick;
m_leftItems.Add(showInProjectItem);
var selectGraph = CreateInstance<TitleBarButtonPresenter>();
selectGraph.text = "Select Graph";
selectGraph.onClick += SelectGraph;
m_leftItems.Add(selectGraph);
var optionsItem = CreateInstance<TitleBarButtonPresenter>();
optionsItem.text = "Options";

void OnShowInProjectClick()
{
if (m_graphAsset != null)
EditorGUIUtility.PingObject(m_graphAsset.GetScriptableObject());
}
void OnShowInProjectClick()
{
if (m_Owner != null)
m_Owner.PingAsset ();
}
class CallbackData
{
public MaterialGraphAsset asset;
public AbstractGraphEditWindow owner;
}
public static List<MaterialGraphAsset> FindAssets()
{
var assets = new List<MaterialGraphAsset>();
string[] guids = AssetDatabase.FindAssets(string.Format("t:MaterialGraphAsset", typeof(MaterialGraphAsset)));
for( int i = 0; i < guids.Length; i++ )
{
string assetPath = AssetDatabase.GUIDToAssetPath( guids[i] );
MaterialGraphAsset asset = AssetDatabase.LoadAssetAtPath<MaterialGraphAsset>( assetPath );
if( asset != null )
{
assets.Add(asset);
}
}
return assets;
}
void SelectGraph()
{
var options = FindAssets().Where (x => EditorUtility.IsPersistent (x)).ToList ();
var gm = new GenericMenu ();
foreach (var option in options) {
gm.AddItem (new GUIContent (AssetDatabase.GetAssetPath (option)), false, Callback, new CallbackData(){asset = option, owner = m_Owner});
}
gm.ShowAsContext ();
}
void Callback(object userData)
{
if (!(userData is CallbackData))
return;
var cbData = (CallbackData)userData;
cbData.owner.ChangeSelction (cbData.asset);
}
if (m_graphAsset != null && m_graphAsset is MaterialGraphAsset)
{
var mg = (MaterialGraphAsset)m_graphAsset;
mg.RegenerateInternalShader();
}
//TODO: We need two currently.. fix later
m_Owner.UpdateAsset ();
m_Owner.UpdateAsset ();
}
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderGraph.cs


public override void Action(int instanceId, string pathName, string resourceFile)
{
var graph = CreateInstance<MaterialGraphAsset>();
graph.name = Path.GetFileName(pathName);
AssetDatabase.CreateAsset(graph, pathName);
var graph = new UnityEngine.MaterialGraph.MaterialGraph();
File.WriteAllText(pathName, EditorJsonUtility.ToJson(graph));
AssetDatabase.Refresh ();
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


namespace UnityEditor.MaterialGraph.Drawing
{
public class MaterialGraphEditWindow : AbstractGraphEditWindow<IMaterialGraphAsset>
public class MaterialGraphEditWindow : AbstractGraphEditWindow
{
[MenuItem("Window/Material Editor")]
public static void OpenMenu()

67
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialGraphAsset.cs


get { return m_MaterialGraph; }
}
public MaterialGraph materialGraph
{
get { return m_MaterialGraph; }
set { m_MaterialGraph = value; }
}
#if UNITY_EDITOR
public static bool ShaderHasError(Shader shader)
{

}
public bool RegenerateInternalShader()
{
if (m_MaterialGraph.masterNode == null)
return false;
var path = "Assets/GraphTemp.shader";
List<PropertyGenerator.TextureInfo> configuredTextures;
var shaderString = m_MaterialGraph.masterNode.GetFullShader(GenerationMode.ForReals, out configuredTextures);
File.WriteAllText(path, shaderString);
AssetDatabase.ImportAsset(path);
var shader = AssetDatabase.LoadAssetAtPath(path, typeof(Shader)) as Shader;
if (shader == null)
return false;
var shaderImporter = AssetImporter.GetAtPath(path) as ShaderImporter;
if (shaderImporter == null)
return false;
var textureNames = new List<string>();
var textures = new List<Texture>();
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable == TexturePropertyChunk.ModifiableState.Modifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetDefaultTextures(textureNames.ToArray(), textures.ToArray());
textureNames.Clear();
textures.Clear();
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable == TexturePropertyChunk.ModifiableState.NonModifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetNonModifiableTextures(textureNames.ToArray(), textures.ToArray());
shaderImporter.SaveAndReimport();
var imported = shaderImporter.GetShader();
if (m_GeneratedShader == null)
{
m_GeneratedShader = Instantiate(imported);
AssetDatabase.AddObjectToAsset(m_GeneratedShader, this);
}
else
{
AssetDatabase.CopyAsset(imported, m_GeneratedShader);
DestroyImmediate(imported, true);
}
AssetDatabase.DeleteAsset(path);
return true;
}
#endif

58
MaterialGraphProject/Assets/UnityShaderEditor/Editor/HelperShader.shader


Shader "Unlit/NewUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/HelperShader.shader.meta


fileFormatVersion: 2
guid: 9ab5e16c2083a4fe689209a8c1ae425e
timeCreated: 1495436993
licenseType: Pro
ShaderImporter:
defaultTextures: []
nonModifiableTextures:
- Texture_2ad4e61d_d44e_4e70_b3d0_540eae630bdf_Uniform: {fileID: 2800000, guid: 76c3d794167d8524481195736f067518,
type: 3}
userData:
assetBundleName:
assetBundleVariant:

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers.meta


fileFormatVersion: 2
guid: a1485bce8acd0428fae1525629b9ed8b
folderAsset: yes
timeCreated: 1495357309
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs.meta


fileFormatVersion: 2
guid: 625f186215c104763be7675aa2d941aa
timeCreated: 1495357324
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

86
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs


using UnityEditor.Experimental.AssetImporters;
using UnityEngine.MaterialGraph;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
#if UNITY_EDITOR
using System.Reflection;
using UnityEditor;
#endif
using UnityEngine.Graphing;
using System.Text;
[ScriptedImporter(1, "ShaderGraph")]
public class ShaderGraphImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)
{
var textGraph = File.ReadAllText(ctx.assetPath, Encoding.UTF8);
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph);
if (graph == null)
return;
var graphAsset = ScriptableObject.CreateInstance<MaterialGraphAsset> ();
graphAsset.materialGraph = graph;
ctx.SetMainAsset("MainAsset", graphAsset);
var shader = RegenerateShader (graphAsset);
if (shader == null)
return;
ctx.AddSubAsset("Shader", shader);
}
public Shader RegenerateShader(MaterialGraphAsset asset)
{
IMasterNode masterNode = asset.materialGraph.masterNode;
if (masterNode == null)
return null;
var path = "Assets/UnityShaderEditor/Editor/HelperShader.shader";
List<PropertyGenerator.TextureInfo> configuredTextures;
var shaderString = masterNode.GetFullShader(GenerationMode.ForReals, out configuredTextures);
var shader = AssetDatabase.LoadAssetAtPath(path, typeof(Shader)) as Shader;
if (shader == null)
return null;
File.WriteAllText (path, shaderString);
ShaderUtil.UpdateShaderAsset (shader, shaderString);
var shaderImporter = AssetImporter.GetAtPath(path) as ShaderImporter;
if (shaderImporter == null)
return null;
var textureNames = new List<string>();
var textures = new List<Texture>();
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable == TexturePropertyChunk.ModifiableState.Modifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetDefaultTextures(textureNames.ToArray(), textures.ToArray());
textureNames.Clear();
textures.Clear();
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable == TexturePropertyChunk.ModifiableState.NonModifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetNonModifiableTextures(textureNames.ToArray(), textures.ToArray());
shaderImporter.SaveAndReimport();
var imported = shaderImporter.GetShader();
return imported;
}
}
正在加载...
取消
保存