浏览代码

[Improvements]Change workflow to work with new montreal importer work (scriptable importer)

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

100
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
[NonSerialized]
private T m_LastSelection;
[NonSerialized]
private IGraphAsset m_Selected;
[SerializeField]
private ScriptableObject m_LastSelectedGraphSerialized;
[NonSerialized]
private IGraphAsset m_InMemoryAsset;
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);

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.Refresh ();
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)
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;
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);
}
}
/*
private void ConvertSelectionToSubGraph()

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

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

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

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


fileFormatVersion: 2
guid: 9ab5e16c2083a4fe689209a8c1ae425e
timeCreated: 1495403711
licenseType: Pro
ShaderImporter:
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

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


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

85
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;
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;
}
}

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:
正在加载...
取消
保存